Android Get Current timestamp?

357,047

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;
    }
}
Share:
357,047

Related videos on Youtube

Rjaibi Mejdi
Author by

Rjaibi Mejdi

Updated on July 15, 2022

Comments

  • Rjaibi Mejdi
    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
      Basil Bourque over 5 years
      FYI, the troublesome old date-time classes such as java.util.Date, java.util.Calendar, and Timestamp 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
    Bianca Daniciuc almost 11 years
    From developer.android.com/reference/java/lang/… I found that System.nanoTime() is an alternative to System.currentTimeMillis() and it has no unpredictable fluctuations, and is designed for measuring duration differences.
  • Michał Klimczak
    Michał Klimczak over 10 years
    This is not a timestamp (time since epoch)
  • Michał Klimczak
    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
    Seynorth almost 10 years
    Not a timestamp, and moreover, not efficiant at all (creation of SimpleDateFormat, creation of date, conversion). Avoid it.
  • Admin
    Admin over 9 years
    That is not a timestamp.
  • Akash Agarwal
    Akash Agarwal over 8 years
    Just a note to @ana01 's comment that System.nanoTime() isn't suitable for to display wall clock time. For that purpose, use System.currentTimeMillis() instead.
  • Abbas
    Abbas about 8 years
    Hey, 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
    jason.kaisersmith about 8 years
    For future reference for anybody reading this be aware that "android.text.format.Time" is now deprecated
  • ersks
    ersks almost 8 years
    @Hits, It's Simple Date Format, not a timestamp. You are just using variable name as currentTimeStap.
  • ersks
    ersks almost 8 years
    You are answering sth else, but it's also valid.
  • kjdion84
    kjdion84 over 6 years
    This doesn't factor in the timezone offset of the device.
  • Slobodan Antonijević
    Slobodan Antonijević over 5 years
    Almost 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ԃաԃ
    Cԃաԃ almost 5 years
    @kjdion84 excuse me, but why do you think it would be important ? Just based on the question.
  • Basil Bourque
    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ć
    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.
    Ole V.V. over 4 years
    Good point, @SlobodanAntonijević. I have tried to make it just a little clearer.
  • Basil Bourque
    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
    nyxee almost 4 years
    This gives GMT time.
  • Donald Duck
    Donald Duck almost 3 years
    Is there a way to get this as int?