首页 文章

Android获取当前时间戳?

提问于
浏览
195

我想得到当前的时间戳: 1320917972

int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts =  tsTemp.toString();

11 回答

  • 74

    解决方案是:

    Long tsLong = System.currentTimeMillis()/1000;
    String ts = tsLong.toString();
    
  • 22

    来自开发者博客:

    System.currentTimeMillis() 是表示自纪元以来毫秒的标准"wall"时钟(时间和日期) . 挂钟可以由用户或电话网络设置(参见setCurrentTimeMillis(long)),因此时间可能会不可预测地向后或向前跳跃 . 只有在与实际日期和时间对应很重要时,例如在日历或闹钟应用程序中,才应使用此时钟 . 间隔或经过时间测量应使用不同的时钟 . 如果您正在使用 System.currentTimeMillis() ,请考虑收听 ACTION_TIME_TICKACTION_TIME_CHANGEDACTION_TIMEZONE_CHANGED Intent广播以查明时间的变化 .

  • 20

    1320917972 是Unix时间戳,使用自1970年1月1日00:00:00 UTC以来的秒数 . 您可以使用 TimeUnit class进行单位转换 - 从 System.currentTimeMillis() 到秒 .

    String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
    
  • 9

    您可以使用SimpleDateFormat类:

    SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
    String format = s.format(new Date());
    
  • 0

    使用以下方法获取当前时间戳 . 这对我来说可以 .

    /**
     * 
     * @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;
        }
    }
    
  • 236

    它很简单:

    long millis = new Date().getTime();
    

    如果你想要特定的格式,那么你需要像下面的格式化程序

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String millisInString  = dateFormat.format(new Date());
    
  • 1

    这是一个人类可读的时间戳,可以在文件名中使用,以防万一有人需要我需要的东西:

    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

    java.time

    我想提出现代的答案 .

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

    刚刚运行时的输出:

    1543320466

    虽然1000分的分数对许多人来说并不会让人感到意外,但是你自己的时间转换很难快速阅读,所以当你可以避免它时,这是一个坏习惯 .

    我正在使用的 Instant 类是java.time的一部分,java.time是现代Java日期和时间API . 它内置于新的Android版本,API级别26及更高版本 . 如果您使用较旧的Android进行编程,则可能会获得后端,请参阅下文 . 如果您不想这样做,可以理解,我仍然使用内置转换:

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

    这与sealskej的答案相同 . 输出与以前相同 .

    问题:我可以在Android上使用java.time吗?

    是的,java.time适用于较旧和较新的Android设备 . 它至少需要 Java 6 .

    • 在Java 8及更高版本和更新的Android设备上(来自API级别26),内置了现代API .

    • 在Java 6和7中获取ThreeTen Backport,新类的后端端口(适用于JSR 310的ThreeTen;请参见底部的链接) .

    • On(较旧)Android使用Android版的ThreeTen Backport . 它被称为ThreeTenABP . 并确保使用子包从 org.threeten.bp 导入日期和时间类 .

    链接

  • 0

    我建议使用Hits的答案,但添加Locale格式,这就是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;
        }
    
  • 8

    只是用

    long millis = new Date().getTime();
    

    获得长时间的当前时间

  • 27

    试试这些

    time.setText(String.valueOf(System.currentTimeMillis()));
    

    和timeStamp到时间格式

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    String dateString = formatter.format(new Date(Long.parseLong(time.getText().toString())));
    time.setText(dateString);
    

相关问题