Java Change Date Format [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-25 19:39:17

问题


I want to change my Date object format. I looked other topics but always Date converted to String. I have this one:

String datee=unitdata[i].toString();
Date dater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(datee);
String smp= new SimpleDateFormat("MM").format(dater);
SimpleDateFormat df = new SimpleDateFormat("MM");
Date date = df.parse(smp);
x[i]=date;

date returned like this: "Wed Jul 01 00:00:00 CET 1970", but I want like this: "01" in Date format not String.

Thanks


回答1:


Date objects don't have a format. Formatting can only be applied when you display them as a strings. A Date object simply stores all the info about the date (day, month, year, time, etc). When displaying that data, it is displayed as a string, and that is when it makes sense to arrange the data in a specific format.

As for outputting the format you mentioned, you can use a format of "dd" (since you just want the date, with leading zero if needed).



来源:https://stackoverflow.com/questions/25203897/java-change-date-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!