简体   繁体   中英

Problem converting date format in Java

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?

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.

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

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

使用SimpleDateFormat(String pattern, Locale locale) ;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM