Android工具類1-Time

 

Android工具類1-Timejava

public classandroid

Time 

extends  Object
java.lang.Object
   ↳ android.text.format.Time

Class Overview


An alternative to the Calendar and GregorianCalendar classes. An instance of the Time class represents a moment in time, specified with second precision. It is modelled after struct tm, and in fact, uses struct tm to implement most of the functionality.less

android中獲取時間不少時候更適合使用time類,請看一下它的public屬性
 
Fields
 
public boolean allDay True if this is an allDay event.
public long gmtoff Offset from UTC (in seconds).
public int hour Hour of day [0-23]
public int isDst This time is in daylight savings time.
public int minute Minute [0-59]
public int month Month [0-11]
public int monthDay Day of month [1-31]
public int second Seconds [0-61] (2 leap seconds allowed)
public String timezone The timezone for this Time.
public int weekDay Day of week [0-6]
public int year Year.
public int yearDay Day of year [0-365]
 
年月日, 時分秒, 時區, 一年第幾天, 星期幾,全都有了,不用本身去算了
 
寫一下樣例:
  1.  
  2. Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone資料。  
  3. t.setToNow(); // 取得系統時間。  
  4. String time=t.year+"年 "+(t.month+1)+"月 "+t.monthDay+"日 "+t.hour+"h "+t.minute+"m "+t.second+"s";
  5.   
其屬性不寫,相似
 
Public Methods
boolean after(Time that)
Returns true if the time represented by this Time object occurs after the given time.
 
若是比給定的時間晚則返回true,不然false
 
boolean before(Time that)
Returns true if the time represented by this Time object occurs before the given time.
 
after有點反人類了,仍是before舒服點
 
void clear(String timezone)
Clears all values, setting the timezone to the given timezone.
 
清楚全部值,時區設置爲指定時區
 
static int compare(Time a, Time b)
Compare two  Time objects and return a negative number if  a is less than  b, a positive number if  a is greater than  b, or 0 if they are equal.
 
靜態比較方法:
     a<b,return<0; 
     a>b,return>0;
     a=b,return 0;
 
String format(String format)
Print the current value given the format string provided.
 
format格式最多256char,舉個例子,format("%y%m%d%T%H%M%S");
 
String format2445()
Format according to RFC 2445 DATETIME type.
 
The same as format("%Y%m%dT%H%M%S").
 
String format3339(boolean allDay)
Return a string in the RFC 3339 format.
 
if(allDay){
     return  Y-M-D;
}else if(timezone=GMT){
     Y-M-D-T-H-M-S +- GMT
}else{
     Y-M-D-T-H-M-S UTC
}
 
int getActualMaximum(int field)
Return the maximum possible value for the given field given the value of the other fields.
 
這個沒搞懂有什麼用
 
static String getCurrentTimezone()
Returns the timezone string that is currently set for the device.
 
返回當前時區
 
static int getJulianMondayFromWeeksSinceEpoch(int week)                                                                                                                           Added in API level 11
Takes a number of weeks since the epoch and calculates the Julian day of the Monday for that week.

static int getWeeksSinceEpochFromJulianDay(int julianDay, int firstDayOfWeek)
Returns the week since  EPOCH_JULIAN_DAY (Jan 1, 1970) adjusted for first day of week.
 
侵略歷.....什麼的不懂
 
int getWeekNumber()
Computes the week number according to ISO 8601.
 
返回標準週數
 
static boolean isEpoch(Time time)
Returns true if the day of the given time is the epoch on the Julian Calendar (January 1, 1970 on the Gregorian calendar).
 
返回Time的時代
 
long normalize(boolean ignoreDst)
Ensures the values in each field are in range.
 
日期規範化,好比你試3月32號,它會規範成4月1號
 
boolean parse(String s)
Parses a date-time string in either the RFC 2445 format or an abbreviated format that does not include the "time" field.
Parses a date-time string in either the RFC 2445 format or an abbreviated format
解析一個字符串成爲一個標準格式或者一個RFC 2445格式
舉例
  • "20081013T160000Z"
  • "20081013T160000"
  • "20081013"
Time time =newTime();
String date ="20081013T160000Z";
time
.parse(date);
long millis = time.normalize(false);
boolean parse3339(String s)
Parse a time in RFC 3339 format.
 
  • "2008-10-13T16:00:00.000Z"
  • "2008-10-13T16:00:00.000+07:00"
  • "2008-10-13T16:00:00.000-07:00"
  • "2008-10-13"
 
void set(int second, int minute, int hour, int monthDay, int month, int year)
Sets the fields.
void set(int monthDay, int month, int year)
Sets the date from the given fields.
void set(Time that)
Copy the value of that to this Time object.
void set(long millis)
Sets the fields in this Time object given the UTC milliseconds.
long setJulianDay(int julianDay)
Sets the time from the given Julian day number, which must be based on the same timezone that is set in this Time object.
 
 
void setToNow()
Sets the time of the given Time object to the current time.
 
注意這個方法,若是你想獲得當前時間,這個方法卻必定要調用,不然你就不得不set了...
 
void switchTimezone(String timezone)
Convert this time object so the time represented remains the same, but is instead located in a different timezone.
 
轉換時區
 
long toMillis(boolean ignoreDst)
Converts this time to milliseconds.
 
時間轉化成秒
Time time =newTime();
time
.set(4,10,2007);// set the date to Nov 4, 2007, 12am
time
.normalize();// this sets isDst = 1
time
.monthDay +=1;// changes the date to Nov 5, 2007, 12am
millis
= time.toMillis(false);// millis is Nov 4, 2007, 11pm
millis
= time.toMillis(true);// millis is Nov 5, 2007, 12am
 
String toString()
Return the current time in YYYYMMDDTHHMMSS format
相關文章
相關標籤/搜索