使用Android格式化字符串时间戳
问题描述:
由于某种原因,这让我把头发撕掉。使用Android格式化字符串时间戳
我有一个UNIX时间戳作为Android中的字符串。我想要做的就是格式化它,以便它返回用户的droid时区中的日期/时间。
我可以将它转换为时间戳就好,但它使用的是GMT而不是它们的本地化区域。
感谢
答
使用SimpleDateFormat
构造与Locale
您需要:
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
退房的日历和日期的Java类,它们含有本地化的工具。 – Dan 2011-05-05 21:08:04