0

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.

Anindya Chatterjee
  • 5,824
  • 13
  • 58
  • 82
  • If the Java code does stupid things like double conversion, it *should* be changed. Stop gap measures are not going to be of help (and personally I feel tired when people ask these kind of questions here because they are only of help to themselves). – Maarten Bodewes Dec 29 '11 at 16:57
  • I agree with you fully. But you know in big IT projects with people of different skills, things like this happens. But I was looking for a workaround for current release in production. Ofcourse this code will get fixed and re-factored in next release. – Anindya Chatterjee Dec 29 '11 at 19:16

2 Answers2

0

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.

Community
  • 1
  • 1
javamonkey79
  • 17,443
  • 36
  • 114
  • 172
0

The date format symbols for en_US are:

GyMdkHmsSEDFwWahKzZ

The date format symbols for nl_NL are:

GyMdkHmsSEDFwWahKzZ

There is no difference. I got the date format symbols by executing the following Java lines:

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

I left the source date format the same:

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

and modified your simple date format statement to this:

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

With this simple date format change, your test code ran fine and produced the following results in the US Eastern time zone:

Date string: 2011-12-20T00:00:00+00:00
Final Date:  12/19/2011 19:00:00
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111