【问题标题】:Convert a local time to a specific country time with JodaTime使用 JodaTime 将本地时间转换为特定国家/地区时间
【发布时间】:2019-05-29 15:44:06
【问题描述】:

我在尝试使用 Joda 将一个人的当地时间转换为英国时间时遇到问题。 说, 31-01-2015 12:00:01am 华盛顿特区时间(可以是任何国家的时间)到

31-01-2015 5:00:01am 伦敦时间(伦敦时间应该始终是输出)

DateTimeZone zone = DateTimeZone.forID("Europe/London"); DateTime dt = new DateTime(zone);

似乎无法将其格式化为这种格式 日-月-年时:分:秒:a

【问题讨论】:

标签: java jodatime android-jodatime


【解决方案1】:

输入格式为华盛顿时间,输出为伦敦时间 您可以将多个区域 ID 作为输入和输出。 从herehere 获取时区ID

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
public class Test {

public static void main(String[] args) throws Exception {

    final DateTimeZone fromTimeZone = DateTimeZone.forID("EST");
    final DateTime dateTime = new DateTime("2019-01-03T01:25:00", fromTimeZone);

    final org.joda.time.format.DateTimeFormatter outputFormatter
            = DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ss a").withZone(DateTimeZone.forID("Europe/London"));
    System.out.println("London time:" + outputFormatter.print(dateTime));

}

输出:

伦敦时间:2019-01-03 06:25:00 AM

Ref

【讨论】:

  • 谢谢,但我需要从任何时区转换为伦敦时间。不只是从华盛顿特区的时间。像 uhm 一样,使用 DateTime() 或 Date() 获取当前时间,然后到伦敦时间。请在我可以从代码更改的地方添加更多 cmets。谢谢。
  • 你可以把zone id放在fromTimeZone变量中的“EST”位置,就这样。
  • 为了获得国家时区转到joda.org/joda-time/timezones.html
猜你喜欢
  • 2016-02-22
  • 1970-01-01
  • 2015-08-11
  • 2021-03-20
  • 2012-03-08
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多