【发布时间】:2014-01-31 23:39:36
【问题描述】:
我需要在 android 中以字符串格式获取当前时间。我在 Time 类中看到了 toString 方法。有没有办法在android中使用时间类或对象来获取当前时间?如果没有,我怎样才能在android中以字符串形式获取当前时间?
【问题讨论】:
我需要在 android 中以字符串格式获取当前时间。我在 Time 类中看到了 toString 方法。有没有办法在android中使用时间类或对象来获取当前时间?如果没有,我怎样才能在android中以字符串形式获取当前时间?
【问题讨论】:
尝试做这样的事情
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()));
【讨论】:
有多种可能性:
自动使用用户喜欢的格式:
String dateString = DateFormat.getTimeFormat(thisContext).format(Calendar.getInstance().getTime())
使用您自己的格式:
String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
【讨论】:
我就是这样做的:
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(getBaseContext());
dateFormat.format(date);
【讨论】: