简体   繁体   中英

Convert milliseconds to time

I am having a variable which is called total and I want to convert it into time.

Time from_time = rs.getTime("nfrm_time");
long f = from_time.getTime(); 
long t= to_time.getTime();
long total= t - f;

So how can I do that?I want it in the format of HH:MM:SS

Java goes out of its way to make this difficult, by making it exceedingly clumsy to request a time formatted for other than the default time zone. Simplest approach is to do the arithmetic yourself --

int hours = (int) (total / (60 * 60 * 1000));
int minutes = (int) (total / (60 * 1000)) % 60;
int seconds = (int) (total / 1000) % 60;

Or something thereabouts. (Of course, then you have the problem of formatting the columns with leading zeros, another difficulty in Java.)

There's no type in the built-in Java libraries to deal with durations. I suggest you use Joda Time and its Duration class. You'd then probably want to convert the Duration into a Period , and format that Period with a PeriodFormatter . (Depending on your exact requirements, you might want to build a Period to start with instead of a Duration .)

Time totalTime = new Time(total)

查看Time(long)

SimpleDateFormat sdf = new SimpleDateFormat("HH:MM:SS");

// Edit: setting the UTC time zone
TimeZone utc = TimeZone.getTimeZone("UTC");
sdf.setTimeZone(utc);

Date date = new Date(total);
System.out.println(sdf.format(date));
import java.util.Scanner;

public class Time {
   public static void main(String[] args) {


      Scanner input = new Scanner(System.in);
      System.out.print("enter offset from GMT: ");

      long offset = input.nextLong();

      long totalMilliseconds = System.currentTimeMillis();
      // NOTE: totalMilliseconds is the number of milliseconds since
      // 12am, Jan 1, 1970


      System.out.print("enter milliseconds: ");
      totalMilliseconds = input.nextLong();

      // Given totalMilliseconds and offset, compute hours, minutes, seconds,
      // and totalDays, where
      //   totalDays is the number of days since Jan 1, 1970
      //   hours, minutes, and seconds is the time of day today.
      // All values are adjusted according to the offset from GMT

      if (totalMilliseconds < 0) {
         System.out.printf("negative time!!\n");
         return;  // exit program
      }

      long totalSeconds = totalMilliseconds / 1000;
      // System.out.printf("unix time = %d\n", totalSeconds);

      long seconds = totalSeconds % 60;
      long totalMinutes = totalSeconds / 60;

      long minutes = totalMinutes % 60;
      long totalHours = totalMinutes / 60;

      totalHours += offset;
      if (totalHours < 0) {
         System.out.printf("negative time!!\n");
         return;
      }

      long hours = totalHours % 24;
      long totalDays = totalHours / 24;


      // Given totalDays, this computes yearAD, dayInYear, and leapInc, where
      //    totalDays is the number of days since Jan 1, 1970
      //    yearAD is the current year
      //    dayInYear is the day within the current year (in the range 0..364/365)
      //    leapInc is 1 if yearAD is a leap year and 0 otherwise

      long yearAD, dayInYear, leapInc;

      long yearOffset = totalDays / 365;

      dayInYear = totalDays % 365;
      yearAD = 1970 + yearOffset;

      long y = yearAD - 1;
      long numOfLeapYears =
         ((y/4) - (1969/4)) -
         ((y/100) - (1969/100)) + ((y/400) - (1969/400));

      dayInYear -= numOfLeapYears;

      leapInc = 0;
      if (yearAD % 4 == 0 && yearAD % 100 != 0 || yearAD % 400 == 0)
         leapInc = 1;

      if (dayInYear < 0) {
         dayInYear += 365 + leapInc;
         yearAD--;
      }

      if (dayInYear < 0) {
         System.out.printf("not implemented!!\n");
         return;
      }


      /* **************** Generate Output ************* */


      // Given hours, minutes, and seconds, output current time in friendly AM/PM format

      long friendlyHours;

      friendlyHours = hours;
      if (friendlyHours >= 12)
         friendlyHours -= 12;
      if (friendlyHours == 0)
         friendlyHours = 12;


      System.out.printf("%02d:%02d:%02d", friendlyHours, minutes, seconds);
      if (hours < 12)
         System.out.printf("am");
      else
         System.out.printf("pm");

      System.out.printf(", ");

      // given totalDays, output the day of the week (i.e., Sunday, Monday, etc.)
      // NOTE: Jan 1, 1970 was a Thursday

      long dayOfWeek = totalDays % 7;

      if (dayOfWeek == 0)
         System.out.printf("Thursday");
      else if (dayOfWeek == 1)
         System.out.printf("Friday");
      else if (dayOfWeek == 2)
         System.out.printf("Saturday");
      else if (dayOfWeek == 3)
         System.out.printf("Sunday");
      else if (dayOfWeek == 4)
         System.out.printf("Monday");
      else if (dayOfWeek == 5)
         System.out.printf("Tuesday");
      else if (dayOfWeek == 6)
         System.out.printf("Wednesday");

      System.out.printf(", ");

      // Given yearAD, dayInYear, and leapeapInc, output the date in usual format,
      // i.e., September 17, 2010

      long lower, upper;

      upper = 0;

      lower = upper;
      upper = lower + 31;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("January %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 28 + leapInc;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("February %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 31;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("March %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 30;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("April %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 31;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("May %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 30;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("June %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 31;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("July %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 31;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("August %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 30;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("September %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 31;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("October %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 30;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("November %d, ", dayInYear - lower + 1);

      lower = upper;
      upper = lower + 31;
      if (lower <= dayInYear  &&  dayInYear < upper)
         System.out.printf("December %d, ", dayInYear - lower + 1);

      System.out.printf("%d", yearAD);


      System.out.printf("\n");
   }
}

That is my idea about whole milliseconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM