UTC-N到当地时间

时间:2015-05-15 03:35:09

标签: java datetime jodatime utc

使用joda,如何格式化UTC +/- n时间,将“挂起时间”显示给用户:

从(UTC +/- n):

2015-05-15T03:28:49.523-04:00

到(EST)Wall:

2015-05-14 23:22:44

更新(1)

请考虑以下代码。我们需要使用时间戳 用于在UTC中与DB进行写入。考虑到这一点:

DateTimeZone.setDefault(DateTimeZone.UTC);
LocalDateTime utcDate = new LocalDateTime();
DateTimeZone utcTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("ETC/UTC"));
DateTimeZone localTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Montreal"));      
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");        
fmt.withZone(localTZ);

DateTime localDateTime = utcDate.toDateTime(localTZ);
DateTime utcDateTime = localDateTime.toDateTime(utcTZ);

Timestamp u = new Timestamp(utcDateTime.getMillis());

System.out.println("UTC Time: " + u);

LocalDateTime date = new LocalDateTime(u);
DateTime srcDateTime = date.toDateTime(utcTZ);
DateTime dstDateTime = srcDateTime.toDateTime(localTZ);

System.out.println("UTC+/- Time: " + dstDateTime.toString());

DateTime dateTimeInTargetTimezone = dstDateTime.withZone(localTZ);
System.out.println("Wall Time: " + dateTimeInTargetTimezone.toString("yyy-MM-dd HH:mm:ss"));    

现在,当我们需要从Timestamp对象中的DB中提取UTC时间时 在他们的TZ中,在“墙/葬时”中显示最终用户的时间,无论你想要什么叫它。

输出

UTC Time: 2015-05-15 20:03:47.561 "Good"
UTC+/- Time: 2015-05-15T20:03:47.561-04:00 "Good"
Wall Time: 2015-05-15 20:03:47 "No! No! No! Danger! We'll be late!"

名称中有什么!我需要做的是让dstDateTime等于我在墙上看到的时间(即2015-05-15 4:03:47)。

更新(2)

摆脱时间戳:

DateTimeZone utcTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("ETC/UTC"));
DateTimeZone localTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Montreal"));
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyy-MM-dd HH:mm:ss");

LocalDateTime utcDate = new LocalDateTime(utcTZ);
DateTime utcDateTime = utcDate.toDateTime(utcTZ);       

System.out.println("UTC Time: " + utcDateTime);

DateTime dstDateTime = utcDateTime.toDateTime(localTZ);

System.out.println("Unformated Wall Time: " + dstDateTime);
System.out.println("Wall Time: " + dstDateTime.toString(fmt));

输出

UTC Time: 2015-05-20T14:09:28.469Z
Unformated Wall Time: 2015-05-20T10:09:28.469-04:00
Wall Time: 2015-05-20 10:09:28

当我尝试将UTZ日期改为DB时,一切看起来都很完美, 我需要转换为Timestamp(即新的Timestamp(o.getOrderDate()。getMillis())),它显然将当地时间权限归功于DB,而不是我需要的UTC Zulu时间。

先谢谢,

尼克。

2 个答案:

答案 0 :(得分:0)

@Nick

我不确定我是否理解你的问题,但你可以试试这个:

    //sample input
    String timestamp = "2015-05-15T03:28:49.523-04:00";

    //format to parse
    DateTimeFormatter dateTimeF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    //parse to local date time
    LocalDateTime dateTime = dateTimeF.parseLocalDateTime(timestamp).minusHours(4);
    //output here minus 4 hours
    System.out.println(dateTime);

您可以通过上面的示例代码改进您的实现,并使偏移时间更加动态,但到目前为止,此代码提供了您在示例中提到的结果。

古德勒克!

答案 1 :(得分:0)

只需使用DateTime.withZone方法更改时区:

@Test
public void change_timezone() {
    String input = "2015-05-15T03:28:49.523-04:00";
    DateTimeZone targetTimeZone = DateTimeZone.forID("Europe/London");
    DateTime dateTime = DateTime.parse(input);
    DateTime dateTimeInTargetTimezone = dateTime.withZone(targetTimeZone);
    assertThat(dateTimeInTargetTimezone.toString("yyy-MM-dd HH:mm:ss"), 
               equalTo("2015-05-15 08:28:49"));
}
相关问题