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
7 个解决方案
#1
13
int hours = new Time(System.currentTimeMillis()).getHours();
#2
10
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.
公共静态最终int HOUR_OF_DAY Since: API级别1的get字段号,并设置为一天的小时数。小时/天用于24小时的时钟。例:在晚上10点15分25分,每天的小时数是22。
#3
5
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
#4
4
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获得与时间相关的其他值,例如自上一次引导(包括睡眠时间)以来的正常运行时间或总运行时间。
#5
#6
2
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.
只是给安德鲁的回复加一点。如果您的时区处于日光节约模式,代码的后面部分将增加小时数。工时/ 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 ;
}
}
#7
1
Calendar cal = Calendar.getInstance(); // get current time in a Calendar
日历卡尔= Calendar.getInstance();//在日历中获取当前时间
then you can do lots with the Calendar instance, such as get the Hours or the Minutes - like:
然后你可以使用日历实例做很多事情,比如获取小时或分钟——比如:
int hour = cal.get(Calendar.HOUR_OF_DAY);
int小时= 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.
当您必须本地化到许多地区,并以多种格式打印数据,或者在日期上执行操作时,建议使用这种方法。
#1
13
int hours = new Time(System.currentTimeMillis()).getHours();
#2
10
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.
公共静态最终int HOUR_OF_DAY Since: API级别1的get字段号,并设置为一天的小时数。小时/天用于24小时的时钟。例:在晚上10点15分25分,每天的小时数是22。
#3
5
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
#4
4
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获得与时间相关的其他值,例如自上一次引导(包括睡眠时间)以来的正常运行时间或总运行时间。
#5
#6
2
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.
只是给安德鲁的回复加一点。如果您的时区处于日光节约模式,代码的后面部分将增加小时数。工时/ 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 ;
}
}
#7
1
Calendar cal = Calendar.getInstance(); // get current time in a Calendar
日历卡尔= Calendar.getInstance();//在日历中获取当前时间
then you can do lots with the Calendar instance, such as get the Hours or the Minutes - like:
然后你可以使用日历实例做很多事情,比如获取小时或分钟——比如:
int hour = cal.get(Calendar.HOUR_OF_DAY);
int小时= 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.
当您必须本地化到许多地区,并以多种格式打印数据,或者在日期上执行操作时,建议使用这种方法。