下面是我的代码。
public class TestCalendar {
public static void main(String[] args){
int unique_id = Integer.parseInt("" + Calendar.HOUR + Calendar.MINUTE
+ Calendar.SECOND);
System.out.println(unique_id);
}
}
Calendar.HOUR 应该给我
public static final int HOUR 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.
无论我运行这段代码多少次,它总是给我相同的 unique_id。 (101213) 我机器上的本地时间是下午 1:30。我在这里做错了什么?
谢谢。
最佳答案
您的代码只是连接常量,Calendar 定义这些常量以标识它的某些字段。要获取这些字段的值,请调用 Calendar.get()
并将常量标识符作为参数传递:
public class TestCalendar {
public static void main(String[] args){
Calendar c = Calendar.getInstance();
int unique_id = Integer.parseInt("" + c.get(Calendar.HOUR) + c.get(Calendar.MINUTE)
+ c.get(Calendar.SECOND));
System.out.println(unique_id);
}
}
上面的方法可行,但结果与唯一 ID 相去甚远。
要获得唯一标识时间点的 ID(精度为毫秒),请考虑 Calendar.getTimeInMillis()
.
关于Java 日历总是显示同一时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082851/