Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
3.2 kB
4
Indexable
Never
package haider2;
public class Time {
    
    private String year;
    private String month;
    private String day;
    private String hour;
    private String minute;
    private String second;
    
    
    public Time() {
        // default constructor
    }
    
    public Time(String year, String month, String day) {
        this.year = year;
        this.month  = month;
        this.day = day;
    }
    
    
    public Time(String year, String month, String day, String hour, String minute, String second) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }
    
    public void setYear(String year) { this.year = year;}
    public void setMonth(String month) {this.month = month;}
    public void setDay(String day) {this.day = day;}
    public void setHour(String hour) {this.hour = hour;}
    public void setMinute(String minute) {this.minute = minute;}
    public void setSecond(String second) {this.second = second;}
    
    // getters
    
    public String getYear() {return year;}
    public String getMonth() {return month;}
    public String getDay() {return day;}
    public String getHour(){return hour;}
    public String getMinute() {return minute;}
    public String getSecond() {return second;}
            
            
            
            // string returner;
            
                public String getDDMMYY() {
        return day + " / " + month + " / " + year;
    }   
    public String getDateTime() {
        return day + " / " + month + " / " + year + " \n " + "Hour: " + hour + " \n " + "Minutes : " + minute + " \n " + "Seconds : " + second + " \n ";
    }
    
}

//----------------------------------------


package haider2;
import java.util.*;


public class AppClass {
 
    public static void main(String[] args) {
        
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        Time t1 = new Time();
        Time t2 = new Time(String.valueOf(c.get(Calendar.YEAR)), String.valueOf(c.get(Calendar.MONTH)), String.valueOf(c.get(Calendar.DATE)));
        Time t3 = new Time(String.valueOf(c.get(Calendar.YEAR)), String.valueOf(c.get(Calendar.MONTH)), String.valueOf(c.get(Calendar.DATE)), String.valueOf(c.get(Calendar.HOUR)), String.valueOf(c.get(Calendar.MINUTE)), String.valueOf(c.get(Calendar.SECOND)));
        
        t1.setDay(String.valueOf(c.get(Calendar.DATE)));
        t1.setMonth(String.valueOf(c.get(Calendar.MONTH)));
        t1.setYear(String.valueOf(c.get(Calendar.YEAR)));
        t1.setHour(String.valueOf(c.get(Calendar.HOUR)));
        t1.setMinute(String.valueOf(c.get(Calendar.MINUTE)));
        t1.setSecond(String.valueOf(c.get(Calendar.SECOND)));
        
        
        System.out.println("From default:");
        System.out.println(t1.getDateTime());
        System.out.println("From 3 parameters");
        System.out.println(t2.getDDMMYY() + " \n ");
        System.out.println("From 6 parameters");
        System.out.println(t3.getDateTime());
    }
    
}