我想设置日期 - 实例的小时和分钟。我有3个解决方案。设置带日历或日期时间的日期
我应该服用什么,为什么? :
版本1:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 8);
calendar.set(Calendar.HOUR, 30);
return calendar.getTime();
版本2:
ZonedDateTime i = date.toInstant()
.atZone(ZoneId.systemDefault())
.withHour(8)
.withMinute(30);
return Date.from(i.toInstant());
版本3:
ZonedDateTime i = date.toInstant()
.atZone(ZoneId.systemDefault())
.withHour(8)
.withMinute(30);
return new Date(i.toEpochSecond());
它不会帮助你的第一个版本试图将小时设置为30,而你的第二个和第三个版本对秒或毫秒不起作用...... –
我会说版本2.它最少使用遗留API 。 – Barend
即使你改正了......你可以去任何方法 – Akshay