日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

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

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

1372916493000

當我通過此代碼將其轉(zhuǎn)換時:

Calendar cal = Calendar.getInstance();

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

cal.setTimeInMillis(millis);

Date date = cal.getTime();

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

Date date = new Date(millis);

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

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

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

您是否考慮了夏令時?

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

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

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

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

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]

細節(jié)

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

java.time

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

Instant instant = Instant.ofEpochMilli( 1_372_916_493_000L ) ;

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

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

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

ZonedDateTime zdt = instant.atZone( z ) ;

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

關(guān)于java.time

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

現(xiàn)在處于維護模式的Joda-Time項目建議遷移到j(luò)ava.time類。

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

在哪里獲取java.time類?

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

內(nèi)置。

標準Java API的一部分,具有捆綁的實現(xiàn)。

Java 9添加了一些次要功能和修復(fù)。

Java SE 6和Java SE 7

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

Android的

更高版本的Android捆綁了java.time類的實現(xiàn)。

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

在轉(zhuǎn)換過程中,您似乎被自己的時區(qū)和UTC時區(qū)弄糊涂了。

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

然后,您可能應(yīng)該:

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;

}

}

測試結(jié)果:

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

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

Date date = cal.getTime();

返回通過創(chuàng)建的日期

public final Date getTime() {

return new Date(getTimeInMillis());

}

其中g(shù)etTimeInMillis()返回毫秒,不帶任何TimeZone。

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

但是,是以毫秒為單位的時間是根據(jù)GMT還是設(shè)備/臺式機所在的時區(qū)生成的?

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。