如何获取Android中的当前时间?如何获取当前时间
当我使用
int hours = java.sql.Time.this.getHours();
我得到的错误:
No enclosing instance of the type Time is accessible in scope
如何获取Android中的当前时间?如何获取当前时间
当我使用
int hours = java.sql.Time.this.getHours();
我得到的错误:
No enclosing instance of the type Time is accessible in scope
int hours = new Time(System.currentTimeMillis()).getHours();
谢谢PERFEKT !!! – 2010-08-31 16:38:40
System.currentTimeMillis()返回long并且Time没有很长的参数!对不起,但这个代码是错误的。 – mSafdel 2012-06-26 11:47:47
此代码是正确的,因为问题是关于具有适当构造函数的java.sql.Time。 – Thorstenvv 2012-09-05 15:00:57
如果你只是想在当前的时间戳,您可以使用:
long millis = System.currentTimeMillis()
还可以获得其它时间相关的值,如自上次启动运行时间或总时间(包括睡眠时间)从android.os.SystemClock
。
试试这个:
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
公共静态最终诠释HOUR_OF_DAY 自:API等级1 场数get和set表示一天中的小时。 HOUR_OF_DAY用于24小时制。例如,在10:04:15.250 PM的HOUR_OF_DAY为22
我最喜欢的样本:
Time dtNow = new Time();
dtNow.setToNow();
int hours = dtNow.hour;
String lsNow = dtNow.format("%Y.%m.%d %H:%M");
String lsYMD = dtNow.toString(); // YYYYMMDDTHHMMSS
Calendar cal = Calendar.getInstance(); // get current time in a Calendar
,那么你可以做很多与日历实例,如拿到小时纪要 - 这样的:
int hour = cal.get(Calendar.HOUR_OF_DAY);
这时候你要定位到许多地方建议,并在MULT打印数据iple格式,或者对日期进行操作。
这是目前getHours()/ getDate()等最好的答案贬值。 – Ravi 2016-06-04 06:50:07
给Andrew的回复添加一点点。如果您的时区处于夏令时模式,代码的后面部分会增加小时。 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 ;
}
}
看到这个职位http://stackoverflow.com/questions/2271131/display-current-time-and-date-in-android-application – Ally 2010-08-31 16:35:03