Convert datetime to a different time zone

孤街浪徒 提交于 2019-12-11 20:27:08

问题


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.

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());

I got:

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?


回答1:


Edit: 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"

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());
}

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");


来源:https://stackoverflow.com/questions/27691073/convert-datetime-to-a-different-time-zone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!