java开发去迪拜,我想使用Java 8 Date Time API从亚洲/加尔各答的时间计算亚洲/迪拜时区的时间...

ZoneId dubai = ZoneId.of("Asia/Dubai");

LocalDate localDate = LocalDate.now();

LocalTime localTime = LocalTime.now();

ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime, dubai);

System.out.println("Dubai Tiime:"+zonedDateTime);

Above code is still printing the time of my current zone (i.e Asia/Kolkata)

Also i tried the following code to achieve the same but it is also printing time in my current zone(Asia/Kolkata):

ZoneOffset offset = ZoneOffset.of("+04:00");

LocalDateTime localDateTime = LocalDateTime.now();

OffsetDateTime plusFour = OffsetDateTime.of(localDateTime, offset);

System.out.println("Dubai Time :"+plusFour);

I am unable to figure out why its not providing desired result.

解决方案

The answer by Kokorin is correct. Here's a bit more discussion.

Problems

When you called the now method and passed no arguments, you failed to specify a time zone. In that omission, java.time silently applied your JVM’s current default time zone in determining the current local time and current local date.

You claim your JVM’s current default time zone is Asia/Kolkata (India time). If when you ran that code it was 15:30 time in your office, your code is saying “let's take my 15:30 and use that as input to represent a wall-clock time in Dubai”. So while the current moment in Dubai was actually 14:00 (an hour and half closer to UTC than India I presume, not sure), you created a date-time for an hour and a half in the Dubai’s future: 15:30.

When you passed dubai in the line ZonedDateTime.of( localDate, localTime, dubai ) you assumed you were asking for an adjustment between time zones. But in fact you were assigning a time zone to a plain (“Local”) date and time that had no time zone at all. All three of the Local… classes store no time zone internally; their very purpose is to ignore time zone. Your code did not match your intentions.

Note how in this revision to your code I pass your ZoneId object to both now methods. This would solve your problem.

ZoneId dubai = ZoneId.of ( "Asia/Dubai" );

LocalDate localDate = LocalDate.now ( dubai );

LocalTime localTime = LocalTime.now ( dubai ); // Capturing `14:00` in Dubai rather than than `15:30` in India as in your version of code.

ZonedDateTime zonedDateTime = ZonedDateTime.of ( localDate , localTime , dubai );

System.out.println ( "Dubai Tiime:" + zonedDateTime );

But this is still bad code. If those pair of .now methods were called over the stroke of midnight, you would have very wrong information (off by about 24 hours).

Solutions

Instead you should capture the current moment atomically. Either user Kokorin's code, or use my code shown next.

An Instant is a moment on the timeline in UTC with a resolution of nanoseconds.

Instant instant = Instant.now();

ZoneId zoneId_Dubai = ZoneId.of( "Asia/Dubai" );

ZonedDateTime zdt_Dubai = ZonedDateTime.ofInstant( instant , zoneId_Dubai );

As a shortcut, call the static method ZonedDateTime.now.

ZonedDateTime zdt_Dubai = ZonedDateTime.now( zoneId_Dubai );

To see the same moment but with your own wall-clock time, adjust into India time.

ZonedDateTime zdt_Kolkata = zdt_Dubai.withZoneSameInstant( ZoneId.of( "Asia/Kolkata" ) );

BIG TIP: Always pass the optional time zone arguments. While I tremendously respect the work that went into java.time, I consider making the time zone arguments optional on various methods to be a design flaw. The silent implicit application of your JVM’s current default time zone is just too easy a trap to fall into for so many programmers. By the way, ditto for Locale, always specify.

Another Tip: Think, work, and store in UTC. As a programmer you must learn to think in UTC, get your head out of “my time in Kolkata” and “their time in Dubai”. You will drive yourself crazy and make your brain hurt. While programming, know that the only one true time is UTC. All the other Dubai/Kolkata/Montréal/Auckland times are smoke and mirrors, mere illusions. Use the Instant class in much of your code, make it your “go to” class when doing date-time work (only apply a time zone for display to the user). Use UTC in your database. Do your logging in UTC. Keep your servers on UTC (or Iceland) time zone. Use UTC when serializing date-time values to storage or in data-exchange (and use ISO 8601 formats btw). Keep a clock on your desk or screen displaying UTC. Later, when you go home from work, then you can slip back into your own local "India time" thinking.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值