【问题标题】:How to fetch time using Time class in Android? [duplicate]如何在 Android 中使用 Time 类获取时间? [复制]
【发布时间】:2014-01-31 23:39:36
【问题描述】:

我需要在 android 中以字符串格式获取当前时间。我在 Time 类中看到了 toString 方法。有没有办法在android中使用时间类或对象来获取当前时间?如果没有,我怎样才能在android中以字符串形式获取当前时间?

【问题讨论】:

    标签: android time


    【解决方案1】:

    尝试做这样的事情

    Time time = new Time();
    
        System.out.println("time = " + time);
        System.out.println("time.toHour() " + time.toHour());
        // etc..
    
        // Test with a supplied value
        Time time2 = new Time(12033312L);
        System.out.println("time2 = " + time2); 
    

    还有

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
           //get current date time with Date()
           Date date = new Date();
           System.out.println(dateFormat.format(date));
    
           //get current date time with Calendar()
           Calendar cal = Calendar.getInstance();
           System.out.println(dateFormat.format(cal.getTime()));
    

    【讨论】:

      【解决方案2】:

      有多种可能性:

      1. 自动使用用户喜欢的格式:

        String dateString = DateFormat.getTimeFormat(thisContext).format(Calendar.getInstance().getTime())
        
      2. 使用您自己的格式:

        String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()); 
        

      【讨论】:

        【解决方案3】:

        我就是这样做的:

        Date date = new Date();
        java.text.DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getBaseContext());
        dateFormat.format(date);
        

        【讨论】:

          猜你喜欢
          • 2021-07-29
          • 2016-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-10
          • 2013-08-02
          相关资源
          最近更新 更多