问题描述
我想更改我的Date对象格式。我看过其他主题,但总是将Date转换为String。我有这个:
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 = df.parse(smp);
x [i] = date;
日期返回如下:Wed Jul 01 00:00:00 CET 1970
,
,但我想要这样: 01
在日期格式不是字符串 / p>
感谢
日期对象没有格式。格式化只能在将其作为字符串显示时应用。 Date对象简单地存储关于日期(日,月,年,时间等)的所有信息。当显示该数据时,它显示为一个字符串,那就是以特定格式排列数据是有意义的。
至于输出您提到的格式,您可以使用dd的格式(因为您只需要日期,如果需要,则为零)。
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
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).
这篇关于Java更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!