nl_NL语言环境中的日期时间模式是什么?

时间:2011-12-29 16:19:25

标签: java unix datetime locale

在我的项目中,我使用了如下的日期转换(为简洁起见,我只采用了相关的块)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;


public class FormatTest {

    public static void main(String[] args) {
        try {
            String destinationDateFormat = "MM/dd/yyyy HH:mm:ss";
            String sourceDateFormat = "EEE MMM d HH:mm:ss z yyyy";
            String dateString = "2011-12-20T00:00:00+00:00";

            DatatypeFactory factory = DatatypeFactory.newInstance();

            XMLGregorianCalendar cal = factory.newXMLGregorianCalendar(dateString);
            Calendar gCal = cal.toGregorianCalendar();
            Date convertedDate = gCal.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat(sourceDateFormat);
            if (convertedDate != null) {
                String convertedDateString = new SimpleDateFormat(destinationDateFormat).format(sdf.parse(
                            convertedDate.toString()).getTime());

                System.out.println("Final Date :" + convertedDateString);
            }       

        } catch (DatatypeConfigurationException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }



    }

}

在我的项目中,正在从spring属性文件中读取变量destinationDateFormatsourceDateFormat。上面的代码在unix框中工作正常,其中系统区域设置设置为 en_US en_GB ,但其中一个测试框具有 nl_NL 区域设置和这就是上面的代码没有给出ParseException的地方。问题就像sourceDateFormat在nl_NL语言环境中无法解析。

有人可以建议我 nl_NL 区域设置中应该有相应的sourceDateFormat吗?

我不想更改java代码,因为它代价很高。

2 个答案:

答案 0 :(得分:0)

看起来可能是这样:EEEE, MMMM d, yyyy h:mm:ss a z

我写了一个小班来得到它:

    DateFormat f = getDateTimeInstance(FULL, FULL, new Locale("nl_NL"));
    SimpleDateFormat sf = (SimpleDateFormat) f;
    String p1 = sf.toPattern();
    String p2 = sf.toLocalizedPattern();

    System.out.println( p1 );
    System.out.println( p2 );

派生自this SO answer

答案 1 :(得分:0)

en_US的日期格式符号为:

GyMdkHmsSEDFwWahKzZ

nl_NL的日期格式符号为:

GyMdkHmsSEDFwWahKzZ

没有区别。我通过执行以下Java行获得了日期格式符号:

System.out.println(DateFormatSymbols.getInstance
        (new Locale("en_US")).getLocalPatternChars());
System.out.println(DateFormatSymbols.getInstance
        (new Locale("nl_NL")).getLocalPatternChars());

我保留了源日期格式:

String sourceDateFormat = "EEE MMM d HH:mm:ss z yyyy";

并将简单的日期格式语句修改为:

SimpleDateFormat sdf = new SimpleDateFormat(sourceDateFormat,
        new Locale("nl_NL"));

通过这种简单的日期格式更改,您的测试代码运行正常并在美国东部时区产生以下结果:

Date string: 2011-12-20T00:00:00+00:00
Final Date:  12/19/2011 19:00:00