当我打印System.currentTimeMillis()
时
给我:
11-03 14:47:05.400: INFO/System.out(7579): date is :: 14475410111
获取完整日期和时间的正确步骤是什么。?
要在Android中获取当前日期和时间,您可以使用:
Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
输出:
当前时间=>2011年11月3日星期四15:00:45 GMT+05:30
仅供参考,一旦在"c"对象中有了这个时间,就可以使用SimpleDateFormat类以所需的格式获取日期/时间。
最终解决方案:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sring formattedDate = df.format(c.getTime()); // c is Calendar object
System.out.println("========> formatted date => "+formattedDate);
输出:=====>格式化日期=>2011-11-03 15:13:37
Date now = new Date(System.currentTimeMillis());
顺便说一下:currentTimeMillis()
返回自1970年1月1日00:00:00 UTC以来的当前系统时间(以毫秒为单位(。
public static String getDateFromTimeStamp(String timeStamp){
return new Date(Long.parseLong(timeStamp)).toString();
}
使用Date类。
(你为什么认为这是错误的?你要求的是以毫秒为单位的时间。(
使用SimpleDateFormat