Android Get Current timestamp?

前端 未结 12 835
灰色年华
灰色年华 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: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;
        }
    
    }
    

提交回复
热议问题