Error when converting date format to another

569 Views Asked by At

I have a String in format "YYYY-MM-dd" and i want convert this into "MMM dd, yyyy" format.

I used bellow code to do this; But when i convert "2014-11-18" the output is this "Sun Dec 29 00:00:00 IST 2013"

How can I solve this?

DateFormat target=new SimpleDateFormat("MMM dd, yyyy");
String P_date="2014-11-18"
Date test1 = new SimpleDateFormat("YYYY-MM-dd").parse(P_date);

String converted_date=target.format(test1);
Date test=target.parse(converted_date);
4

There are 4 best solutions below

0
On BEST ANSWER

The y (lowercase Y) format means "year". Y (uppercase Y) you were using means "WeekYear". Just use y and you should be OK:

DateFormat target=new SimpleDateFormat("MMM dd, yyyy");
String P_date="2014-11-18";
Date test1 = new SimpleDateFormat("yyyy-MM-dd").parse(P_date);

String converted_date=target.format(test1);
Date test=target.parse(converted_date);
0
On

Y returns Week year that's why you are seeing week day too. use y instead.

Date test1 = new SimpleDateFormat("yyyy-MM-dd").parse(P_date);
0
On

You can write like this

final JDateChooser startDateChooser = new JDateChooser();
startDateChooser.setDateFormatString("yyyy-MM-dd");
Date startDate = startDateChooser.getDate();
HashMap listMap = new HashMap();
listMap.put("Start Period is ", ((startDate.getYear() + 1900)+ "-" + (startDate.getMonth() + 1) + "-" +startDate.getDate()));
0
On

tl;dr

LocalDate.parse( "2014-11-18" ).format(    // Parse string as `LocalDate` object, then generate a string in a certain format.
    DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM )
        .withLocale( Locale.US )           // Automatically localize to a locale’s human language and cultural norms.
)                                          // Returns a String.

Details

The accepted Answer by Mureinik is correct, your formatting pattern used codes incorrectly.

Another issue is that you are interested in a date-only value, but you are using a date-with-time type.

Also, you are using troublesome old date-time classes that are now supplanted by the java.time classes.

java.time

Your YYYY-MM-DD format complies with ISO 8601 format. The java.time classes use those standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

LocalDate ld = LocalDate.parse( "2014-11-18" ) ;

To generate a string in other formats, use the DateTimeFormatter or DateTimeFormatterBuilder classes.

You could specify a hard-coded formatting pattern. But better to soft-code by letting java.time automatically localize. To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Example:

Locale l = Locale.US ;  // Or Locale.CANADA_FRENCH, Locale.ITALY, etc. 
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( l );
String output = zdt.format( f );

Nov 18, 2014


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.

With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or 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.