I’m trying to do a function that will parse a long millesecond value into a Date object with formatting:
public static Date parseDate(long millisec, String format) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date formattedDate = new Date(millisec);
formatter.format(formattedDate);
return formattedDate;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
The format I plugged into the function is this: “dd-MM-yyyy HH-mm:ss”
And still I am getting this result format: “Thu Apr 19 19:51:22 SGT 2012“
Any ideas why I get this kind of result?
The format is applied only when you output the date (actually it is used to convert the date to string). It will not change the internal representation of the date.
In your case the
formattedDateobject will not be affected in any way by theformat.A way to see the string representation is like that:
This is like the bases of a number. You have many different visualizations of a number like
101(2)or5(10), but they have meaning only when displaying the number. Otherwise the value of the number itself does not change when you change the base number.