Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. package project.two;
  2.  
  3. public class Date {
  4.  
  5. private int day;
  6. private int month;
  7. private int year;
  8.  
  9. Date(){} // No parameter constructor
  10.  
  11. Date(int day){this(day, 0,0);}
  12.  
  13. Date(int day, int month){this(day, month,0);}
  14.  
  15. Date(int day, int month, int year){
  16. this.day=day;
  17. this.month=month;
  18. this.year = year;
  19. }
  20.  
  21. void setDate(int day, int month, int year) {
  22.  
  23. if(day < 0 || day> 7||month <0 || month > 31|| year < 0)
  24. System.out.println("Illegal inputs.");
  25.  
  26. this.day=day;
  27. this.month=month;
  28. this.year=year;
  29. }
  30.  
  31. void setDay(int day){this.day=day;}
  32. void setMonth(int month){this.month= month;}
  33. void setYear(int year){this.year=year;}
  34.  
  35. int getDay(){return day;}
  36. int getMonth(){return month;}
  37. int getYear(){return year;}
  38.  
  39. private boolean isLeapYear(){
  40. return (getYear() % 400 == 0);
  41. }
  42.  
  43. public int getDaysInTheMonth(int month){
  44. switch (month){
  45. case 1:case 3:
  46. case 5:case 7:
  47. case 8:case 10:
  48. case 12:
  49. return 31;
  50. case 2:
  51. return (isLeapYear()? 29: 28);
  52. case 4: case 6:
  53. case 9: case 11:
  54. return 30;
  55. default:
  56. return -1;
  57. }
  58. }
  59. public int daysPassedInYears(int year){
  60. int totalDays = 0;
  61. for(int i= 1; i <= getMonth(); i++){
  62. totalDays+= getDaysInTheMonth(i);
  63. }
  64. return year *365 + totalDays;
  65. }
  66.  
  67. public int daysRemainingItTheYear(){
  68. int totalDays = 0;
  69. for(int i= 1; i <= getMonth(); i++){
  70. totalDays+= getDaysInTheMonth(i);
  71. }
  72. return 360 - totalDays - getDay();
  73. }
  74.  
  75. public void addDays(int days){
  76.  
  77. int totalDays = days + getDay();
  78. while (true){
  79. if(totalDays > 28)
  80. setMonth(getMonth()+1);
  81. else if(getMonth() > 12)
  82. setYear(getYear()+1);
  83. else if(totalDays < 28) {
  84. setDay(totalDays);
  85. return;
  86. }
  87. totalDays-= getDaysInTheMonth(getMonth());
  88. }
  89. }
  90.  
  91. @Override
  92. public String toString(){
  93. return getMonth() +" - "+getDay()+" - "+getYear();
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement