Java 日历总是显示相同的时间

Java Calendar always shows the same time(Java 日历总是显示相同的时间)
本文介绍了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);
}
}

Calendar.HOUR 应该给我

Calendar.HOUR is supposed to give me

public static final int HOUR 用于获取和设置的字段编号,指示早上的时间或下午.HOUR 用于 12 小时制 (0 - 11).中午和午夜用 0 表示,而不是到 12 点.例如,在晚上 10:04:15.250,HOUR 是 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(精度为毫秒),请考虑 Calendar.getTimeInMillis().

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 日历总是显示相同的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)