2011-08-07 165 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