RFC822时区在Java中解析 [英] RFC822 Timezone Parsing in Java

查看:497
本文介绍了RFC822时区在Java中解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在被Dojo转换为RFC822格式的JS日期。函数调用
dojo.date.toRfc3339(jsDate)生成以下日期 - 2007-02-26T20:15:00 + 02:00。



我有一个应用程序使用Java日期SimpleDateFormat来解析上面生成的日期。由于时区,我无法解析此日期格式。我试图使用



yyyy-mm-DD'T'hh:mm:ssZ



因为时区的'Z'并不期待':'的字符。有没有人知道如何使用':'来指定处理RFC822日期的模式?



修订版:



感谢正确解释我正在尝试做什么:)我的意思是说日期是在RFC3339中生成的,我需要RFC822。看起来我将不得不重写JavaScript。我希望我不必这样做,并且可以指定日期格式模式,而不必修改任何Java代码,因为日期格式被简单地注入到应用程序的Spring bean中。



只是为了完整,有没有办法在日期格式表达式中指定忽略序列中的字符(不需要进行字符串操作/替换)?在这种情况下,我会说忽略任何':'或只是忽略时区在一起?

解决方案

 code> 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(([\\ + \\\--\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ,$ 1 $ 2);
return formatter.parse(dts);
} catch(Exception e){
return null;
}
}
}


I have a JS date that is being converted by Dojo into RFC822 format. The function call - dojo.date.toRfc3339(jsDate), generates the following date - 2007-02-26T20:15:00+02:00.

I have an application that uses a Java date SimpleDateFormat to parse in the dates generated above. I am having problems parsing this date format due to the timezone. I have attempted to use

yyyy-mm-DD'T'hh:mm:ssZ

This fails as the 'Z' for timezone doesn't expect a ':' character. Does anyone know how I would specify a pattern to handle a RFC822 date with the ':'?

revision:

Thanks for correctly interpreting what I am trying to do :) I was meant to say the date is generating in RFC3339 and I needed RFC822. Looks like I will have to override the JavaScript. I was hoping that I wouldn't have to do that and could specify a date format pattern without having to modify any Java Code as the date format is simply injected into a Spring bean of an application.

Just for completeness, is there a way to specify in a date format expression to ignore characters in the sequence (without doing String manipulation/replacement)? In this case I'd be saying ignore any ':' or just ignore the timezone all together?

解决方案

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

这篇关于RFC822时区在Java中解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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