How do you get Hours and Minutes since Date.getHours
and Date.getMinutes
got deprecated? The examples that I found on Google search used the deprecated methods.

- 44,500
- 61
- 101
- 156
-
2FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 20 '19 at 20:42
13 Answers
Try using Joda Time instead of standard java.util.Date classes. Joda Time library has much better API for handling dates.
DateTime dt = new DateTime(); // current time
int month = dt.getMonth(); // gets the current month
int hours = dt.getHourOfDay(); // gets hour of day
See this question for pros and cons of using Joda Time library.
Joda Time may also be included to some future version of Java as a standard component, see JSR-310.
If you must use traditional java.util.Date and java.util.Calendar classes, see their JavaDoc's for help (java.util.Calendar and java.util.Date).
You can use the traditional classes like this to fetch fields from given Date instance.
Date date = new Date(); // given date
Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
calendar.setTime(date); // assigns calendar to given date
calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
calendar.get(Calendar.HOUR); // gets hour in 12h format
calendar.get(Calendar.MONTH); // gets month number, NOTE this is zero based!

- 1
- 1

- 33,425
- 31
- 131
- 183
-
2Question: Now that Java 8 has rolled out with LocalDateTime, is there any reason you should use Joda Time over LocalDateTime? – chrips Apr 29 '18 at 14:51
-
@Chrips Opinions differ. Joda-Time officially recommends using java.time instead. – Ole V.V. Mar 21 '19 at 06:27
-
If you have libraries or legacy stuff that use Joda, that might be a good reason to use Joda with Java 8. Otherwise use java.time. – Juha Syrjälä Jan 26 '21 at 07:14
From the Javadoc for Date.getHours
As of JDK version 1.1, replaced by Calendar.get(Calendar.HOUR_OF_DAY)
So use
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
and the equivalent for getMinutes.

- 28,783
- 8
- 63
- 92
-
8So... how does this work if I want to get the hours from a Date that does not correspond to now? – Michael Sep 23 '13 at 04:40
-
See http://stackoverflow.com/questions/8150155/java-gethours-getminutes-and-getseconds – Mark Sep 23 '13 at 07:29
-
This terrible class was supplanted years ago by the modern *java.time* classes, with the adoption of JSR 310. Use `ZonedDateTime` instead. – Basil Bourque May 08 '19 at 15:28
java.time
While I am a fan of Joda-Time, Java 8 introduces the java.time package which is finally a worthwhile Java standard solution! Read this article, Java SE 8 Date and Time, for a good amount of information on java.time outside of hours and minutes.
In particular, look at the LocalDateTime
class.
Hours and minutes:
LocalDateTime.now().getHour();
LocalDateTime.now().getMinute();

- 303,325
- 100
- 852
- 1,154

- 1,025
- 8
- 14
-
4This is the best answer. You may want to provide explicit time zone to remind the reader and yourself that this is a time zone sensitive operation: `LocalDateTime.now(ZoneId.of("Asia/Manilla"))`. Read `now()` only once for consistency (time will pass between the two calls). If the date is really irrelevant, `LocalTime` works too. – Ole V.V. Jan 29 '18 at 10:38
-
1Thank you for this. I have somehow MISSED this wonderful built in class for a long time! – chrips Apr 29 '18 at 14:49
-
Comment by Ole V.V. is correct. But even better, use `ZonedDateTime` class rather than `LocalDateTime`. With `LocalDateTime` you are discarding valuable time zone information for no reason, with nothing gained. – Basil Bourque Mar 19 '19 at 23:07
-
Useful if you don't have to be backwards-compatible. Regrettably not suitable if you're writing for Android and supporting older API versions (or in any other instance when Java 8 isn't always available). – M_M Jul 23 '19 at 16:54
-
@M_M For Java 6 & 7, most of the *java.time* functionality is back-ported in the *ThreeTen-Backport* library. Further adapted to early Android in *ThreeTenABP*. For links, see the last section of [my Answer](https://stackoverflow.com/a/55269976/642706). – Basil Bourque Mar 27 '20 at 02:21
First, import java.util.Calendar
Calendar now = Calendar.getInstance();
System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE));

- 431
- 3
- 5
tl;dr
ZonedDateTime.now().getHour()
… or …
LocalTime.now().getHour()
ZonedDateTime
The Answer by J.D. is good but not optimal. That Answer uses the LocalDateTime
class. Lacking any concept of time zone or offset-from-UTC, that class cannot represent a moment.
Better to use ZonedDateTime
.
ZoneId z = ZoneID.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Specify time zone
If you omit the ZoneId
argument, one is applied implicitly at runtime using the JVM’s current default time zone.
So this:
ZonedDateTime.now()
…is the same as this:
ZonedDateTime.now( ZoneId.systemDefault() )
Better to be explicit, passing your desired/expected time zone. The default can change at any moment during runtime.
If critical, confirm the time zone with the user.
Hour-minute
Interrogate the ZonedDateTime
for the hour and minute.
int hour = zdt.getHour() ;
int minute = zdt.getMinute() ;
LocalTime
If you want just the time-of-day without the time zone, extract LocalTime
.
LocalTime lt = zdt.toLocalTime() ;
Or skip ZonedDateTime
entirely, going directly to LocalTime
.
LocalTime lt = LocalTime.now( z ) ; // Capture the current time-of-day as seen in the wall-clock time used by the people of a particular region (a time zone).
java.time types
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 303,325
- 100
- 852
- 1,154
One more way of getting minutes and hours is by using SimpleDateFormat.
SimpleDateFormat formatMinutes = new SimpleDateFormat("mm")
String getMinutes = formatMinutes.format(new Date())
SimpleDateFormat formatHours = new SimpleDateFormat("HH")
String getHours = formatHours.format(new Date())

- 1,053
- 11
- 20
-
FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 20 '19 at 20:42
Try Calender. Use getInstance to get a Calender-Object. Then use setTime to set the required Date. Now you can use get(int field) with the appropriate constant like HOUR_OF_DAY or so to read the values you need.
http://java.sun.com/javase/6/docs/api/java/util/Calendar.html

- 10,634
- 6
- 46
- 76
int hr=Time.valueOf(LocalTime.now()).getHours();
int minutes=Time.valueOf(LocalTime.now()).getMinutes();
These functions will return int values in hours and minutes.

- 7,830
- 13
- 34
- 43

- 41
- 1
-
1Duplicate. This approach was already covered in the [Answer by J.D.](http://stackoverflow.com/a/29270120/642706). – Basil Bourque Jan 13 '17 at 17:41
-
Is that `java.sql.Time`? Certainly no reason at all to bring that outdated class into play when you’ve got `java.time.LocalTime`. You’re overcomplicating. – Ole V.V. Jan 29 '18 at 10:34
-
Looks like `getMinutes()` has been deprecated in (I am using java 1.8) and has been replaced with `Calendar.get(Calendar.MINUTE)` – Saltz3 Nov 28 '18 at 14:18
public static LocalTime time() {
LocalTime ldt = java.time.LocalTime.now();
ldt = ldt.truncatedTo(ChronoUnit.MINUTES);
System.out.println(ldt);
return ldt;
}
This works for me

- 21
- 1
I would recommend looking ad joda time. http://www.joda.org/joda-time/
I was afraid of adding another library to my thick project, but it's just easy and fast and smart and awesome. Plus, it plays nice with existing code, to some extent.
- Get hour from Date variable (yourdate):
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourdate);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);

- 31
- 2
While I wouldn't recommend doing so, I think it's worth pointing out that although many methods on java.util.Date have been deprecated, they do still work. In trivial situations, it may be OK to use them. Also, java.util.Calendar is pretty slow, so getMonth and getYear on Date might be be usefully quicker.

- 398,947
- 96
- 818
- 769
import java.util.*
You can gethour and minute using calendar and formatter class. Calendar cal = Calendar.getInstance()
and Formatter fmt=new Formatter()
and set a format for display hour and minute fmt.format("%tl:%M",cal,cal)
and print System.out.println(fmt)
output shows like 10:12

- 2,257
- 1
- 15
- 24