Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy

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

like image 321
user182944 Avatar asked Aug 28 '13 06:08

user182944


People also ask

How do I change the date format in YYYY-MM-DD in Java?

Formatting DatesString pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);

How do I change the date format in Java Util?

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

What date format is DD MMM YYYY in Java?

The format used is EEE, dd MMM yyyy HH:mm:ss Z in US locale. ISO8601 formatter for date-time without time zone. The format used is yyyy-MM-dd'T'HH:mm:ss. ISO8601 formatter for date-time with time zone.


2 Answers

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+

LocalDateTime ldt = LocalDateTime.now(); System.out.println(DateTimeFormatter.ofPattern("MM-dd-yyyy", Locale.ENGLISH).format(ldt)); System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH).format(ldt)); System.out.println(ldt); 

Outputs...

05-11-2018 2018-05-11 2018-05-11T17:24:42.980 

Java 7-

You should be making use of the ThreeTen Backport

Original Answer

For example...

Date myDate = new Date(); System.out.println(myDate); System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(myDate)); System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(myDate)); System.out.println(myDate); 

Outputs...

Wed Aug 28 16:20:39 EST 2013 08-28-2013 2013-08-28 Wed Aug 28 16:20:39 EST 2013 

None of the formatting has changed the underlying Date value. This is the purpose of the DateFormatters

Updated 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 to Dates. The resulting parse does not alter the way Date reports it's value.

Date#toString is just a dump of it's contents. You can't change this, but you can format the Date object any way you like

try {     Date myDate = new Date();     System.out.println(myDate);      SimpleDateFormat mdyFormat = new SimpleDateFormat("MM-dd-yyyy");     SimpleDateFormat dmyFormat = new SimpleDateFormat("yyyy-MM-dd");      // Format the date to Strings     String mdy = mdyFormat.format(myDate);     String dmy = dmyFormat.format(myDate);      // Results...     System.out.println(mdy);     System.out.println(dmy);     // Parse the Strings back to dates     // Note, the formats don't "stick" with the Date value     System.out.println(mdyFormat.parse(mdy));     System.out.println(dmyFormat.parse(dmy)); } catch (ParseException exp) {     exp.printStackTrace(); } 

Which outputs...

Wed Aug 28 16:24:54 EST 2013 08-28-2013 2013-08-28 Wed Aug 28 00:00:00 EST 2013 Wed Aug 28 00:00:00 EST 2013 

Also, be careful of the format patterns. Take a closer look at SimpleDateFormat to make sure you're not using the wrong patterns ;)

like image 136
MadProgrammer Avatar answered Sep 23 '22 13:09

MadProgrammer


SimpleDateFormat("MM-dd-yyyy"); 

instead of

SimpleDateFormat("mm-dd-yyyy"); 

because MM points Month, mm points minutes

SimpleDateFormat sm = new SimpleDateFormat("MM-dd-yyyy"); String strDate = sm.format(myDate); 
like image 40
newuser Avatar answered Sep 23 '22 13:09

newuser