CymsSoul

Untitled

Dec 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1.  
  2. import java.util.Calendar;
  3.  
  4.  
  5. public class MyDate {
  6.  
  7. private int day;
  8. private int month;
  9. private int year;
  10.  
  11. public MyDate(int pv, int kk, int vv) {
  12. this.day = pv;
  13. this.month = kk;
  14. this.year = vv;
  15. }
  16.  
  17.  
  18.  
  19. public String toString() {
  20. return this.day + "." + this.month + "." + this.year;
  21. }
  22.  
  23.  
  24.  
  25. public boolean earlier(MyDate compared) {
  26. if (this.year < compared.year) {
  27. return true;
  28. }
  29.  
  30. if (this.year == compared.year && this.month < compared.month) {
  31. return true;
  32. }
  33.  
  34. if (this.year == compared.year && this.month == compared.month
  35. && this.day < compared.day) {
  36. return true;
  37. }
  38.  
  39. return false;
  40. }
  41.  
  42. /*
  43. * In assignment 92 method differneceInYears was added to MyDate
  44. * Copy the method here since it eases this assignment considerably.
  45. */
  46. public int differneceInYears(MyDate compared) {
  47. if(this.earlier(compared)){
  48. return differenceWhenThisEarlier(compared);
  49. }
  50.  
  51. int minusOneYear = 0;
  52. if (this.month < compared.month) {
  53. minusOneYear = 1;
  54. } else if (this.month == compared.month && this.day < compared.day) {
  55. minusOneYear = 1;
  56. }
  57. return this.year - compared.year - minusOneYear;
  58. }
  59.  
  60. private int differenceWhenThisEarlier(MyDate compared)
  61. {
  62. int minusOneYear = 0;
  63.  
  64. if(compared.month < this.month){
  65. minusOneYear = 1;
  66. }
  67. else if (compared.month == this.month && compared.day < this.day){
  68. minusOneYear = 1;
  69. }
  70.  
  71. return compared.year - minusOneYear;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment