Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ar223ni_assign3;
- import java.util.Date;
- public class DateFormat {
- //Add all my variables
- private int year = 0;
- private int month = 0;
- private int day = 0;
- private char punctutation;
- private char format;
- private boolean invalidDate = false;
- private String newDay;
- private String newMonth;
- private String finalDate;
- //Create my two constructors
- public DateFormat(int year, int month, int day, char punctuation, char format){
- this.year = year;
- this.month = month;
- this.day = day;
- this.punctutation = punctuation;
- this.format = format;
- }
- public DateFormat(){
- }
- public void setYear(int year){
- this.year = year;
- }
- public int getYear(){
- return year;
- }
- public int getMonth() {
- return month;
- }
- //Making sure that the month inst above 12
- public void setMonth(int month) {
- if(month > 12){
- invalidDate = true;
- this.month = 0;
- }
- this.month = month;
- }
- public int getDay() {
- return day;
- }
- public void setDay(int day) {
- this.day = day;
- }
- public char getPunctuation() {
- return punctutation;
- }
- public void setPunctuation(char punctutation) {
- this.punctutation = punctutation;
- }
- public char getFormat() {
- return format;
- }
- public void setFormat(char format) {
- this.format = format;
- }
- //b = year, month ,day
- //m = month, day , year
- // l = day, month, year
- public String getDate(boolean fullYear){
- if(month > 12 || day > 31){
- invalidDate = true;
- }
- //If the day is larger than 31, make it say 0
- newDay = Integer.toString(day);
- if(day > 31){
- invalidDate = true;
- newDay = "0";
- }
- //If the day is less than 10, covert to string and add a 0 before it
- else if(day < 10){
- newDay = "0" + Integer.toString(day);
- }
- //Also convert to string and add a 0
- if(month < 10){
- newMonth = "0" + Integer.toString(month);
- }
- if(fullYear == true){ //For the year to be printed in full
- //Checking that the date isnt invalid
- if(invalidDate == true){
- finalDate = "Invalid Date!";
- }
- else{
- //print out with full year
- if(getPunctuation() == '!'){
- //Nothing between the numbers
- if(getFormat() == 'b'){
- finalDate = year + "" + newMonth + "" + newDay;
- }
- if(getFormat() == 'l'){
- finalDate = newDay + "" + newMonth + "" + year;
- }
- if(getFormat() == 'm'){
- finalDate = newMonth + "" + newDay + "" + year;
- }
- }
- else{
- if(getFormat() == 'b'){
- finalDate = year + "" + punctutation + "" + newMonth + "" + punctutation + "" + newDay;
- }
- if(getFormat() == 'l'){
- finalDate = newDay + "" + punctutation + "" + newMonth + "" + punctutation + "" + year;
- }
- if(getFormat() == 'm'){
- finalDate = newMonth + "" + punctutation + "" + newDay + "" + punctutation + "" + year;
- }
- }
- }
- }
- else{
- //for the year to be printed with only the last two digits
- if(invalidDate == true){
- //System.out.println("Invalid date!");
- finalDate = "Invalid Date!";
- }
- //This removes the first two digits making 1999 -> 99
- year = year % 100;
- if(getPunctuation() == '!'){
- //Nothing betweeen the numbers
- if(getFormat() == 'b'){
- finalDate = year + "" + newMonth + "" + newDay;
- }
- if(getFormat() == 'l'){
- finalDate = newDay + "" + newMonth + "" + year;
- }
- if(getFormat() == 'm'){
- finalDate = newMonth + "" + newDay + "" + year;
- }
- }
- else{
- if(getFormat() == 'b'){
- finalDate = year + "" + punctutation + "" + newMonth + "" + punctutation + "" + newDay;
- }
- if(getFormat() == 'l'){
- finalDate = newDay + "" + punctutation + "" + newMonth + "" + punctutation + "" + year;
- }
- if(getFormat() == 'm'){
- finalDate = newMonth + "" + punctutation + "" + newDay + "" + punctutation + "" + year;
- }
- }
- }
- //Return the final formatted date
- return finalDate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement