Android工具类1-Time

Android工具类1-Time

public class

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.

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
 
原文地址:https://www.cnblogs.com/haoxiqiang/p/3670287.html