I have a java.util.Date
in the format yyyy-mm-dd
. I want it to be in the format mm-dd-yyyy
Below is the sample util I tried out for this conversion:
// Setting the pattern
SimpleDateFormat sm = new SimpleDateFormat("mm-dd-yyyy");
// myDate is the java.util.Date in yyyy-mm-dd format
// Converting it into String using formatter
String strDate = sm.format(myDate);
//Converting the String back to java.util.Date
Date dt = sm.parse(strDate);
Still the output I am getting is not in the format mm-dd-yyyy
.
Kindly let me know how to format a java.util.Date
from yyyy-mm-dd
to mm-dd-yyyy
It is simple use below codes.
instead of
because
MM points Month
,mm points minutes
Please change small "mm" month to capital "MM" it will work.for reference below is the sample code.
tl;dr
Details
As other mentioned, the
Date
class has no format. It has a count of milliseconds since the start of 1970 in UTC. No strings attached.java.time
The other Answers use troublesome old legacy date-time classes, now supplanted by the java.time classes.
If you have a
java.util.Date
, convert to aInstant
object. TheInstant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).Time zone
The other Answers ignore the crucial issue of time zone. Determining a date requires a time zone. For any given moment, the date varies around the globe by zone. A few minutes after midnight in Paris France is a new day, while still “yesterday” in Montréal Québec.
Define the time zone by which you want context for your
Instant
.Apply the
ZoneId
to get aZonedDateTime
.LocalDate
If you only care about the date without a time-of-day, extract a
LocalDate
.To generate a string in standard ISO 8601 format, YYYY-MM-DD, simply call
toString
. The java.time classes use the standard formats by default when generating/parsing strings.If you want a MM-DD-YYYY format, define a formatting pattern.
Note that the formatting pattern codes are case-sensitive. The code in the Question incorrectly used
mm
(minute of hour) rather thanMM
(month of year).Use the same
DateTimeFormatter
object for parsing. The java.time classes are thread-safe, so you can keep this object around and reuse it repeatedly even across threads.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.'M' (Capital) represent month & 'm' (Simple) represent minutes
Some example for months
Some example for minutes
Date
is a container for the number of milliseconds since the Unix epoch ( 00:00:00 UTC on 1 January 1970).It has no concept of format.
Java 8+
Outputs...
Java 7-
You should be making use of the ThreeTen Backport
Original Answer
For example...
Outputs...
None of the formatting has changed the underlying
Date
value. This is the purpose of theDateFormatter
sUpdated with additional example
Just in case the first example didn't make sense...
This example uses two formatters to format the same date. I then use these same formatters to parse the
String
values back toDate
s. The resulting parse does not alter the wayDate
reports it's value.Date#toString
is just a dump of it's contents. You can't change this, but you can format theDate
object any way you likeWhich outputs...
Also, be careful of the format patterns. Take a closer look at
SimpleDateFormat
to make sure you're not using the wrong patterns ;)