从设备获取日期并将其转换为GMT + 4 [英] Get date from device and convert it to GMT+4

查看:133
本文介绍了从设备获取日期并将其转换为GMT + 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找android手机的时区.因为我想获取日期对象,但我想使用GMT + 4格式.我看到的所有其他答案都将转换来自API请求(已知时区)的时间.我该怎么做?

I am trying to find the timezone of the android phone. Because I want to get the date object but I want in GMT+4 format. Every other answer I saw converts the time coming from an API request(whose timezone is known). How can I accomplish this?

其他方法可能是将我的GMT + 4时间从服务器转换为设备的本地时间.

Other approach may be converting the GMT+4 time that comes to me from server to the local time of my device.

推荐答案

最好使用实时区域,而不仅仅是偏移+04.例如:

Best to use a real time zone rather than just an offset of +04. For example:

    OffsetDateTime currentDateTimeInGeorgia = OffsetDateTime.now(ZoneId.of("Asia/Tbilisi"));
    System.out.println(currentDateTimeInGeorgia);

此代码段仅输出:

2018-09-13T19:18:35.642592 + 04:00

2018-09-13T19:18:35.642592+04:00

请替换您自己的时区.如果您确实坚持使用GMT + 4,请使用 ZoneOffset.ofHours(4).

Please substitute your own time zone. If you do insist on GMT+4, use ZoneOffset.ofHours(4).

旧的 Date 类不能容纳偏移量或时区,但现代的 OffsetDateTime 可以容纳偏移量或时区. Date 类的设计也很差,因此请考虑使用现代Java日期和时间API java.time.

The old Date class cannot hold an offset or time zone, but the modern OffsetDateTime does, as the name says. The Date class is also poorly designed, so do consider using java.time, the modern Java date and time API.

如您所见,以上未使用设备时区.如果您仍然需要这样做:

As you see, the above doesn’t use the device time zone. If you need this anyway:

    ZoneId deviceTimeZone = ZoneId.systemDefault();
    System.out.println(deviceTimeZone);

示例输出:

亚洲/迪拜

问题:我可以在Android上使用java.time吗?

是的, java.time 在较新的Android设备上都能很好地工作.它只需要至少Java 6 .

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • 在Java 8和更高版本以及较新的Android设备(我听说API级别为26)上,内置了现代API.
  • 在Java 6和7中,获取ThreeTen反向端口,即新类的反向端口(JSR 310的ThreeTen;请参见底部的链接).
  • 在(较旧的)Android上,请使用Android版的ThreeTen Backport.称为ThreeTenABP.并确保使用子包从 org.threeten.bp 导入日期和时间类.
  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

这篇关于从设备获取日期并将其转换为GMT + 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆