Android 如何获取当前时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3611059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the current Time
提问by Hans Wurst
How to get the current time in Android?
如何在Android中获取当前时间?
When i use
当我使用
int hours = java.sql.Time.this.getHours();
i get the error:
我收到错误:
No enclosing instance of the type Time is accessible in scope
回答by Thorstenvv
int hours = new Time(System.currentTimeMillis()).getHours();
回答by user223697
Try this:
尝试这个:
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
public static final int HOUR_OF_DAY Since: API Level 1 Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.
public static final int HOUR_OF_DAY 自:API 级别 1 用于获取和设置的字段编号,指示一天中的小时。HOUR_OF_DAY 用于 24 小时制。例如,在晚上 10:04:15.250,HOUR_OF_DAY 是 22。
回答by JohnnyLinTW
My favorite sample:
我最喜欢的样品:
Time dtNow = new Time();
dtNow.setToNow();
int hours = dtNow.hour;
String lsNow = dtNow.format("%Y.%m.%d %H:%M");
String lsYMD = dtNow.toString(); // YYYYMMDDTHHMMSS
回答by Erich Douglass
If you just want the current timestamp, you can use:
如果您只想要当前时间戳,可以使用:
long millis = System.currentTimeMillis()
You can also get other time related values such as the uptime or total elapsed time since the last boot (including sleep time) from android.os.SystemClock.
您还可以从android.os.SystemClock.
回答by tobsen
回答by Andrew Mackenzie
Calendar cal = Calendar.getInstance(); // get current time in a Calendar
Calendar cal = Calendar.getInstance(); // get current time in a Calendar
then you can do lots with the Calendar instance, such as get the Hours or the Minutes - like:
那么你可以用 Calendar 实例做很多事情,比如获取小时或分钟 - 比如:
int hour = cal.get(Calendar.HOUR_OF_DAY);
int hour = cal.get(Calendar.HOUR_OF_DAY);
This is recommended when you have to localize to many locales, and print data in multiple formats, or do operations on dates.
当您必须本地化到许多语言环境、以多种格式打印数据或对日期进行操作时,建议使用此方法。
回答by Brian Fleming
Just adding a little to Andrew's reply. The later part of the code increments the hour if your time zone is in daylight savings mode. The HOUR_OF_DAY is in 24 hour format.
只是在安德鲁的回复中添加一点。如果您的时区处于夏令时模式,则代码的后面部分会增加小时数。HOUR_OF_DAY 采用 24 小时格式。
Calendar currentTime = Calendar.getInstance() ;
int hour = currentTime.get(Calendar.HOUR_OF_DAY) ;
int minute = currentTime.get(Calendar.MINUTE) ;
int second = currentTime.get(Calendar.SECOND) ;
long milliDiff = currentTime.get(Calendar.ZONE_OFFSET) ;
// Got local offset, now loop through available timezone id(s).
String [] ids = TimeZone.getAvailableIDs() ;
for (String id : ids)
{
TimeZone tz = TimeZone.getTimeZone(id) ;
if (tz.getRawOffset() == milliDiff)
{ // Found a match, now check for daylight saving
boolean inDs = tz.inDaylightTime(new Date()) ;
if (inDs) { hour += 1 ; }
if (hour == 25) { hour = 1 ; }
break ;
}
}

