我想设置日历的时间部分。这就是我正在做的事情
Calendar calNow = Calendar.getInstance();
Calendar endWait = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date d1 = null;
try {
d1 = sdf.parse("14:45");
}catch(ParseException ex){
logger.error("Error parsing time");
}
endWait.setTime(d1);
Date waitTo = endWait.getTime();
Date now = calNow.getTime();
“now”变量是正确的日期和时间,但是 waitTo 预计是今天的日期和时间 14:45,但实际上是明天的 02:45。
最佳答案
对我来说,不是明天,而是 waitTo = Thu Jan 01 14:45:00 CET 1970
。
原因可以在 javadoc 中找到SimpleDateFormat 的:
This parsing operation uses the calendar to produce a Date. All of the calendar's date-time fields are cleared before parsing, and the calendar's default values of the date-time fields are used for any missing date-time information. For example, the year value of the parsed Date is 1970 with GregorianCalendar if no year value is given from the parsing operation.
关于java - 将时间解析为日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25037169/