Android Get Current timestamp?

前端 未结 12 740
灰色年华
灰色年华 2020-11-27 09:53

I want to get the current timestamp like that : 1320917972

int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(t         


        
相关标签:
12条回答
  • 2020-11-27 10:26

    I suggest using Hits's answer, but adding a Locale format, this is how Android Developers recommends:

    try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            return dateFormat.format(new Date()); // Find todays date
        } catch (Exception e) {
            e.printStackTrace();
    
            return null;
        }
    
    0 讨论(0)
  • 2020-11-27 10:30

    You can use the SimpleDateFormat class:

    SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
    String format = s.format(new Date());
    
    0 讨论(0)
  • 2020-11-27 10:34

    The solution is :

    Long tsLong = System.currentTimeMillis()/1000;
    String ts = tsLong.toString();
    
    0 讨论(0)
  • 2020-11-27 10:34

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 10:35

    java.time

    I should like to contribute the modern answer.

        String ts = String.valueOf(Instant.now().getEpochSecond());
        System.out.println(ts);
    

    Output when running just now:

    1543320466

    While division by 1000 won’t come as a surprise to many, doing your own time conversions can get hard to read pretty fast, so it’s a bad habit to get into when you can avoid it.

    The Instant class that I am using is part of java.time, the modern Java date and time API. It’s built-in on new Android versions, API level 26 and up. If you are programming for older Android, you may get the backport, see below. If you don’t want to do that, understandably, I’d still use a built-in conversion:

        String ts = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
        System.out.println(ts);
    

    This is the same as the answer by sealskej. Output is the same as before.

    Question: Can I use java.time on Android?

    Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

    • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
    • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
    • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    0 讨论(0)
  • 2020-11-27 10:39

    Here's a human-readable time stamp that may be used in a file name, just in case someone needs the same thing that I needed:

    package com.example.xyz;
    
    import android.text.format.Time;
    
    /**
     * Clock utility.
     */
    public class Clock {
    
        /**
         * Get current time in human-readable form.
         * @return current time as a string.
         */
        public static String getNow() {
            Time now = new Time();
            now.setToNow();
            String sTime = now.format("%Y_%m_%d %T");
            return sTime;
        }
        /**
         * Get current time in human-readable form without spaces and special characters.
         * The returned value may be used to compose a file name.
         * @return current time as a string.
         */
        public static String getTimeStamp() {
            Time now = new Time();
            now.setToNow();
            String sTime = now.format("%Y_%m_%d_%H_%M_%S");
            return sTime;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题