Android Get Current timestamp?
Solution 1
The solution is :
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
Solution 2
From developers blog:
System.currentTimeMillis()
is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis()
, consider listening to the ACTION_TIME_TICK
, ACTION_TIME_CHANGED
and ACTION_TIMEZONE_CHANGED
Intent broadcasts to find out when the time changes.
Solution 3
1320917972 is Unix timestamp using number of seconds since 00:00:00 UTC on January 1, 1970. You can use TimeUnit
class for unit conversion - from System.currentTimeMillis()
to seconds.
String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
Solution 4
You can use the SimpleDateFormat class:
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
String format = s.format(new Date());
Solution 5
Use below method to get current time stamp. It works fine for me.
/**
*
* @return yyyy-MM-dd HH:mm:ss formate date as string
*/
public static String getCurrentTimeStamp(){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateTime = dateFormat.format(new Date()); // Find todays date
return currentDateTime;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Related videos on Youtube
Rjaibi Mejdi
Updated on July 15, 2022Comments
-
Rjaibi Mejdi almost 2 years
I want to get the current timestamp like that : 1320917972
int time = (int) (System.currentTimeMillis()); Timestamp tsTemp = new Timestamp(time); String ts = tsTemp.toString();
-
Basil Bourque over 5 yearsFYI, the troublesome old date-time classes such as
java.util.Date
,java.util.Calendar
, andTimestamp
are now legacy, supplanted by the java.time classes. Most of the java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android in the ThreeTenABP project. See How to use ThreeTenABP….
-
-
Bianca Daniciuc almost 11 yearsFrom developer.android.com/reference/java/lang/… I found that
System.nanoTime()
is an alternative toSystem.currentTimeMillis()
and it has no unpredictable fluctuations, and is designed for measuring duration differences. -
Michał Klimczak over 10 yearsThis is not a timestamp (time since epoch)
-
Michał Klimczak over 10 years@ana01 "the zero value is typically whenever the device last booted" - so it can be used only when you compare duration differences on the same device. Not useful for database storage for example.
-
Seynorth almost 10 yearsNot a timestamp, and moreover, not efficiant at all (creation of SimpleDateFormat, creation of date, conversion). Avoid it.
-
Admin over 9 yearsThat is not a timestamp.
-
Akash Agarwal over 8 yearsJust a note to @ana01 's comment that
System.nanoTime()
isn't suitable for to display wall clock time. For that purpose, useSystem.currentTimeMillis()
instead. -
Abbas about 8 yearsHey, Could u also tell the best way to sort a list of timestamps? I was thinking of sorting them myself but thought there may be a better way.
-
jason.kaisersmith about 8 yearsFor future reference for anybody reading this be aware that "android.text.format.Time" is now deprecated
-
ersks almost 8 years@Hits, It's Simple Date Format, not a timestamp. You are just using variable name as currentTimeStap.
-
ersks almost 8 yearsYou are answering sth else, but it's also valid.
-
kjdion84 over 6 yearsThis doesn't factor in the timezone offset of the device.
-
Slobodan Antonijević over 5 yearsAlmost perfect answer (I upvoted) but, at least in my honest opinion, you should remove the ThreeTen Backport reference as this is a question about Android and not Java in general. And it may get to be confusing for beginners at Android.
-
Cԃաԃ almost 5 years@kjdion84 excuse me, but why do you think it would be important ? Just based on the question.
-
Basil Bourque over 4 years@SlobodanAntonijević It is important for Android programmers to understand that (a) if they are supporting Android 26 and later, they have an implementation of java.time built-in, (b) if supporting early Android before 26, they must add a library, the ThreeTenABP library, and (c) if using ThreeTenABP, know that this library is actually an adaptation of ThreeTen-Backport adapted from Java to Android. The ThreeTenABP library is just a Android-specific wrapper around ThreeTen-Backport library. See this table graphic comparing the 3 libraries.
-
Slobodan Antonijević over 4 years@BasilBourque you are completely correct. But, you can in fact import ThreeTen-Backport and use it in an Android app, but with extremely huge impact to performance cause of its JAR dependency. This is why I've said that the post should be more specific that Android devs should never use ThreeTen-Backport, and instead use ThreeTenABP for API 25 and below support. I've seen numerous devs on various boards that get confused on which one should be used for Android, cause they sound similar by name.
-
Ole V.V. over 4 yearsGood point, @SlobodanAntonijević. I have tried to make it just a little clearer.
-
Basil Bourque over 4 years@SlobodanAntonijević This table showing where to obtain java.time for both Java and Android might help make it clear.
-
nyxee almost 4 yearsThis gives GMT time.
-
Donald Duck almost 3 yearsIs there a way to get this as
int
?