【发布时间】:2017-11-09 10:46:32
【问题描述】:
我尝试了下面的代码。
Timestamp timestampDate = scheduled.getInterviewDateAndTime(); //from DB
Map<String, Object> map = new HashMap();
map.put("eventTitle", "interview with");
map.put("startDateTime", timestampDate);
System.out.println("startDateTime : " + timestampDate);
long addTime = 1*60*60*1000;
timestampDate.setTime(timestampDate.getTime() + TimeUnit.HOURS.toMillis(addTime));
map.put("endDateTime", timestampDate);
System.out.println("endDateTime : " + timestampDate);
这是正确的方法还是有什么好的替代方法?
输出是:
startDateTime : 2017-11-07 09:08:00.0
endDateTime : 2428-07-15 09:08:00.0
如何得到正确的输出?
【问题讨论】:
-
请用
ctr + k格式化代码。 -
正确吗 - 你检查了吗?
-
map的定义是什么?为什么先输入<String, String>然后输入<String, long>? (第一行很奇怪) -
map.put("startDateTime", timestampDate);和 map.put("endDateTime", timestampDate);引用同一个对象。您需要为 endDateTime 创建一个新对象。
-
想知道您是否有可能摆脱早已过时的
Timestamp类?现代 Java 日期和时间 API 使用起来要好得多。Instant类是Timestamp的自然替代品。即使您绑定到Timestamp,您仍然可以从使用Instant计算“时间戳加1 小时”中受益。
标签: java collections timestamp