日历 HOUR_OF_DAY 的问题
Issue with HOUR_OF_DAY of Calendar
在调试模式下,我看到我正在解析的值等于 "2015-09-01 22:00"
。当我创建日历时,我看到 HOUR_OF_DAY
等于 22。
但是,interval
等于10,而不是22。哪里出错了?
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class HelloWorld{
public static void main(String []args){
try {
Calendar c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2015-09-01 22:00"));
int interval = c.get(Calendar.HOUR_OF_DAY-1);
System.out.println(interval);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
您要求的不是 HOUR_OF_DAY
值 - 您要求的是 HOUR_OF_DAY - 1
值,它实际上对应于 Calendar.HOUR
- 12 小时版本:
Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.
只需将您的代码更改为
int interval = c.get(Calendar.HOUR_OF_DAY);
...会好的。
在调试模式下,我看到我正在解析的值等于 "2015-09-01 22:00"
。当我创建日历时,我看到 HOUR_OF_DAY
等于 22。
但是,interval
等于10,而不是22。哪里出错了?
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class HelloWorld{
public static void main(String []args){
try {
Calendar c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2015-09-01 22:00"));
int interval = c.get(Calendar.HOUR_OF_DAY-1);
System.out.println(interval);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
您要求的不是 HOUR_OF_DAY
值 - 您要求的是 HOUR_OF_DAY - 1
值,它实际上对应于 Calendar.HOUR
- 12 小时版本:
Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.
只需将您的代码更改为
int interval = c.get(Calendar.HOUR_OF_DAY);
...会好的。