Tom*_*m G 7 unix android timestamp date
我今天一直试图找到这个问题的答案,并且有这么多矛盾的信息......
我想做的是在android中获取当前的unix时间戳,然后将其转换为允许我使用getHours()和getMinutes()的格式.
我现在正在这样做:
int time = (int) (System.currentTimeMillis());
Timestamp ts = new Timestamp(time);
mHour = ts.getHours();
mMinute = ts.getMinutes();
Run Code Online (Sandbox Code Playgroud)
但它并没有给我一个正确的小时或分钟值(当前东海岸时间13:33返回03:38).
这有效:
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
mHour = date.getHours();
mMinute = date.getMinutes();
Run Code Online (Sandbox Code Playgroud)
只需使用Java Calendar类.
Calendar c = new GregorianCalendar(); // This creates a Calendar instance with the current time
mHour = c.get(Calendar.HOUR);
mMinute = c.get(Calendar.MINUTE);
Run Code Online (Sandbox Code Playgroud)
另请注意,您的Android模拟器将返回GMT当前时间.我建议在真实设备上测试这种类型的代码.