Saturday, 3 September 2011

Time Class - to Display Time Formats in java


class time
{
private int hour,min,sec;
public void setTime(int h, int m, int s)
{
hour=((h>=0&&h<=24)?h:0);
min=((m>=0&&m<=60)?m:0);
sec=((s>=0&&s<=60)?s:0);
}
public String toMilitary()
{
return String.format("%02d:%02d:%02d %s", hour, min, sec, "hour");
}
public String toRegular()
{
return String.format("%d:%02d:%02d %s", ((hour==0||hour==12)?12:hour%12), min, sec, (hour<12)?"am":"pm");
}
}
class abc
{
public static void main(String args[])
{
time obj=new time();
obj.setTime(23,30,45);
System.out.println(obj.toMilitary());
System.out.println(obj.toRegular());
}
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.