2011-08-07 162 views
2

我有一个名为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

回答

3

的Java超出它的方式,使这个困难,通过使极其笨拙的请求时间格式化不是默认时区。最简单的方法是自己做算术 -

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

或者其他东西。 (当然,你有前导零,在Java另一个困难格式化列的问题。)

+0

看到,做了很多次。结合'Formatter's或'String.format()',可以处理大多数常见的案例 – Asaf

+0

嗯,是的 - 他们终于在Java 5中引入了Formatter。 –

+0

This works thank you – maas

1
Time totalTime = new Time(total) 

Time(long)

+0

我试过,但它给我错误的价值观,在结果为毫秒为21600000,但是当我使用上面的代码时,它显示了09:00:00它应该显示的位置6:00:00 – maas

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

它没有工作asaf – maas

+0

我试过了,但它显示出错误的值,结果为毫秒是21600000,但是当我使用上面的代码时,它显示了09:00:00它应该显示的位置6:00:00 – maas

+0

我怀疑这里的问题是由于'SimpleDateFormat'使用默认时区 - 但基本上你是不格式化一个日期 - 你正在格式化一个持续时间。我会亲自避免在这里使用'Date'。 –

2

有在任何类型的内置Java库来处理持续时间。我建议你使用Joda Time及其Duration类。那么您可能想要将Duration转换为Period,并将Period的格式设置为PeriodFormatter。 (根据您的确切要求,你可能想建立一个Period入手,而不是一个Duration的。)

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

这是我关于整个毫秒想法

+1

请简短地解释一句话或者两个,你的代码中有什么神奇的东西。请参阅常见问题解答部分编写答案。谢谢。 – mtk