-5

Hi I am trying to get today's date but not getting proper output

import java.text.DateFormat;
import java.text.SimpleDateFormat;

private static final DateFormat sdf = new SimpleDateFormat("DD/MM/YYYY");
System.out.println("TODAY  :" + sdf.format(new Date()));

output

TODAY  :142/05/2017

Year and month coming properly but why the day is coming like this

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
subhashis
  • 4,629
  • 8
  • 37
  • 52
  • It's `dd/MM/yyyy` – Murat Karagöz May 22 '17 at 10:35
  • 1
    DD is day in year: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Stefan Warminski May 22 '17 at 10:35
  • See this you will surely get the ans http://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string – Mukesh May 22 '17 at 10:36
  • I am sure this question is a duplicate. I don’t think the link in the previous comments is the best, but I have not found a better one myself yet, so please check it out. – Ole V.V. May 22 '17 at 11:43
  • 1
    While the answers given here are correct, the better way to format today’s date is with the newer and more programmer-friendly classes described in JSR-310: `LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))`. – Ole V.V. May 22 '17 at 11:56
  • Possible duplicate of [“EEE MMM dd HH:mm:ss ZZZ yyyy” date format to java.sql.Date](http://stackoverflow.com/questions/43933597/eee-mmm-dd-hhmmss-zzz-yyyy-date-format-to-java-sql-date). – Ole V.V. May 22 '17 at 11:59
  • Or possible duplicate of [Converting current time to this format: “2017-04-25T17:12:42+01:00”](https://stackoverflow.com/questions/44067846/converting-current-time-to-this-format-2017-04-25t1712420100). – Ole V.V. May 22 '17 at 13:51

4 Answers4

4

D is Day in year d is Day in month http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

EDIT: Also as @JeremyP noticed you can use yyyyfor getting year 'cause Y is Week year

1

Oracle:

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

  • simple y Year -Year (1996; 96)
  • capital Y Week year -Year (2009; 09)

  • simple d Day in month -Number (10)

  • capital D Day in year -Number (189)

  • capital M Month in year -Month (July; Jul; 07)

In your code:

SimpleDateFormat("DD/MM/YYYY")

Should be:

SimpleDateFormat("dd/MM/yyyy")

refer documentation for more info

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

According to the javadocs D is for the day in the year and Y is for the week year.

You need to use d (lower case) for Day in month and y (lower case) for Year

This you pattern should look like:

 DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
A4L
  • 17,353
  • 6
  • 49
  • 70
0

Replace DD with dd and YYYY to yyyy. MM is in capital and others are in lowercase.

d = day of the month

D = day of the year

M = month in year

m = minute in hour

y = Year

Y = Week year

private static final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("TODAY  :" + sdf.format(new Date()));
Community
  • 1
  • 1
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53