I've been trying to find the answer to this for a while today and there's just so much contradictory information....
What I'd like to do is get a current unix timestamp in android, and then convert it to a format that allows me to getHours() and getMinutes().
I'm currently doing this:
int time = (int) (System.currentTimeMillis());
Timestamp ts = new Timestamp(time);
mHour = ts.getHours();
mMinute = ts.getMinutes();
But it's not giving me a correct value for hour or minute (it's returning 03:38 for the current East-coast time of 13:33).
The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).
Java Code:Timestamp ts = new Timestamp(date. getTime()); Above example command will return the current timestamp and save values in ts object variable.
This works:
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
mHour = date.getHours();
mMinute = date.getMinutes();
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