Search code examples
javaunixdatetimelocale

What will be the date-time pattern in nl_NL locale of this?


In my project I am using a date conversion as follows (I have taken only the relevant chunk for brevity)

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



    }

}

In my project the variables destinationDateFormat and sourceDateFormat is being read from a spring properties file. The above code works fine in the unix boxes where system locale is set as en_US or en_GB, but one of the test boxes has nl_NL locale and that's where the above code is failing giving a ParseException. The problem is like sourceDateFormat is not parse-able in nl_NL locale.

Can anybody suggest me what should be the corresponding sourceDateFormat in nl_NL locale?

I don't want to the change the java code as it is costly.


Solution

  • It looks like this might be it: EEEE, MMMM d, yyyy h:mm:ss a z

    I wrote a small class to get it:

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

    Derived from this SO answer.