Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- public class DateClass {
- private int hours;
- private int minutes;
- private int seconds;
- // GETTERS, SETTERS
- public void setHours(int h){
- this.hours = h;
- }
- public void setMinutes(int m){
- this.minutes = m;
- }
- public void setSeconds(int s){
- this.seconds = s;
- }
- public int getHours(){
- return this.hours;
- }
- public int getMinutes(){
- return this.minutes;
- }
- public int getSeconds(){
- return this.seconds;
- }
- // CONSTRUCTORS
- public DateClass(int h, int m, int s){
- setHours(h);
- setMinutes(m);
- setSeconds(s);
- }
- public DateClass(int h, int m){
- this(h, m, 0);
- }
- public DateClass(int h){
- this(h, 0, 0);
- }
- public DateClass(){
- this(0, 0, 0);
- }
- // METHODS
- public void setTime(int h, int m, int s){
- this.hours = ((h>=0 && h<24) ? h : 0);
- this.minutes = ((m>=0 && m<60) ? m : 0);
- this.seconds = ((s>=0 && s<60) ? s : 0);
- }
- public String toMilitary(){
- return String.format("%02d : %02d : %02d", this.hours, this.minutes, this.seconds);
- }
- public String toOrdinary(){
- return String.format("%d : %02d : %02d %s", this.hours % 12, this.minutes, this.seconds, (this.hours >= 12 ? "AM" : "PM"));
- }
- public static void main(String args[]){
- System.out.println("Checking military and ordinary hours");
- DateClass dateclass = new DateClass();
- String str01 = dateclass.toMilitary();
- String str02 = dateclass.toOrdinary();
- System.out.println(str01);
- System.out.println(str02);
- dateclass.setTime(4, 23, 18);
- String str11 = dateclass.toMilitary();
- String str12 = dateclass.toOrdinary();
- System.out.println(str11);
- System.out.println(str12);
- dateclass.setTime(16, 23, 18);
- String str21 = dateclass.toMilitary();
- String str22 = dateclass.toOrdinary();
- System.out.println(str21);
- System.out.println(str22);
- System.out.println();
- System.out.println();
- DateClass d1 = new DateClass();
- DateClass d2 = new DateClass(14);
- DateClass d3 = new DateClass(14, 15);
- DateClass d4 = new DateClass(14, 15, 16);
- System.out.println(d1.toMilitary());
- System.out.println(d2.toMilitary());
- System.out.println(d3.toMilitary());
- System.out.println(d4.toMilitary());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement