Today i have written a program to convert seconds into hours, minutes and seconds in java.  Any one can easily understand this program and implement in their web application without any difficulties. Definitely it is more usefull for those who are working on date/time related functionalites. Lets start understanding the example carefully.
package Examples;

public class ConvertSeconds2Time {
 
 public static String getFormattedTimeString(long timeInSeconds) {
  String timeStr = new String();
  long sec_term = 1;
  long min_term = 60 * sec_term;
  long hour_term = 60 * min_term;
  long result = Math.abs(timeInSeconds);

  int hour = (int) (result / hour_term);
  result = result % hour_term;
  int min = (int) (result / min_term);
  result = result % min_term;
  int sec = (int) (result / sec_term);

  if (timeInSeconds < 0) {
   timeStr = "-";
  }
  if (hour > 0) {
   timeStr += hour + "h ";
  }
  if (min > 0) {
   timeStr += min + "m ";
  }
  if (sec > 0) {
   timeStr += sec + "s ";
  }
  return timeStr;
 }
 public static void main(String args[]){
  System.out.println(getFormattedTimeString(64000));
 }

}

2 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete

 
Top