简体   繁体   English

如何在Java中使用默认的GMT小时创建日历

[英]How to create a calendar with the default GMT hours in java

I'm getting a long from a server that I have to parse into a date. 我要从服务器解析很长时间,必须将其解析为一个日期。 I'm using a calendar to do so. 我正在使用日历。

Thing is that the long came transformed from the server (it have the user local time), but I get it as a default GMT and I also transform it into local time. 事情是从服务器转换过来的(它具有用户本地时间),但是我将其作为默认的GMT并将其也转换为本地时间。

So, it transforms twice. 因此,它转换了两次。 Since I get it right, how can I show it without changing it to local (seems to do it by default)? 既然我做对了,如何在不将其更改为本地的情况下显示它(默认情况下似乎要这样做)? My code: 我的代码:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));           
calendar.setTimeInMillis(dateLong);
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format1.format(cal.getTime());

Instead of using Calendar use SimpleDateFormat . 代替使用Calendar使用SimpleDateFormat the following code shows me the correct results. 以下代码向我显示了正确的结果。

    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    String result = df.format(dateLong);
    System.out.println(result);

The other answers already provide solutions with Calendar and SimpleDateFormat . 其他答案已经提供了CalendarSimpleDateFormat解决方案。 I'd just like to add another approach. 我只想添加另一种方法。

The old classes ( Date , Calendar and SimpleDateFormat ) have lots of problems and design issues , and they're being replaced by the new APIs. 旧的类( DateCalendarSimpleDateFormat )存在很多问题设计问题 ,并且已被新的API取代。

In Android (if you're ok about adding a dependency to your project - and in this case it's totally worth it, IMO), you can use the ThreeTen Backport , a great backport for Java 8's new date/time classes. 在Android中(如果可以将依赖项添加到项目中-在这种情况下,这是完全值得的,IMO),则可以使用ThreeTen Backport ,它是Java 8的新日期/时间类的绝佳反向端口 To make it work, you'll also need the ThreeTenABP (more on how to use it here ). 为了使其工作,您还需要ThreeTenABP (更多有关如何在此处使用它的信息 )。

First you can use a org.threeten.bp.Instant to convert the millis value to a corresponding UTC instant. 首先,您可以使用org.threeten.bp.Instantorg.threeten.bp.Instant值转换为相应的UTC时刻。 Then you use a org.threeten.bp.format.DateTimeFormatter to define the format you want the date. 然后,使用org.threeten.bp.format.DateTimeFormatter定义所需日期的格式。 I also use a org.threeten.bp.ZoneOffset to indicate that the formatter should use the date in UTC: 我还使用org.threeten.bp.ZoneOffset来指示格式化程序应使用UTC中的日期:

long dateLong = System.currentTimeMillis();
// convert long millis value to Instant
Instant instant = Instant.ofEpochMilli(dateLong);
// create formatter in UTC
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
    .withZone(ZoneOffset.UTC);
// format it
System.out.println(fmt.format(instant));

The output will be something like: 输出将类似于:

13/09/2017 11:28:02 13/09/2017 11:28:02

Sorry, I misunderstood I guess you have time in milliseconds. 抱歉,我误会了您的时间,以毫秒为单位。 Well, from there: 好吧,从那里:

Date dateCreated = new Date(timeInMilliseconds);

And then when you create the Calendar, just set Timezone after set time, because setTimeInMillis is overriding your previous TimeZone set when you are creating the instance 然后,当您创建日历时,只需在设置时间之后设置时区,因为在创建实例时setTimeInMillis会覆盖您之前的TimeZone设置

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(new java.util.Date().getTime());
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));

That's it 而已

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM