2

This is a possible duplicate, but I'm not able to figure out why the month is returned as zero when specified as MMM and works well with mm(numeric). Any help here would be really appreciated?

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Locale;

 public class time1 {

 public static void main(String[] args) {

    DateFormat originalFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
    DateFormat targetFormat = new SimpleDateFormat("yyyy-mm-dd");
    Date date = null;
    try {
            date = originalFormat.parse("26-Aug-2011");
    } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
    String formattedDate = targetFormat.format(date);
    System.out.println("old date: " + date);
    System.out.println("new date: " + formattedDate);
 }
}

The output is:

old date: Fri Aug 26 00:00:00 IST 2011
new date: 2011-00-26

When the format is changed to dd-mm-yyyy and the date is 26-08-2011, the output is

old date: Wed Jan 26 00:07:00 IST 2011
new date: 2011-07-26

I'm not able to understand the reason it fails with MMM, all my dates are in the format(26-Aug-2011) and I need to convert them to yyyy-mm-dd (26-07-2011).

user3616977
  • 527
  • 2
  • 5
  • 14
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 22 '17 at 17:11

2 Answers2

11

From the documentation, I can say that

DateFormat targetFormat = new SimpleDateFormat("yyyy-mm-dd");

needs to be changed to

DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
// this gives you the Date in Digits.

As per documentation,

'M' is used for Month in Year, whereas
'm' is used for Minute in Hour.

Hence, your 'mm' is returning the Minutes which it takes as 00:00 by default which is the output you have been getting. This will give you the following Output.

old date: Fri Aug 26 00:00:00 IST 2011
new date: 2011-08-26
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
4

java.time

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

String input = "26-Aug-2011" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US ) ;
LocalDate ld = LocalDate.parse( input , f ) ;

You desired output format of YYYY-MM-DD happens to comply with the ISO 8601 standard. The java.time classes use the standard formats by default when parsing/generating strings.

String output = ld.toString() ;

2011-08-26

See this code run live at 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.

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