time class

0 paras taneja · April 12, 2015
printf is not  working , what to do ?? and how to print string format of time in tomilitary function itself ??

Post a Reply

Replies

Oldest  Newest  Rating
0 Eyeshield Rosalin · April 12, 2015
This code is handy sometimes, I'm glad I have it in my vault haha!

Java Programming Tutorial - 36 - Time Class
code:
public class apples {
   public static void main(String[] args) {
      tuna tunaObject = new tuna();
      System.out.println(tunaObject.toMilitary());
      tunaObject.setTime(13, 27, 6);
      System.out.println(tunaObject.toMilitary());
   }

}

public class tuna {
   private int hour;
   private int minute;
   private int second;
   
   public void setTime(int h, int m, int s){
      hour = ((h>=0 && h<24) ? h : 0);
      minute = ((m>=0 && m<60) ? m : 0);
      hour = ((s>=0 && s<60) ? s : 0);
   }
   
   public  String toMilitary(){
      return String.format("%02d:%02d:%02d", hour, minute, second);
   }
}
  • 1

Java / Android Development

107,278 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator