Java 如何在android中使用DateFormat.getDateTimeInstance()仅显示当前时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20212608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display only current time using DateFormat.getDateTimeInstance() in android
提问by liya
I am new to android. In android I want to display only current time in a textview in the format hh:mm am/pm (eg: 3:02 PM) I use the following code.
我是安卓新手。在android中,我只想以hh:mm am/pm(例如:3:02 PM)格式在textview中显示当前时间,我使用以下代码。
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
tv.setText(currentDateTimeString);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
tv.setText(currentDateTimeString);
It gives the current date,month,year and time.But i need to get only the current time from this. Is there any way to display only time? Can anyone help me to get the time without date from the above code.[using DateFormat.getDateTimeInstance()]
它给出了当前日期、月份、年份和时间。但我只需要从中获取当前时间。有没有办法只显示时间?谁能帮我从上面的代码中获取没有日期的时间。[使用 DateFormat.getDateTimeInstance()]
采纳答案by Ketan Ahir
try following code
尝试以下代码
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
String currentDateTimeString = sdf.format(d);
tv.setText(currentDateTimeString);
回答by Bhanu Sharma
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();
}
回答by Rajiv yadav
try this:
尝试这个:
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DigitalClock" />
this is basic control of android.
这是android的基本控制。
回答by Mohammod Hossain
System.out.println(new SimpleDateFormat("HH:mm:SS").format(new Date()));
Android Time picker component follow the link http://developer.android.com/guide/topics/ui/controls/pickers.html
Android 时间选择器组件按照链接http://developer.android.com/guide/topics/ui/controls/pickers.html
for more info
欲了解更多信息
http://developer.android.com/reference/java/text/SimpleDateFormat.html
http://developer.android.com/reference/java/text/SimpleDateFormat.html
回答by weston
It gives the current date, month, year and time, but I need to get only the current time from this.
它给出了当前的日期、月份、年份和时间,但我只需要从中获取当前时间。
If you want to do this andrespect the users preferences (e.g. 12 vs 24 hour clock) then continue to use DateFormat
, only like so:
如果您想这样做并尊重用户的偏好(例如 12 小时制与 24 小时制),请继续使用DateFormat
,就像这样:
DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());