【问题标题】:How to display only current time using DateFormat.getDateTimeInstance() in android如何在 android 中使用 DateFormat.getDateTimeInstance() 仅显示当前时间
【发布时间】:2013-11-26 09:10:35
【问题描述】:

我是安卓新手。在 android 中,我只想在 textview 中以 hh:mm am/pm 格式显示当前时间(例如:3:02 PM)我使用以下代码。

String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date()); tv.setText(currentDateTimeString);

它给出了当前日期、月份、年份和时间。但我只需要从中获取当前时间。有没有办法只显示时间?谁能帮我从上面的代码中获取没有日期的时间。[使用 DateFormat.getDateTimeInstance()]

【问题讨论】:

  • 你想在文本视图中显示当前时间还是编辑文本?

标签: java android date-format date-formatting


【解决方案1】:

试试下面的代码

     Date d=new Date();
     SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
     String currentDateTimeString = sdf.format(d);
     tv.setText(currentDateTimeString);

【讨论】:

  • 是的,这正是我需要的。谢谢
  • 这是 24 小时还是 12 小时?
  • 12 小时...这就是为什么我用 'a' 表示上午/下午
【解决方案2】:

它给出了当前的日期、月份、年份和时间,但是 我只需要从中获取当前时间。

如果您想这样做尊重用户的偏好(例如 12 小时制还是 24 小时制),请继续使用 DateFormat,就像这样:

DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());

【讨论】:

  • 对不起,我错误地否决了这个答案。实际上,这是唯一正确的答案。
  • @AlexeyVMP 谢谢,我已经编辑,所以你现在应该可以删除不赞成或反对。
  • 这应该是公认的答案!感谢分享。
【解决方案3】:

试试这个:

<DigitalClock
        android:id="@+id/digitalClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DigitalClock" />

这是android的基本控制。

【讨论】:

    【解决方案4】:
     startTime = ""+System.currentTimeMillis();
                        StringTokenizer tk = new StringTokenizer(startTime.trim());
                        String date = tk.nextToken();  
                        String time = tk.nextToken();
    
                        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
                        SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
                        Date dt;
                        try {    
                            dt = sdf.parse(time);
                            System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    

    【讨论】:

      【解决方案5】:
      System.out.println(new SimpleDateFormat("HH:mm:SS").format(new Date()));
      

      Android 时间选择器组件点击链接http://developer.android.com/guide/topics/ui/controls/pickers.html

      更多信息

      http://developer.android.com/reference/java/text/SimpleDateFormat.html

      【讨论】:

        【解决方案6】:

        它给出了当前的日期、月份、年份和时间。但我只需要得到 从现在开始的当前时间。

        java.util.Date 对象不像modern Date-Time types 那样是真正的日期时间对象;相反,它表示自称为“纪元”的标准基准时间以来的毫秒数,即January 1, 1970, 00:00:00 GMT(或 UTC)。当您打印java.util.Date 的对象时,它的toString 方法会返回JVM 时区中的日期时间,从这个毫秒值计算得出。如果您需要在不同的时区打印日期时间,则需要将时区设置为 SimpleDateFormat 并从中获取格式化字符串,例如

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
        sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        System.out.println(sdf.format(date));
        

        为了获得准确的时间,您只需将 (a) yyyy-MM-dd'T'HH:mm:ss.SSSXXX 替换为 hh:mm a 和 (b) America/New_York 替换为适用的时区。

        但是,java.util 日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用,改用modern Date-Time API*

        使用现代 API java.time 的解决方案:

        import java.time.LocalTime;
        import java.time.ZoneId;
        import java.time.format.DateTimeFormatter;
        import java.util.Locale;
        
        public class Main {
            public static void main(String[] args) {
                // Replace JVM's timezone i.e. ZoneId.systemDefault() with the applicable
                // timezone e.g. ZoneId.of("Europe/London")
                LocalTime time = LocalTime.now(ZoneId.systemDefault());
        
                // Print the default format i.e. the value of time.toString()
                System.out.println(time);
        
                // Custom format
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
                String formattedTime = dtf.format(time); // Alternatively, time.format(dtf)
                System.out.println(formattedTime);
            }
        }
        

        输出:

        11:59:07.443868
        11:59 AM
        

        Trail: Date Time了解更多关于java.timemodern Date-Time API*的信息。


        * 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7 . 如果您正在为一个 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

        【讨论】:

          猜你喜欢
          • 2020-12-25
          • 1970-01-01
          • 2021-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-03
          • 1970-01-01
          • 2022-08-20
          相关资源
          最近更新 更多