1. java.util.Date 类常用方法:
public Date()public Date(long date)
long getTime()
void setTime(long time)
boolean after(Date when)
boolean before(Date when)
int compareTo(Date anotherDate)
Object clone()
boolean equals(Object obj)
int hashCode()
String toString()
其他方法皆已过时。
2. 若想直接获取或者改变年、月、日、时、分、秒等等这些属性的值,用java.util.Calendar / GregorianCalendar:
Calendar calendar = Calendar.getInstance(); 或Calendar calendar = new GregorianCalendar();
Date getTime()
void setTime(Date date)
Date getTime()
void add(int field, int amount)
void roll(int field, boolean up)
void roll(int field, int amount)
推荐用常量Calendar.JULY来设置月份值,因为0是第一月
void set(int field, int value) //field: public static final int Calendar.DAY_OF_YEAR
void set(int year, int month, int date)
void set(int year, int month, int date, int hourOfDay, int minute)
void set(int year, int month, int date, int hourOfDay, int minute, int second)
GregorianCalendar()
GregorianCalendar(int year, int month, int dayOfMonth)
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute)
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)
3. 若要格式化输出或转化:
只用DateFormat的话,DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL);
String dateStr = df.format(new Date());
用SimpleDateFormat,
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String dateStr = df.format(new Date());
Date date = df.pase(“2001-07-04T12:08:56.235-0700”);