日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android java标准时间_关于android:Java中格林尼治标准时间的毫秒数

發布時間:2023/12/16 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android java标准时间_关于android:Java中格林尼治标准时间的毫秒数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我需要隱瞞格林威治標準時間(在Android應用中)的毫秒數,例如:

1372916493000

當我通過此代碼將其轉換時:

Calendar cal = Calendar.getInstance();

cal.setTimeZone(TimeZone.getTimeZone("GMT"));

cal.setTimeInMillis(millis);

Date date = cal.getTime();

結果為07:41 07/04/2013。 當我只使用以下結果時:

Date date = new Date(millis);

不幸的是,結果看起來不正確,看起來像我的當地時間。 我嘗試通過此服務轉換相同的數字,結果是05:41 07/04/2013,我認為這是正確的。 所以我有兩個小時的差異。 有人有任何建議/提示我的轉換有什么問題嗎?

因此,要清楚一點,您要使用GMT時區將時間戳打印為字符串嗎?

嘗試查看以下答案:[如何將本地日期轉換為GMT] [1] [1]:stackoverflow.com/questions/10599109/

您是否考慮了夏令時?

看到這里stackoverflow.com/questions/230126/

您提供的毫秒代表您的本地時區還是GMT時區?

僅供參考,麻煩的舊日期時間類(例如java.util.Date,java.util.Calendar和java.text.SimpleDateFormat)現在已被遺留,由java.time類取代。 在ThreeTen-Backport項目中,許多java.time功能都被反向移植到Java 6和Java 7。 在ThreeTenABP項目中進一步適用于早期的Android。 請參閱如何使用ThreeTenABP…。

如果結果看起來不正確,則表示System.out.println(date),這并不奇怪,因為Date.toString將日期轉換為本地時區中的字符串表示形式。要查看格林尼治標準時間的結果,您可以使用此

SimpleDateFormat df = new SimpleDateFormat("hh:ss MM/dd/yyyy");

df.setTimeZone(TimeZone.getTimeZone("GMT"));

String result = df.format(millis);

這段代碼對我不起作用..顯示IlligalArgumentException

TL;博士

Instant.ofEpochMilli( 1_372_916_493_000L ) ? ? ? ? // Moment on timeline in UTC.

2013-07-04T05:41:33Z

…和…

Instant.ofEpochMilli( 1_372_916_493_000L ) ? ? ? ? ?// Moment on timeline in UTC.

.atZone( ZoneId.of("Europe/Berlin" ) ) ?// Same moment, different wall-clock time, as used by people in this region of Germany.

2013-07-04T07:41:33+02:00[Europe/Berlin]

細節

您正在使用麻煩的舊日期時間類,而現在卻被java.time類所取代。

java.time

如果自1970年1月的UTC 1970年第一時刻的紀元參考日期起算的毫秒數,則將其解析為Instant。 Instant類表示UTC時間軸上的時刻,其分辨率為納秒(最多十進制的九(9)位數字)。

Instant instant = Instant.ofEpochMilli( 1_372_916_493_000L ) ;

instant.toString(): 2013-07-04T05:41:33Z

要通過特定區域的掛鐘時間通過鏡頭看到相同的瞬間,請應用時區(ZoneId)以獲得ZonedDateTime。

ZoneId z = ZoneId.of("Europe/Berlin" ) ;

ZonedDateTime zdt = instant.atZone( z ) ;

zdt.toString(): 2013-07-04T07:41:33+02:00[Europe/Berlin]

關于java.time

java.time框架內置于Java 8及更高版本中。這些類取代了麻煩的舊舊式日期時間類,例如java.util.Date,Calendar和SimpleDateFormat。

現在處于維護模式的Joda-Time項目建議遷移到java.time類。

要了解更多信息,請參見Oracle教程。并在Stack Overflow中搜索許多示例和說明。規格為JSR 310。

在哪里獲取java.time類?

Java SE 8,Java SE 9和更高版本

內置。

標準Java API的一部分,具有捆綁的實現。

Java 9添加了一些次要功能和修復。

Java SE 6和Java SE 7

java.time的許多功能在ThreeTen-Backport中都被反向移植到Java 6和7。

Android的

更高版本的Android捆綁了java.time類的實現。

對于較早的Android,ThreeTenABP項目改編了ThreeTen-Backport(如上所述)。請參閱如何使用ThreeTenABP…。

在轉換過程中,您似乎被自己的時區和UTC時區弄糊涂了。

假設您在倫敦(當前倫敦比格林尼治標準時間早1個小時),而毫秒數就是您所在時區(本例中為倫敦)的時間。

然后,您可能應該:

Calendar cal = Calendar.getInstance();

// Via this, you're setting the timezone for the time you're planning to do the conversion

cal.setTimeZone(TimeZone.getTimeZone("Europe/London"));

cal.setTimeInMillis(1372916493000L);

// The date is in your home timezone (London, in this case)

Date date = cal.getTime();

TimeZone destTz = TimeZone.getTimeZone("GMT");

// Best practice is to set Locale in case of messing up the date display

SimpleDateFormat destFormat = new SimpleDateFormat("HH:mm MM/dd/yyyy", Locale.US);

destFormat.setTimeZone(destTz);

// Then we do the conversion to convert the date you provided in milliseconds to the GMT timezone

String convertResult = destFormat.parse(date);

請讓我知道我是否正確理解了您的觀點?

干杯

嘗試這個

public class Test{

public static void main(String[] args) throws IOException {

Test test=new Test();

Date fromDate = Calendar.getInstance().getTime();

System.out.println("UTC Time -"+fromDate);

System.out.println("GMT Time -"+test.cvtToGmt(fromDate));

}

private ?Date cvtToGmt( Date date )

{

TimeZone tz = TimeZone.getDefault();

Date ret = new Date( date.getTime() - tz.getRawOffset() );

// if we are now in DST, back off by the delta. ?Note that we are checking the GMT date, this is the KEY.

if ( tz.inDaylightTime( ret ))

{

Date dstDate = new Date( ret.getTime() - tz.getDSTSavings() );

// check to make sure we have not crossed back into standard time

// this happens when we are on the cusp of DST (7pm the day before the change for PDT)

if ( tz.inDaylightTime( dstDate ))

{

ret = dstDate;

}

}

return ret;

}

}

測試結果:

UTC時間-5月15日星期二16:24:14 IST 2012

GMT時間-2012年5月15日星期二10:54:14

Date date = cal.getTime();

返回通過創建的日期

public final Date getTime() {

return new Date(getTimeInMillis());

}

其中getTimeInMillis()返回毫秒,不帶任何TimeZone。

我建議在這里尋找如何做你想要的如何使用Java處理日歷時區

但是,是以毫秒為單位的時間是根據GMT還是設備/臺式機所在的時區生成的?

總結

以上是生活随笔為你收集整理的android java标准时间_关于android:Java中格林尼治标准时间的毫秒数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。