使用Android格式化字符串时间戳

问题描述:

由于某种原因,这让我把头发撕掉。使用Android格式化字符串时间戳

我有一个UNIX时间戳作为Android中的字符串。我想要做的就是格式化它,以便它返回用户的droid时区中的日期/时间。

我可以将它转换为时间戳就好,但它使用的是GMT而不是它们的本地化区域。

感谢

+0

退房的日历和日期的Java类,它们含有本地化的工具。 – Dan 2011-05-05 21:08:04

使用SimpleDateFormat构造与Locale您需要:

http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat%28java.lang.String,%20java.util.Locale%29

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); 
Calendar c = Calendar.getInstance(); 
try { 
    Date dt = sdf.parse("2011-03-01 17:55:15"); 
    c.setTime(dt); 
    System.out.println(c.getTimeInMillis()); 
    System.out.println(dt.toString()); 
} catch (ParseException e) { 
    System.err.println("There's an error in the Date!"); 
} 

输出:

1299002115000 
Tue Mar 01 12:55:15 EST 2011