从设备获取当前时间毫秒并将其转换为具有不同时区的新日期 [英] Getting the current time millis from device and converting it into a new date with different timezone

查看:30
本文介绍了从设备获取当前时间毫秒并将其转换为具有不同时区的新日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我面临的问题是 -

首先,我创建了一个日期对象,该对象将为我提供带有设备时区的当前日期和时间,即

Date date = new Date();//假设时区是印度 - GMT (+05:30)日期的值为 = "Mon Sep 24 13:54:06 GMT+05:30 2018"

不,我有一个日期格式化程序,我使用它转换了以下日期对象.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z");sdf.setTimeZone(TimeZone.getTimeZone(loadPreferences(Utility.TIMEZONE_NAME)));//这里的时区是夏威夷 (GMT-10:00)

现在根据新时区(即夏威夷)获取时间

String dateS = sdf.format(date);//这将为您提供新时区的日期 - 2018/09/23 22:24:06 GMT-10:00"

现在将此字符串日期转换为日期对象 -

Date newDate = sdf.parse(dateS);

现在我得到的新日期与我过去的时区不同.

我得到的 newDate 的值是 = "Mon Sep 24 13:54:06 GMT+05:30 2018"//这是设备时区,而不是我设置的时区.

我已经在日期格式化程序中尝试过Z"、z"、X"、ZZ"、ZZZZZ",但仍然没有运气.

如果你们有任何想法阅读本文,请告诉我.

解决方案

两条消息:

  • 您的期望是错误的.Date 没有时区,它不能有.因此,无论您如何编写代码,都无法使用 DateSimpleDateFormat 获得什么.
  • DateSimpleDateFormatTimeZone 类早已过时且设计不佳.它们的现代替代品是 java.time,即 2014 年推出的日期和时间 API.

分区日期时间

现代 ZonedDateTime 有一个时区,顾名思义:

 DateTimeFormatter 格式化程序= DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss z", Locale.US);ZonedDateTime nowInHawaii = ZonedDateTime.now(ZoneId.of("Pacific/Honolulu"));String dateS = nowInHawaii.format(formatter);System.out.println(dateS);

这个片段的输出是:

<块引用>

2018/09/24 18:43:19 夏令时

如果您想要输出中的偏移量,请更改格式化程序:

 DateTimeFormatter 格式化程序= DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss OOOO", Locale.US);

<块引用>

2018/09/24 18:45:53 GMT-10:00

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

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

  • 在 Java 8 及更高版本以及新的 Android 设备(我听说从 API 级别 26 开始)中,现代 API 是内置的.
  • 在 Java 6 和 7 中获得 ThreeTen Backport,现代类的后向移植(ThreeTen for JSR 310,现代 API 首次被描述).
  • 在(旧版)Android 上,使用 ThreeTen Backport 的 Android 版本.它被称为 ThreeTenABP.确保从包 org.threeten.bp 和子包中导入日期和时间类.

链接

Here the that problem that i'm facing is -

First i have created a date object which will give me current date and time with device timezone i.e

Date date = new Date(); // Let say the time zone is India - GMT (+05:30)
The value of date is = "Mon Sep 24 13:54:06 GMT+05:30 2018"

No i have a Date formatter using which i have converted the following date object.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone(loadPreferences(Utility.TIMEZONE_NAME)));
// Here the timezone is Hawaii (GMT-10:00)

Now getting the time as per the new time zone i.e., Hawaii

String dateS = sdf.format(date); 
// This will give you the date with new timezone - "2018/09/23 22:24:06 GMT-10:00"

Now converting this string date to date object as -

Date newDate = sdf.parse(dateS);

Now the new date which i'm getting is not as per the timezone which i have passed.

The value of newDate which i'm getting is = "Mon Sep 24 13:54:06 GMT+05:30 2018" 
//This is device timezone not the one i have set.

I have already tried "Z", "z", "X", "ZZ", "ZZZZZ" in the date formatter still no luck.

If any of you have any idea reading this then let me know.

解决方案

Two messages:

  • Your expectations are wrong. A Date hasn’t got a time zone, it cannot have. So what you are trying to obtain is impossible using Date and SimpleDateFormat no matter how you write the code.
  • The classes Date, SimpleDateFormat and TimeZone are long outdated and poorly designed. Their modern replacements are in java.time, the date and time API introduced in 2014.

ZonedDateTime

A modern ZonedDateTime has a time zone as the name says:

    DateTimeFormatter formatter 
            = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss z", Locale.US);
    ZonedDateTime nowInHawaii = ZonedDateTime.now(ZoneId.of("Pacific/Honolulu"));
    String dateS = nowInHawaii.format(formatter);
    System.out.println(dateS);

Output from this snippet was:

2018/09/24 18:43:19 HST

If you want the offset in the output, change the formatter thusly:

    DateTimeFormatter formatter 
            = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss OOOO", Locale.US);

2018/09/24 18:45:53 GMT-10:00

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.

  • In Java 8 and later and on new 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 modern classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

这篇关于从设备获取当前时间毫秒并将其转换为具有不同时区的新日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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