Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. public class BusinessDate {
  2.  
  3.  
  4.     public BusinessDate(int pDay, int pMonth, int pYear) {
  5.         setMonth(pMonth);
  6.         setYear(pYear);
  7.         setDay(pDay);
  8.     }
  9.    
  10.     private int day = 0;
  11.     private int month = 0;
  12.     private int year = 0;
  13.  
  14.     public int getDay() {
  15.         return day;
  16.     }
  17.    
  18.     public int getMonth() {
  19.         return month;
  20.     }
  21.    
  22.     public int getYear() {
  23.         return year;
  24.     }
  25.  
  26.     public void setMonth(int month) {
  27.         if(month < 1){
  28.             this.month = 1;
  29.         }
  30.        
  31.         if(month > 12){
  32.             this.month = 12;
  33.         }
  34.         else
  35.             this.month = month;
  36.     }
  37.    
  38.     public void setYear(int year) {
  39.         if(year < 0){
  40.             this.year = 0;
  41.         }
  42.         if(year > 3000){
  43.             this.year = 3000;
  44.         }
  45.         else
  46.             this.year = year;
  47.     }
  48.    
  49.     public void setDay(int day) {
  50.         if(day < 1)
  51.             this.day = 1;
  52.        
  53.         else  {
  54.             switch(month){
  55.             case 1:
  56.                 if(day > 31)
  57.                     this.day = 31;
  58.                 else
  59.                     this.day = day;
  60.                 break;
  61.             case 2:
  62.                 if(isLeapYear(day) == true)
  63.                     this.day = 29;
  64.                 else if (day > 30 && isLeapYear(day) == true)
  65.                     this.day = 29;
  66.                 else if (day > 29 && isLeapYear(day) == false)
  67.                     this.day = 28; 
  68.                 break;
  69.                
  70.             case 3:
  71.                 if(day >31)
  72.                     this.day = 31;
  73.                 else
  74.                     this.day = day;
  75.                 break;
  76.                
  77.             case 4:
  78.                 if (day < 30)
  79.                     this.day = 30;
  80.                 else
  81.                     this.day = day;
  82.                 break;
  83.                
  84.             case 5:
  85.                 if (day > 31)
  86.                     this.day = 31;
  87.                 else
  88.                     this.day = day;
  89.                 break;
  90.                
  91.             case 6:
  92.                 if(day > 30)
  93.                     this.day = 30;
  94.                 else
  95.                     this.day = day;
  96.                 break;
  97.                
  98.             case 7:
  99.                 if (day > 31)
  100.                     this.day = 31;
  101.                 else
  102.                     this.day = day;
  103.                 break;
  104.                
  105.             case 8:
  106.                 if (day > 31)
  107.                     this.day = 31;
  108.                 else
  109.                     this.day = day;
  110.                 break;
  111.                
  112.             case 9:
  113.                 if (day > 30)
  114.                     this.day = 30;
  115.                 else
  116.                     this.day = day;
  117.                 break;
  118.                
  119.             case 10:
  120.                 if (day > 31)
  121.                     this.day = 31;
  122.                 else
  123.                     this.day = day;
  124.                 break;
  125.                
  126.             case 11:
  127.                 if(day > 30)
  128.                     this.day = 30;
  129.                 else
  130.                     this.day = day;
  131.                 break;
  132.                
  133.             case 12:
  134.                 if(day > 31)
  135.                     this.day = 31;
  136.                 else
  137.                     this.day = day;
  138.             default:
  139.                 this.day = day;
  140.             }
  141.         }
  142.     }
  143.  
  144.    
  145.     public boolean isLeapYear(int year) {
  146.         if (year % 4 != 0) {
  147.             return false;
  148.         } else if (year % 400 == 0) {
  149.             return true;
  150.         } else if (year % 100 == 0) {
  151.             return false;
  152.         } else {
  153.             return true;
  154.         }
  155.     }
  156.  
  157.     public void print() {
  158.         System.out.println(day + "." + month + "." + year);
  159.     }
  160. }
Add Comment
Please, Sign In to add comment