Java date format conversion - getting the wrong month

I have a problem converting a 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(); } 

The input date is 2011-12-15, and I expect the result to be December 15, 2011, but I get it as January 15, 2011

where am i going wrong?

+7
source share
9 answers

Your fromFormat uses minutes where it should use months.

 String fromFormat = "yyyy-MM-dd"; 
+25
source

I think the format should be "yyyy-mm-dd."

Here is the format:

  • m == minute per hour
  • M == Month in a year

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

+5
source

Take a look at javadoc SimpleDateFormat and see what m represents. Not the months you think, but the minutes.

+2
source
  String fromFormat = "yyyy-MM-dd"; 
+2
source

The format should be:

 String fromFormat = "yyyy-MM-dd" 
+2
source

m in SimpleDateFormat is minutes, and m is month. So your first format should be yyyy-MM-dd .

+1
source

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. 

December 15, 2011

java.time

While the accepted answer is correct (capital MM for the month), there is now a better approach. The problematic old date and time classes are now inherited; they are replaced by the java.time classes.

Your input string is in standard ISO 8601 format . Therefore, you do not need to specify a formatting template 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 the console.

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

ld.toString (): 2011-12-15

day off: December 15, 2011

See live code at IdeOne.com .


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old obsolete date and time classes, such as java.util.Date , Calendar , and SimpleDateFormat .

The Joda-Time project, currently in maintenance mode , recommends switching to the java.time classes.

To learn more, see the Oracle Tutorial . And a search for many examples and explanations. JSR 310 specification .

You can exchange java.time objects directly with your database. Use a JDBC driver that conforms to JDBC 4.2 or later. No strings needed, no java.sql.* Needed.

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a testing ground for possible future additions to java.time. Here you can find some useful classes such as Interval , YearWeek , YearQuarter and others .

+1
source

Well, this may not be your case, but it may help someone. In my case, after the conversion, the day of the month and the month are set to 1. So, whatever the date, after the conversion, I get Jan 1, which is wrong. After the fight, I found that in the date format, I used YYYY instead of yyyy . When I changed all the capital letters Y to y, everything worked fine.

0
source

This worked for me instead of using the format. To work with time, just use the parse method and toString()

 String localTime="6:11"; LocalTime localTime = LocalTime.parse(localtime); LocalTime lt = 6:11; localTime = lt.toString(); 
0
source

All Articles