4

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

forcey123
  • 139
  • 3
  • 3
  • 6
  • 2
    "d" Not "D" specifies "Day in month", "M" not "m" specifies "Month in year". I feel a little bit ashamed to get reputation from this kind of questions. – Hui Zheng Feb 12 '13 at 15:40
  • 3
    For the record, when *two* different APIs aren't producing the results you suspect it's unlikely that the problem resides in the APIs. – Brian Roach Feb 12 '13 at 15:42

9 Answers9

17

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);
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
4

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

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
Dave DeCaprio
  • 2,051
  • 17
  • 31
3

Try this: "yyyy-MM-dd"

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

mightyrick
  • 910
  • 4
  • 6
2

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 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.

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

You may want to use format "yyyy-MM-dd" with lower case d. Capital D is the format for day in year. You can find an overview of date patterns here.

Howard
  • 38,639
  • 9
  • 64
  • 83
1

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

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Your format string should be "YYYY-MM-dd" rather than "YYYY-MM-DD".

DD is "Day in year", while dd is "Day in month".

Check out the reference.

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
0

Looks like the dateformat should be in lower case

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

That returns 2013-02-12

Else it returns the day of the year.

Please refer to SimpleDateFormat API page for the entire list of accepted format patterns.

Arun Manivannan
  • 4,213
  • 3
  • 29
  • 38
0

Use following code:

SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd");
Date date = new Date();
System.out.println(df.format(date));

It should be dd and not DD.

deej
  • 2,536
  • 4
  • 29
  • 51