将毫秒转换为时间

时间:2011-08-07 10:58:27

标签: java time

我有一个名为total的变量,我希望将其转换为时间。

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

那我怎么能这样做?我希望它的格式为HH:MM:SS

5 个答案:

答案 0 :(得分:3)

Java竭尽全力使其变得困难,因为请求格式化为默认时区以外的时间非常笨拙。最简单的方法是自己做算术 -

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

或者其他什么东西。 (当然,你会遇到使用前导零格式化列的问题,这是Java中的另一个难题。)

答案 1 :(得分:2)

内置Java库中没有类型来处理持续时间。我建议你使用Joda Time及其Duration类。然后,您可能希望将Duration转换为Period,并使用PeriodFormatter格式化Period。 (根据您的完全要求,您可能希望构建一个Period而不是Duration。)

答案 2 :(得分:1)

Time totalTime = new Time(total)

请参阅Time(long)

答案 3 :(得分:1)

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));

答案 4 :(得分:0)

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");
   }
}

这是我关于整整毫秒的想法