将日期时间转换为其他时区 [英] Convert datetime to a different time zone

查看:99
本文介绍了将日期时间转换为其他时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从外部组件获取时间和时间的时区. 时间格式为"MMM dd yyyy hh:mma".例如:2014年10月31日下午12:54,此时间的时区是单独收到的.

I am getting time and the timezone of the time separately from a external component. The time is in the format 'MMM dd yyyy hh:mma'. Ex: Oct 31 2014 12:54PM and the timezone of this time is received separately.

在保存到数据库之前,我需要将此时间标准化为EST时区.我尝试了Calendar(我不认为这可以使用java Date来完成),但是与输出存在很大的不一致,然后经过一番谷歌搜索后,认为Joda会提供更好的支持,并改用了它.但是我遇到了以下异常.

I need to normalize this time to EST timezone before saving in the database. I tried Calendar(I don't think this can be done with java Date), but there were considerable inconsistencies with the output, and then after some googling, thought Joda had better support and switched to it. But I am getting the below exception.

    String clientTimeZone = "America/New_York";
    String value = "Nov 25 2014  2:18PM";
     DateTimeFormatter df = DateTimeFormat.forPattern("MMM dd yyyy hh:mma");//.withLocale(Locale.ENGLISH);
            DateTime temp = df.withZone(DateTimeZone.forID(clientTimeZone)).withOffsetParsed().parseDateTime(value);
            DateTime date = temp.withZone(DateTimeZone.forID("America/New_York"));
            Timestamp ts = new Timestamp(date.getMillis());
            Date d = new Date(ts.getTime());
            System.out.println(d.toString());

我得到了:

java.lang.IllegalArgumentException:无效格式:"2014年11月25日2:18 PM"在"2:18 PM"时格式错误 在org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:899) 在LearnJoda.main(LearnJoda.java:34)

java.lang.IllegalArgumentException: Invalid format: "Nov 25 2014 2:18PM" is malformed at " 2:18PM" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:899) at LearnJoda.main(LearnJoda.java:34)

关于可能是什么问题的任何想法?

Any idea on what could be the issue?

推荐答案

编辑:从OP获得更多信息后,答案已略有更改.

after more info from the OP the answer has been slightly changed.

您可以使用以下模式(您定义的).

You can use the following pattern (that you defined).

"MMM dd yyyy hh:mma"

但是,由于您输入的字符串取决于2位数小时还是1位数小时,因此我建议您使用String.replace方法对值进行预处理(如本answer ).

But, since your input strings varies depending on if it is a 2 digit hour or a one digit hour I suggest that you pre-process the values by using the String.replace method (as suggested in this answer).

@Test
public void test() throws JsonProcessingException {
    final String value1 = "Nov 24 2014  2:40PM"; // Double space
    final String value2 = "Nov 24 2014 11:40PM"; // Single space

    // Set up a test time zone
    final ZoneId clientTimeZone = ZoneId.systemDefault();

    // Create the formatter (as previously)
    DateTimeFormatter df = DateTimeFormat.forPattern("MMM dd yyyy hh:mma");


    DateTime temp1 = 
        df.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(clientTimeZone)))
               .withOffsetParsed()
               .parseDateTime(value1.replace("  ", " ")); // replace double space with single space

    DateTime temp2 = 
        df.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(clientTimeZone)))
                .withOffsetParsed()
                .parseDateTime(value2.replace("  ", " ")); // always replace to be sre

    DateTime date1 = temp1.toDateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York")));
    DateTime date2 = temp2.toDateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York")));
    Timestamp ts1 = new Timestamp(date1.getMillis());
    Timestamp ts2 = new Timestamp(date2.getMillis());
}

如果您使用的是Java 8,也可以跳过Joda,而使用java.time软件包中的 new Java 8时间和日期函数.例如

If you are using Java 8 it is also a possibility to skip Joda and instead use the new Java 8 time and date functions from the java.time packages. E.g.

DateTimeFormatter df = DateTimeFormatter.ofPattern("MMM dd yyyy h:ma");

这篇关于将日期时间转换为其他时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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