Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the date format in Java? [duplicate]

Tags:

java

date

I need to change the date format using Java from

 dd/MM/yyyy  to yyyy/MM/dd 
like image 988
rasi Avatar asked Aug 12 '10 15:08

rasi


People also ask

How do you change date format to MM DD YYYY in Java?

You can just use: Date yourDate = new Date(); SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); String date = DATE_FORMAT. format(yourDate);

How do I change the format of a date object in Java?

String mydateStr = "/107/2013 12:00:00 AM"; DateFormat df = new SimpleDateFormat("/dMM/yyyy HH:mm:ss aa"); Date mydate = df. parse(mydateStr); Two method above can be used to change a formatted date string from one into the other. See the javadoc for SimpleDateFormat for more info about formatting codes.


2 Answers

How to convert from one date format to another using SimpleDateFormat:

final String OLD_FORMAT = "dd/MM/yyyy"; final String NEW_FORMAT = "yyyy/MM/dd";  // August 12, 2010 String oldDateString = "12/08/2010"; String newDateString;  SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT); Date d = sdf.parse(oldDateString); sdf.applyPattern(NEW_FORMAT); newDateString = sdf.format(d); 
like image 178
Christopher Parker Avatar answered Sep 30 '22 03:09

Christopher Parker


SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); sdf.format(new Date()); 

This should do the trick

like image 29
KristofMols Avatar answered Sep 30 '22 01:09

KristofMols