'What should I use instead of the deprecated Date.getHours()

If a method is deprecated in Java there will be a another better way to have same functionality, right?

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

As getHours() is deprecated, what is the best way to get hours using only the Date class?



Solution 1:[1]

You should be using a Calendar or the Joda Time library.

However if you can only use Date, this is your only method. Note, this will not adjust for timezone.

Solution 2:[2]

Javadoc explicitly suggests

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

Joda library is another best alternative to handle Date and Time.

Solution 3:[3]

These methods are indeed deprecated.

You should now use java.util.Calendar#get()

So your example becomes

Calendar cal = Calendar.getInstance();
cal.get(Calendar.HOUR);

see the javadoc of this class.

Note that you can get a Date object by calling getTime on cal.

Solution 4:[4]

Calendar cal = Calendar.getInstance();
cal.get(Calendar.HOUR);

If you want is as a date

Date date = cal.getTime();

Solution 5:[5]

As others already stated Javadoc suggests to instead use Calendar.get(Calendar.HOUR_OF_DAY).

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

Here's how you could do it for already set Date:

int getHourOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.HOUR_OF_DAY);
}

Use Calendar.HOUR_OF_DAY if you want number from 0-23 and Calendar.HOUR for 0-11.

Solution 6:[6]

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

You can use ZonedDateTime#now(ZoneId) to get the current Date-Time object from which you can get the information like timezone offset, year, month, day, hour, minute, second, nanosecond etc. If you do not need timezone offset, you can use LocalDateTime#now(ZoneId) instead. However, if you need just the current time information, you can use LocalTime#now(ZoneId) to get the local time in a specific timezone and then get hour, minute, second, nanosecond etc. from the LocalTime.

Demo:

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        // Change the JVM's ZoneId, ZoneId.systemDefault() with the applicable ZoneId
        // e.g. ZoneId.of("America/Los_Angeles")
        ZoneId zoneId = ZoneId.systemDefault();

        ZonedDateTime zdt = ZonedDateTime.now(zoneId);
        System.out.println(zdt.getHour());

        LocalDateTime ldt = LocalDateTime.now(zoneId);
        System.out.println(ldt.getHour());

        LocalTime time = LocalTime.now(zoneId);
        System.out.println(time.getHour());
    }
}

Output from a sample run:

13
13
13

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Solution 7:[7]

use such as below code.

Date date = new Date();
    System.out.println(date);
    System.out.println(new SimpleDateFormat("HH").format(date));
    System.out.println(new SimpleDateFormat("mm").format(date));
    System.out.println(new SimpleDateFormat("MM").format(date));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Lawrey
Solution 2 kosa
Solution 3 autra
Solution 4 Darussian
Solution 5 zb226
Solution 6
Solution 7 sajeeth