我是安卓新手。在 android 中,我只想在 TextView 中以 hh:mm am/pm 格式显示当前时间(例如:3:02 PM),我使用以下代码。
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
tv.setText(currentDateTimeString);
它给出了当前日期、月份、年份和时间。但我只需要从中获取当前时间。有没有办法只显示时间?谁能帮我从上面的代码中获取没有日期的时间。[使用 DateFormat.getDateTimeInstance()]
最佳答案
尝试下面的代码
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
String currentDateTimeString = sdf.format(d);
tv.setText(currentDateTimeString);
关于java - 如何在 android 中使用 DateFormat.getDateTimeInstance() 仅显示当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212608/