Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java date format conversion - getting wrong month

Tags:

java

date

I have a problem in converting the date in java, don't know where i am going wrong...

    String dateStr = "2011-12-15";
    String fromFormat = "yyyy-mm-dd";
    String toFormat = "dd MMMM yyyy";

    try {
        DateFormat fromFormatter = new SimpleDateFormat(fromFormat);
        Date date = (Date) fromFormatter.parse(dateStr);

        DateFormat toformatter = new SimpleDateFormat(toFormat);
        String result = toformatter.format(date);

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

Input date is 2011-12-15 and I am expecting the result as "15 December 2011", but I get it as "15 January 2011"

where am I going wrong?

like image 582
amithgc Avatar asked Jan 07 '12 15:01

amithgc


People also ask

How do I change the date format from mm/dd/yyyy to mm/dd/yyyy in Java?

Format date with SimpleDateFormat('MM/dd/yy') in Java // displaying date Format f = new SimpleDateFormat("MM/dd/yy"); String strDate = f. format(new Date()); System. out. println("Current Date = "+strDate);


1 Answers

Your fromFormat uses minutes where it should use months.

String fromFormat = "yyyy-MM-dd";
like image 92
sje397 Avatar answered Sep 30 '22 14:09

sje397