// 当前时间
Date now = new Date();
// 2010上海世博会开幕时间
Date expo2010BeginDate = DateUtils.parseDate("2010-05-01 23:59:59", new String[] { "yyyy-MM-dd HH:mm:ss" });
// 2010上海世博会闭幕时间
Date expo2010EndDate = DateUtils.parseDate("2010-10-31 23:59:59", new String[] { "yyyy-MM-dd HH:mm:ss" });
System.out.println(DurationFormatUtils.formatDuration(expo2010BeginDate.getTime() - now.getTime(), "d"));
System.out.println(DurationFormatUtils.formatDuration(expo2010BeginDate.getTime() - expo2010EndDate.getTime(),
"d"));
Duration formatting utilities and constants. The following table describes the tokens used in the pattern language for formatting.
以下是format参数
| character | duration element |
|---|---|
| y | years |
| M | months |
| d | days |
| H | hours |
| m | minutes |
| s | seconds |
| S | milliseconds |
本文介绍了使用Java进行日期计算的方法,包括获取2010年上海世博会从开幕到闭幕的具体天数,以及开幕前的倒计时天数。通过`DateUtils`和`DurationFormatUtils`工具类实现了精确到秒的时间间隔展示。
3182

被折叠的 条评论
为什么被折叠?



