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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With