2012-08-22 83 views
17

我想將當前日期轉換爲整數形式。默認情況下,它返回長。當我嘗試將long轉換爲整數,然後將整數值轉換爲日期意味着它顯示1970年代的日期。將當前日期轉換爲整數

int i = (int) new Date().getTime(); 
System.out.println("Integer : " + i); 
System.out.println("Long : "+ new Date().getTime()); 
System.out.println("Long date : " + new Date(new Date().getTime())); 
System.out.println("Int Date : " + new Date(i)); 

輸出將成爲

Integer : 1292838124 
Long : 1345617601771 
Long date : Wed Aug 22 12:10:01 IST 2012 
Int Date : Fri Jan 16 04:37:18 IST 1970 

任何一個請幫助我,如何爲當前日期轉換爲整數(10位數字)

+0

O/P是:整數:1293630553, 長:1345618394201, 長日期:星期三8月22日12點23分14秒北京時間2012, 詮釋日期:星期五1月16日4時50分30秒IST 1970年 –

+0

我認爲你給出了錯誤的輸出 –

+0

抱歉我的錯誤..現在我改變它 – Bathakarai

回答

30

問題是一個Integer並不大足以存儲當前日期,您需要使用Long。

日期在內部存儲爲自1970年1月1日以來的毫秒數。

的整數最大值爲2147483648,而自1970年以來的毫秒數是目前的1345618537869

把整數最大值爲日期收益率週一1月26日1970年

編輯順序:代碼根據下面的註釋顯示除以1000:

int i = (int) (new Date().getTime()/1000); 
    System.out.println("Integer : " + i); 
    System.out.println("Long : "+ new Date().getTime()); 
    System.out.println("Long date : " + new Date(new Date().getTime())); 
    System.out.println("Int Date : " + new Date(((long)i)*1000L)); 

Integer : 1345619256 
Long : 1345619256308 
Long date : Wed Aug 22 16:37:36 CST 2012 
Int Date : Wed Aug 22 16:37:36 CST 2012 
+0

謝謝你shonky,我接受你的答案。但我的要求是保持只有整數不長..是否有任何其他方式來實現我的要求。 – Bathakarai

+6

如果您願意以1秒的分辨率生活(您將失去日期的毫秒組分),那麼您可以在存儲整數之前除以1000。並乘以轉換回來。然而它的生命有限(一種千年蟲問題) - 高達2038年。 –

+0

我已將代碼添加到我的答案中,以顯示上述建議 –

0

您的問題是因爲getTime()。它總是返回以下。

返回自此日期對象表示的1970年1月1日00:00:00 GMT以來的毫秒數。

因爲最大整數值小於getTime()那麼爲什麼顯示 錯誤的結果。

4

你需要這樣的東西嗎(沒有時間)?

public static Integer toJulianDate(Date pDate) { 
if (pDate == null) { 
    return null; 
} 
Calendar lCal = Calendar.getInstance(); 
lCal.setTime(pDate); 
int lYear = lCal.get(Calendar.YEAR); 
int lMonth = lCal.get(Calendar.MONTH) + 1; 
int lDay = lCal.get(Calendar.DATE); 
int a = (14 - lMonth)/12; 
int y = lYear + 4800 - a; 
int m = lMonth + 12 * a - 3; 
return lDay + (153 * m + 2)/5 + 365 * y + y/4 - y/100 + y/400 - 32045; 
} 
+0

謝謝你。你解決了我的問題。 – Ponting

+0

@Stefan感謝您的解決方案。但是你能解釋一下這裏發生了什麼嗎? – Droidme

0

在我的Java 7,輸出不同的是:

Integer : 1293732698 
Long : 1345618496346 
Long date : Wed Aug 22 10:54:56 MSK 2012 
Int Date : Fri Jan 16 02:22:12 MSK 1970 

這是一個預期的行爲。

這是不可能顯示以毫秒爲單位的當前日期爲一個整數(10位數字),因爲最新的可能日期是Sun Apr 26 20:46:39 MSK 1970

Date d = new Date(9999_9999_99L); 
System.out.println("Date: " + d); 

Date: Sun Apr 26 20:46:39 MSK 1970 

你可能要考慮以秒/分鐘顯示它。

6

爲了得到當前日期作爲整數(10位數字),你需要劃分由長由新的日期()返回。的getTime() 1000.

這將在int範圍內,並且直到2038年1月18日爲止。

2

如果你只需要自1970年1月1日表示經過幾天的一個整數,你可以嘗試這些:

// magic number= 
// millisec * sec * min * hours 
// 1000 * 60 * 60 * 24 = 86400000 
public static final long MAGIC=86400000L; 

public int DateToDays (Date date){ 
    // convert a date to an integer and back again 
    long currentTime=date.getTime(); 
    currentTime=currentTime/MAGIC; 
    return (int) currentTime; 
} 

public Date DaysToDate(int days) { 
    // convert integer back again to a date 
    long currentTime=(long) days*MAGIC; 
    return new Date(currentTime); 
} 

較短,但不易閱讀(稍快?):

public static final long MAGIC=86400000L; 

public int DateToDays (Date date){ 
    return (int) (date.getTime()/MAGIC); 
} 

public Date DaysToDate(int days) { 
    return new Date((long) days*MAGIC); 
} 

希望這有助於。

編輯:這可能是工作,直到週五07月11日01:00:00 CET 5881580

-1

簡單的真正創造一個長變量,它表示你的程序 獲取日期到另一個長變量默認的起始日期。 然後扣除長的開始日期並轉換爲整數瞧 讀取和轉換回來只是添加而不是減去。顯然這取決於您需要的日期範圍有多大。

1

我已經解決了這個爲如下所示:

long year = calendar.get(Calendar.YEAR); 
    long month = calendar.get(Calendar.MONTH) + 1; 
    long day = calendar.get(Calendar.DAY_OF_MONTH); 
    long calcDate = year * 100 + month; 
    calcDate = calcDate * 100 + day; 
    System.out.println("int: " + calcDate); 
+1

這個問題已經解決了,並且因爲它已經超過2年了,似乎沒有理由添加其他答案。 – mbomb007

+0

此答案不解決問題。這個問題問如何將數字合併到「int」而不是「long」中。 [正確的接受答案](http://stackoverflow.com/a/12067775/642706)解釋說這是不可能的。 –

+0

感謝您的幫助 – user49557

0

TL;博士

Instant.now().getEpochSecond() // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z. 

詳細

正如其他人指出,一個32-bit整數無法容納數量足夠大從時代(1970年初以UTC開始)和現在的秒數。您需要64-bit整數(一個long基元或Long對象)。

java.time

其他的答案是使用舊的遺留日期時間類。它們已被java.time類取代。

Instant類表示UTC中的時間軸上的一個時刻,分辨率爲nanoseconds

Instant now = instant.now() ; 

您可以詢問自紀元以來的毫秒數。小心這意味着數據的丟失,將納秒縮短到毫秒。

long millisecondsSinceEpoch = now.toEpochMilli() ; 

如果你想紀元以來納秒的數量,你需要做一點數學作爲類奇怪缺乏toEpochNano方法。請注意0​​附加到十億以激發計算爲64位長整數。由於曆元

long nanosecondsSinceEpoch = (instant.getEpochSecond() * 1_000_000_000L) + instant.getNano() ; 

整秒但問題的末端要求一個10位數字。我懷疑這意味着自1970-01-01T00:00:00這個時代以來整個整個秒的計數。這通常被稱爲Unix Time或Posix Time。

我們可以詢問Instant這個數字。同樣,這意味着數據的丟失和截斷該對象可能容納的任何小數部分。

long secondsSinceEpoch = now.getEpochSecond() ; // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z. 

相關問題