如何在java中将时间设置为日期对象

2022-01-11 00:00:00 datetime date calendar java

我在 Java 中创建了一个 Date 对象.当我这样做时,它会显示如下内容:date=Tue Aug 09 00:00:00 IST 2011.结果,我的 Excel 文件似乎小了一天(2 月 27 日变成了 2 月 26 日,依此类推)我认为这一定是因为时间.如何将其设置为下午 5:30?

I created a Date object in Java. When I do so, it shows something like: date=Tue Aug 09 00:00:00 IST 2011. As a result, it appears that my Excel file is lesser by one day (27 feb becomes 26 feb and so on) I think it must be because of time. How can I set it to something like 5:30 pm?

推荐答案

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,17);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

Date d = cal.getTime();

另见

  • 乔达时间
  • 日历文档

相关文章