Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting hours,minutes, and seconds from Date? [duplicate]

Tags:

I want to get the current hour,minute, and second from the Date object.

This is my code:

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

But I get a kind of error, the getHours() part doesnt work. I get a "The method getHours() from the type Data is deprecated". What does that mean, and how do I get the hour,minutes, and seconds?

like image 757
Carlton Avatar asked Aug 24 '14 16:08

Carlton


People also ask

Is used to show current year Month date with hours minutes and seconds?

YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter. HH:mm:ss. sss – is the time: hours, minutes, seconds and milliseconds.

How can I get minutes from datetime?

To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.


1 Answers

Use Calendar:

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

From Date javadoc:

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.

like image 157
nanofarad Avatar answered Sep 21 '22 04:09

nanofarad