Java Date() giving the wrong date [duplicate]

I've got a simple method that should get the current date, put it into a certain format and then return it as a String. Up to this point it's been fine (last tried it on about 31st Jan) but for some reason when I tried it today it returns the String "2013-02-43".

Now obviously there aren't 43 days in February and I have no idea why it's returning this. I've searched everywhere I can for a solution but none of them seem to fit the specific problem I am having. Here is the code:

public String getDate(){
    DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD");
    Date date = new Date();

    return dateFormat.format(date);
}

Just for the record I've tried using Calendar.getInstance() etc. with the same result. Interestingly when I try

get(Calendar.DAY_OF_MONTH) 

it comes back with 12, so somewhere the numbers are right but something's going wrong in between.

Thanks


Solution 1:

DD means Day of Year, while dd means Day of Month. Also, Y means Week Year, while y means Year. You want yyyy-MM-dd (case-sensitive).

public String getDate(){
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();

    return dateFormat.format(date);
}

Solution 2:

Capital D in a DateFormat is date in year. You want date in month:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Solution 3:

Try this: "yyyy-MM-dd"

Case sensitivity matters. "DD" is the day in the year.

Solution 4:

tl;dr

LocalDate.now( ZoneId.of( "America/Montreal" ) )
         .toString()

2016-12-24

Details

The accepted Answer by Cashwell is correct: The formatting pattern codes are incorrect, case-sensitive.

Using java.time

The more modern way to do this is with the java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

Strings

Your desired output happens to comply with the ISO 8601 standard text formats for date-time values. The java.time classes use ISO 8601 by default when parsing/generating strings. So no need to specify a formatting pattern at all!

String output = today.toString() ;

2016-12-24

By the way, while there is no need to do so in this case, if you were to create a formatting pattern it would look like this code example. The codes are similar to those of SimpleDateFormat but not exactly the same, so study the class doc carefully.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd" );
String output = today.format( f );

Usually best to also specify a Locale as a habit, but not necessary in this particular case.


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?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use….

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.

Solution 5:

Read the javadoc of SimpleDateFormat. Y doesn't exist, and D is the day in year. You want yyyy-MM-dd as your pattern.