Change the format of Date Java [closed]

First of all: java.util.Date object never has a format stored in it. You can think of Date as an object containing long value of milliseconds since epoch at UTC timezone. The class has few methods, but no timezone offset, formatted pattern etc stored in it.

Secondly, following basic code can be used to format a date to yyyy-MM-dd in your machine’s locale:

Date mydate = ...
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String mydateStr = df.format(mydate);

Similarly, you can parse a string “/107/2013 12:00:00 AM” into a date object in your machine’s locale like this:

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.

Leave a Comment