Java日历始终显示相同的时间 [英] Java Calendar always shows the same time

查看:104
本文介绍了Java日历始终显示相同的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码.

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);
}
}

日历.小时应该给我

public static final int HOUR get和set的字段号,指示早上或 下午. HOUR用于12小时制(0-11).中午和午夜用0表示,而不是 到12点.例如,在10:04:15.250 PM,小时是10.

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.我在这里做什么错了?

It doesnt matter how many times I run this code, it always gives me the same unique_id. (101213) and my local time on my machine is 1:30pm. What am I doing wrong here?

谢谢.

推荐答案

您的代码只是连接常量,Calendar定义这些常量以标识其中的某些字段.要获取这些字段的值,请调用Calendar.get()并将常量标识符作为参数传递:

Your code is just concatenating constants, that the Calendar defines to identify some of it's fields. To get values of these fields, call Calendar.get() and pass the constant identifier as an argument:

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(精确到毫秒),请考虑

The above would work, but the result will be far from unique ID. To get an ID uniquely identifying a point in time (with the precision of milliseconds), consider Calendar.getTimeInMillis().

这篇关于Java日历始终显示相同的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆