java - 将时间解析为日历

标签 java calendar

我想设置日历的时间部分。这就是我正在做的事情

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/

相关文章:

java - 基本 MongoOperations 调用返回零

java - 从哪里开始使用神经网络进行手写识别?

java - notificationDataSetChanged() 未在 gridview 中刷新

java - ArrayList 获取操作 O(1)。如何?

java - 生产者线程和消费者线程之间的线程间通信?

android - Android 2.2 日历

css - 如何响应插件生成的表格内容

javascript - 是否有一个采用ical链接作为输入来显示事件的JavaScript日历?

datetime - 返回给定年份复活节日期的函数

java - 插入语句在java中不起作用