以下代码中的问题是我正在尝试将晚上时间设置为下午6点。请忽略Calendar.evening值,因为它是用于测试目的。 这是我的LOG文件
D /通知:MORN Tue Nov 14 16:00:00 GMT + 05:30 2017 vs EVE Tue Nov 14 16:00:00 GMT + 05:30 2017
MORN:Tue Nov 14 16:00:00 GMT + 05:30 2017 vs Tue Nov 14 16:00:00 GMT + 05:30 2017 EVE:Tue Nov 14 16:00:00 GMT + 05:30 2017 vs vs Tue Nov 14 16:00:00 GMT + 05:30 2017
现在问题是两个getTime()都返回CURRENT TIME。
merge
答案 0 :(得分:1)
使用AM / PM需要使用Calendar.HOUR
,而不是Calendar.HOUR_OF_DAY
,在设置AM / PM时会被忽略:
Calendar morn = Calendar.getInstance();
morn.set(Calendar.HOUR_OF_DAY, 4);
morn.set(Calendar.MINUTE, 0);
morn.set(Calendar.SECOND, 0);
morn.set(Calendar.AM_PM, Calendar.PM);
System.out.println(someDateFormat.format(morn.getTime()));
// current hour
morn = Calendar.getInstance();
morn.set(Calendar.HOUR, 4);
morn.set(Calendar.MINUTE, 0);
morn.set(Calendar.SECOND, 0);
morn.set(Calendar.AM_PM, Calendar.PM);
System.out.println(someDateFormat.format(morn.getTime()));
// correct hour