19

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?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
sunleo
  • 10,589
  • 35
  • 116
  • 196

7 Answers7

13

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.

kosa
  • 65,990
  • 13
  • 130
  • 167
12

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.

zb226
  • 9,586
  • 6
  • 49
  • 79
beam022
  • 1,793
  • 4
  • 20
  • 27
  • 3
    Your answer is the only one describing how to get from an existing Date to the hour like OP asked. Your answer should be accepted! – Martin Eckleben May 11 '22 at 09:35
10

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.

autra
  • 895
  • 6
  • 21
6
Calendar cal = Calendar.getInstance();
cal.get(Calendar.HOUR);

If you want is as a date

Date date = cal.getTime();
Darussian
  • 1,573
  • 1
  • 16
  • 28
  • Wrong. `Date.getHours()` tried to return the hour of day (0–23). `cal.get(Calendar.HOUR)` gives you the hour within AM or PM (0–11). Not that it matters these days. Both `Date` and `calendar` were poorly designed and are long outdated. Don’t use either of them at all. Use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 22 '21 at 18:08
5

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1

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));
}
sajeeth
  • 47
  • 2
  • 9
  • As this answer currently stands, it is hard to decipher which line of output shows the expected result without knowing or referring to the specification of `SimpleDateFormat`. Please remove the unnecessary outputs or elaborate on each line of output. – Izruo Jul 21 '21 at 11:29