RFC822 Timezone Parsing in Java

时光怂恿深爱的人放手 提交于 2019-12-05 03:05:35
Alen
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

    public static Date ParseRFC3339DateFormat(String p_date)
    {
        try{
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
            String dts = p_date.replaceAll("([\\+\\-]\\d\\d):(\\d\\d)","$1$2");
            return formatter.parse(dts);
        }catch (Exception e) {
                return null;
        }
    }
}

This works

SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-DD'T'hh:mm:ssZ");
format.parse("2007-02-26T20:15:00+02:00".replaceAll("([\\+\\-]\\d\\d):(\\d\\d)","$1$2"));

(Note I've taking the final colon out of the format after the 'Z' format specifier.)

laz

RFC822 does not allow a colon to be in the time zone portion of date. It expects just the 4 digits. The name of that Dojo method indicates that it is using RFC3339. It seems that is practically the same format as ISO8601. It just so happens that Joda Time has ISODateTimeFormat which is ISO8601 compatible if you are able to use that library. The method dateTimeNoMillis() looks like a match with the Dojo format. It really is nicer than the standard Java date and time API. Otherwise highlycaffeinated's suggestion would be the way to go.

Updated in response to Jamen's update

Isn't there a way to have Dojo use a format that doesn't include the timezone? Then you can adjust the format on the Java side to match. I don't know much about Dojo and I haven't been able to find any documentation on the toRfc3339 function it provides.

In Java 8 you can use:

Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse( rfc822Time ) );

FYI: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#RFC_1123_DATE_TIME

I'd strip the ':' out of the timezone and use the format you have above.

Instead of using dojo.date.toRfc3339(jsDate) you could create your own function with a custom format string.

Something like the following would remove the colon and should be parsable by your java format.

function toRfc3339String(jsDate){
    return dojo.date.locale.format(jsDate,{selector: 'date', datePattern:'yyyy-MM-dd\'T\'hh:mm:ssZ'});
}
PNS

You can do this generically without Java 7. I have asked 2 questions in StackOverflow on this, within 2012, so there is a solution that does not need any third party libraries.

Just check the implementation presented in the description of my latest question, which also points to the earlier one that discusses exactly this issue.

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