我今天一直在试图find这个答案,这里有很多矛盾的信息….
我想要做的是在Android中获取当前的unix时间戳,然后将其转换为允许我getHours()和getMinutes()的格式。
我目前正在这样做:
int time = (int) (System.currentTimeMillis()); Timestamp ts = new Timestamp(time); mHour = ts.getHours(); mMinute = ts.getMinutes();
但是,它并没有给我一个小时或分钟的正确值(现在东部海岸时间是13:33,这个时间是03:38)。
这工作:
final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); Date date = cal.getTime(); mHour = date.getHours(); mMinute = date.getMinutes();
只需使用Java 日历类即可。
Calendar c = new GregorianCalendar(); // This creates a Calendar instance with the current time mHour = c.get(Calendar.HOUR); mMinute = c.get(Calendar.MINUTE);
另外请注意,您的Android模拟器将在GMT时间返回当前时间。 我建议在真实的设备上测试这种类型的代码。
看看这个。
希望这是你需要的。
Android简单的日期格式
祝你好运!!
int time = (int) (System.currentTimeMillis());
在这里你应该使用long而不是int 。
使用Time类形式Google它是最适合这种工作的专用它不像Calendar的性能好。