9

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
amithgc
  • 6,167
  • 6
  • 29
  • 38

8 Answers8

35

Your fromFormat uses minutes where it should use months.

String fromFormat = "yyyy-MM-dd";
sje397
  • 41,293
  • 8
  • 87
  • 103
6

I think the fromFormat should be "yyyy-MM-dd".

Here is the format:

  • m == Minute in Hour
  • M == Month in Year

More: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Dan Hardiker
  • 3,013
  • 16
  • 19
2

Look at the javadoc of SimpleDateFormat and look at what the m represents. Not months as you think but minutes.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Robin
  • 36,233
  • 5
  • 47
  • 99
2
 String fromFormat = "yyyy-MM-dd"; 
kosa
  • 65,990
  • 13
  • 130
  • 167
2

From format should be:

String fromFormat = "yyyy-MM-dd"
Brent Worden
  • 10,624
  • 7
  • 52
  • 57
1

tl;dr

LocalDate.parse( "2011-12-15" )                            // Date-only, without time-of-day, without time zone.
.format(                                                   // Generate `String` representing value of this `LocalDate`. 
    DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )  // How long or abbreviated?
                     .withLocale(                          // Locale used in localizing the string being generated.
                         new Locale( "en" , "IN" )         // English language, India cultural norms.
                     )                                     // Returns a `DateTimeFormatter` object.
)                                                          // Returns a `String` object.

15 December 2011

java.time

While the accepted Answer is correct (uppercase MM for month), there is now a better approach. The troublesome old date-time classes are now legacy, supplanted by the java.time classes.

Your input string is in standard ISO 8601 format. So no need to specify a formatting pattern for parsing.

LocalDate ld = LocalDate.parse( "2011-12-15" );  // Parses standard ISO 8601 format by default.
Locale l = new Locale( "en" , "IN" ) ;  // English in India.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )
                                       .withLocale( l );
String output = ld.format( f );

Dump to console.

System.out.println( "ld.toString(): " + ld );
System.out.println( "output: " + output );

ld.toString(): 2011-12-15

output: 15 December 2011

See live code in IdeOne.com.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

m in SimpleDateFormat stands for minutes, while M stands for month. Thus your first format should be yyyy-MM-dd.

ffriend
  • 27,562
  • 13
  • 91
  • 132
  • One thing I like SO for is a that questions are answered so quickly. 3 new answers during 1 minute when I typed an answer, awesome! – ffriend Jan 07 '12 at 15:30
0

Well this may not be your case but may help someone. In my case after conversion, day of month and month set 1. So whatever date is, after conversion i get 1 jan which is wrong. After struggling i found that in date format i have used YYYY instead of yyyy. When i changed all caps Y to y it works fine.

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46