2

I have a string in the form MMM/dd/yyyy, ie. May/21/2010. Now I want to convert it to yyyyMMdd, ie. 20100521.

My code is:

public static void main(String[] args) {        
        ArrayList<String> dates = new ArrayList<String>();
        dates.add("Jan/13/2011");
        dates.add("Feb/03/2001");
        dates.add("Mar/19/2012");
        dates.add("Apr/20/2011");
        dates.add("May/21/2010");
        dates.add("Jun/23/2008");
        dates.add("Jul/12/2009");
        dates.add("Aug/14/2010");
        dates.add("Sep/01/2011");
        dates.add("Oct/07/2010");
        dates.add("Nov/05/2011");
        dates.add("Dec/30/2011");

        for(String s : dates) {
            System.out.println(transformPrevDate(s));
        }
    }

And the method to transform:

public String transformPrevDate(String datoe) {
        String[] splitter = datoe.split("/");
        String m = splitter[0].toUpperCase();
        String d = splitter[1];
        String y = splitter[2];

        DateFormat formatter = new SimpleDateFormat("MMM");
        DateFormat formatter2 = new SimpleDateFormat("MM");

        try {
            Date date = formatter.parse(m);
            m = formatter2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String date = y + m + d;

        return date;
    }

The problem is that I get an Unparseable date exception, on May and Oct. I'm from Denmark and if I change it to danish "Maj" and "Okt" it succeeds. So what am I doing wrong here?

0

6 Answers 6

4

Your transformDate method can be much simpler written like this:

DateFormat input = new SimpleDateFormat("MMM/dd/yyyy", Locale.ENGLISH);
DateFormat output = new SimpleDateFormat("yyyyMMdd");
public String transformPrevDate(String datoe) throws ParseException {
    return output.format(input.parse(datoe));
}

You don't need to do your parsing yourself.

Sign up to request clarification or add additional context in comments.

1 Comment

Just FYI... you will still need to wrap it in try-catch block or have the API to throw ParseException because input.parse(datoe) throws ParseException, which is a checked exception.
2

Use SimpleDateFormat(String pattern, Locale locale) to add Locale to your date parsing (for english, use Locale.ENGLISH).

Better solution:

public String transformPrevDate(String datoe) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMM/dd/yyyy", Locale.ENGLISH);
    SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd");

    try {
        return dateFormat2.format(dateFormat.parse(datoe));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Comments

2

You will need to apply the locale on SimpleDateFormat.

Here's a more shorter version:-

public String transformPrevDate(String date) {
    DateFormat oldFormat = new SimpleDateFormat("MMM/dd/yyyy", Locale.ENGLISH);
    DateFormat newFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);

    String formattedDate = "";

    try {
        formattedDate = newFormat.format(oldFormat.parse(date));
    }
    catch (ParseException e) {
        e.printStackTrace();
    }

    return formattedDate;
}

@Test
public void testTransformPrevDate() {
    assertEquals("20110113", transformPrevDate("Jan/13/2011"));
    assertEquals("20010203", transformPrevDate("Feb/03/2001"));
}

Comments

1

use SimpleDateFormat(String pattern, Locale locale);

Comments

1

SimpleDateFormat is locale-dependent, and it's using your own locale by default. If you would like to use an English-based locale, you can create it by passing in a Locale when you create your SimpleDateFormat.

So to use a US-based locale, change your SimpleDateFormat initialization to:

    DateFormat formatter = new SimpleDateFormat("MMM", Locale.US);
    DateFormat formatter2 = new SimpleDateFormat("MM", Locale.US);

Comments

0

SimpleDateFormat uses your locale - so your computer is probably set to use Danish by default. Specify the English Locale explicitly:

DateFormat formatter = new SimpleDateFormat("MMM", Locale.ENGLISH);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.