As I know getHours()
, getMinutes()
and getSeconds()
are all deprecated in Java and they are replaced with Calendar.HOUR_OF_DAY
, Calendar.MINUTE
, Calendar.SECOND
.
These will in fact return the hour, minute and second for that particular moment. However, I would want to retrieved the hours and minutes from a Date variable. For instance,
say the time retrieved from database is
time = Thu Jan 01 09:12:18 CET 1970;
int hours = time.getHours();
int minutes = time.getMinutes();
int seconds = time.getSeconds();
By retrieving the hours, minutes, and seconds, I get
hours = 9
minutes = 12
seconds = 18
So, how do I use Calendar for this function? Although the getHours()
has been deprecated but it still worked. I would still like to know if there is an alternative to this.
Java 8
Calendar
Joda Time
Formatted
Java 8
Try this:
Edit:
above will be the hours, minutes and seconds after converting
yourdate
to System Timezone!For a time difference, note that the calendar starts at 01.01.1970, 01:00, not at 00:00. If you're using java.util.Date and java.text.SimpleDateFormat, you will have to compensate for 1 hour:
This will print:
Duration: 1:23:45.678