Advertisement
allerost

DateFormat

Jan 7th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.06 KB | None | 0 0
  1. package ar223ni_assign3;
  2.  
  3. import java.util.Date;
  4.  
  5. public class DateFormat {
  6.  
  7.     //Add all my variables
  8.     private int year = 0;
  9.     private int month = 0;
  10.     private int day = 0;
  11.     private char punctutation;
  12.     private char format;
  13.     private boolean invalidDate = false;
  14.     private String newDay;
  15.     private String newMonth;
  16.     private String finalDate;
  17.  
  18.     //Create my two constructors
  19.     public DateFormat(int year, int month, int day, char punctuation, char format){
  20.         this.year = year;
  21.         this.month = month;
  22.         this.day = day;
  23.         this.punctutation = punctuation;
  24.         this.format = format;
  25.     }
  26.  
  27.     public DateFormat(){
  28.  
  29.     }
  30.  
  31.     public void setYear(int year){
  32.         this.year = year;
  33.     }
  34.  
  35.     public int getYear(){
  36.         return year;
  37.     }
  38.  
  39.     public int getMonth() {
  40.         return month;
  41.     }
  42.     //Making sure that the month inst above 12
  43.     public void setMonth(int month) {
  44.         if(month > 12){
  45.             invalidDate = true;
  46.             this.month = 0;
  47.         }
  48.         this.month = month;
  49.     }
  50.  
  51.     public int getDay() {
  52.         return day;
  53.     }
  54.  
  55.     public void setDay(int day) {
  56.         this.day = day;
  57.     }
  58.  
  59.  
  60.  
  61.     public char getPunctuation() {
  62.         return punctutation;
  63.     }
  64.  
  65.     public void setPunctuation(char punctutation) {
  66.         this.punctutation = punctutation;
  67.     }
  68.  
  69.     public char getFormat() {
  70.         return format;
  71.     }
  72.  
  73.     public void setFormat(char format) {
  74.         this.format = format;
  75.  
  76.     }
  77.     //b = year, month ,day
  78.     //m = month, day , year
  79.     // l = day, month, year
  80.     public String getDate(boolean fullYear){
  81.         if(month > 12 || day > 31){
  82.             invalidDate = true;
  83.         }
  84.         //If the day is larger than 31, make it say 0
  85.         newDay = Integer.toString(day);
  86.         if(day > 31){
  87.             invalidDate = true;
  88.             newDay = "0";
  89.         }
  90.         //If the day is less than 10, covert to string and add a 0 before it
  91.         else if(day < 10){
  92.             newDay = "0" + Integer.toString(day);
  93.         }
  94.  
  95.         //Also convert to string and add a 0
  96.         if(month < 10){
  97.             newMonth = "0" + Integer.toString(month);
  98.         }
  99.         if(fullYear == true){ //For the year to be printed in full
  100.             //Checking that the date isnt invalid
  101.             if(invalidDate == true){
  102.                 finalDate = "Invalid Date!";
  103.             }
  104.             else{
  105.                 //print out with full year
  106.                 if(getPunctuation() == '!'){
  107.                     //Nothing between the numbers
  108.                     if(getFormat() == 'b'){
  109.                         finalDate  = year + "" + newMonth + "" + newDay;
  110.                     }
  111.                     if(getFormat() == 'l'){
  112.                         finalDate =  newDay + "" + newMonth + "" + year;
  113.                     }
  114.                     if(getFormat() == 'm'){
  115.                         finalDate =  newMonth + "" + newDay + "" + year;
  116.                     }
  117.                 }
  118.                 else{
  119.                     if(getFormat() == 'b'){
  120.                         finalDate =  year + "" + punctutation + "" + newMonth + "" + punctutation + "" + newDay;
  121.                     }
  122.                     if(getFormat() == 'l'){
  123.                         finalDate = newDay + "" + punctutation + "" + newMonth + "" + punctutation + "" + year;
  124.                     }
  125.                     if(getFormat() == 'm'){
  126.                         finalDate = newMonth + "" + punctutation + "" + newDay + "" + punctutation + "" + year;
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.         else{
  132.             //for the year to be printed with only the last two digits
  133.             if(invalidDate == true){
  134.                 //System.out.println("Invalid date!");
  135.                 finalDate = "Invalid Date!";
  136.             }
  137.             //This removes the first two digits making 1999 -> 99
  138.             year = year % 100;
  139.  
  140.             if(getPunctuation() == '!'){
  141.                 //Nothing betweeen the numbers
  142.                 if(getFormat() == 'b'){
  143.                     finalDate =  year + "" + newMonth + "" + newDay;
  144.                 }
  145.                 if(getFormat() == 'l'){
  146.                     finalDate =  newDay + "" + newMonth + "" + year;
  147.                 }
  148.                 if(getFormat() == 'm'){
  149.                     finalDate = newMonth + "" + newDay + "" + year;
  150.                 }
  151.             }
  152.             else{
  153.                 if(getFormat() == 'b'){
  154.                     finalDate = year + "" + punctutation + "" + newMonth + "" + punctutation + "" + newDay;
  155.                 }
  156.                 if(getFormat() == 'l'){
  157.                     finalDate =  newDay + "" + punctutation + "" + newMonth + "" + punctutation + "" + year;
  158.                 }
  159.                 if(getFormat() == 'm'){
  160.                     finalDate = newMonth + "" + punctutation + "" + newDay + "" + punctutation + "" + year;
  161.                 }
  162.             }
  163.         }
  164.         //Return the final formatted date
  165.         return finalDate;
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement