mmayoub

School, 08.10.2017, Date class

Oct 8th, 2017
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Date.java
  2. ----------
  3.  
  4. public class Date {
  5.     int day;
  6.     int month;
  7.     int year;
  8.  
  9.     public Date() {
  10.         this.day = 1;
  11.         this.month = 1;
  12.         this.year = 1970;
  13.     }
  14.  
  15.     public Date(int day, int month, int year) {
  16.         this.day = day;
  17.         this.month = month;
  18.         this.year = year;
  19.     }
  20.  
  21.     public int getDay() {
  22.         return day;
  23.     }
  24.  
  25.     public void setDay(int day) {
  26.         this.day = day;
  27.     }
  28.  
  29.     public int getMonth() {
  30.         return month;
  31.     }
  32.  
  33.     public void setMonth(int month) {
  34.         this.month = month;
  35.     }
  36.  
  37.     public int getYear() {
  38.         return year;
  39.     }
  40.  
  41.     public void setYear(int year) {
  42.         this.year = year;
  43.     }
  44.  
  45.     @Override
  46.     public String toString() {
  47.         return day + "/" + month + "/" + year;
  48.     }
  49.  
  50. }
  51.  
  52.  
  53. RunDate.java
  54. ------------
  55.  
  56. public class RunDate {
  57.     public static void main(String[] args) {
  58.         Date d1, d2;
  59.  
  60.         d1 = new Date();
  61.         System.out.println("d1 = " + d1);
  62.  
  63.         d2 = new Date(8, 10, 2017);
  64.         // not a good way
  65.         // d2.setDay(8);
  66.         // d2.setMonth(10);
  67.         // d2.setYear(2017);
  68.         System.out.println("d2 = " + d2);
  69.  
  70.         Date d3 = new Date(26, 5, 2001);
  71.         System.out.println("Happy Birthday: " + d3);
  72.     }
  73. }
RAW Paste Data