有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从日期开始获得小时、分钟和秒?

我想从Date对象获取当前的小时、分钟和秒

这是我的代码:

Date date = new Date();
int hour = date.getHours();

但是我得到了一种错误,getHours()部分不起作用。我得到一个“类型数据中的getHours()方法已弃用”。这意味着什么?我如何计算小时、分钟和秒


共 (1) 个答案

  1. # 1 楼答案

    使用^{}

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);  
    int hours = cal.get(Calendar.HOUR_OF_DAY);
    

    Date{a2}

    Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MINUTE).

    Returns the number of minutes past the hour represented by this date, as interpreted in the local time zone. The value returned is between 0 and 59.