Skip to content Skip to sidebar Skip to footer

Java Simple Timestamp To Date Conversion

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,

Solution 1:

This works:

final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Datedate = cal.getTime();
mHour = date.getHours();
mMinute = date.getMinutes();

Solution 2:

Just use the Java Calendar class.

Calendar c = new GregorianCalendar();  // This creates a Calendar instance with the current time
mHour = c.get(Calendar.HOUR);
mMinute = c.get(Calendar.MINUTE);

Also note that your Android emulator will return times in GMT for the current time. I advise testing this type of code on a real device.

Solution 3:

Solution 4:

inttime = (int) (System.currentTimeMillis());

here you should use long instead of int.

Solution 5:

Use Time class form Google it is the best for this kind of job specialy it has good performance not like Calendar.

Post a Comment for "Java Simple Timestamp To Date Conversion"