我知道要收到日期,我必须使用小写的3000
代码:
dd
,输出为:
日期格式:2016/28 / 06 09:03:493
但是当我以日期格式使用大写DateFormat dateFormat0 = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date dateNow0 = new Date();
System.out.println("Date format: " + dateFormat0.format(dateNow0));
时。代码:
DD
输出结果为:
日期格式:2016/28 / 66 09:03:494
为什么我使用资本获得了不同的结果' DD'而不是' dd'? DD' DD的结果是什么?代表什么?
答案 0 :(得分:6)
D
是一年中的一天(1-365)
d
是每月(1-31)
请参阅SimpleDateFormat
上的文档:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
答案 1 :(得分:1)
Java 7中的正确解决方案代码是:
DateFormat dateFormat3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat3.format(cal.getTime()));
和Java 8:
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String text = localDateTime.format(formatter);
System.out.println(text);