1.UTC & GTM
GMT就是格林尼治标准时间,它是Greenwich mean time的简称。
UTC则是协调世界时间,它是coordinated universal time的时间。它是一种更精确的GMT。
2. java.util.Date类
Date类的时间实质是通过计算出来的,其实它叫做DateTime更合适,它的时间已经能精确表示到秒。它的构造方法
/**
* Allocates a <code>Date</code> object and initializes it so that
* it represents the time at which it was allocated, measured to the
* nearest millisecond.
*
* @see java.lang.System#currentTimeMillis()
*/
public Date() {
this(System.currentTimeMillis());
}
/**
* Allocates a <code>Date</code> object and initializes it to
* represent the specified number of milliseconds since the
* standard base time known as "the epoch", namely January 1,
* 1970, 00:00:00 GMT.
*
* @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
* @see java.lang.System#currentTimeMillis()
*/
public Date(long date) {
fastTime = date;
}
目前不建议使用Date中的getDate、getYea等方法。以Calendar类来替代。
3. java.util.Calendar类
calendar直译为日历,它做为Date类的补充,下面以一段代码来说明下calendar的应用。
<span style="white-space:pre"> </span>Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);//获取年份
int month = cal.get(Calendar.MONTH);//获取月份
int day = cal.get(Calendar.DATE);//获取日
int hour = cal.get(Calendar.HOUR);//小时
int minute = cal.get(Calendar.MINUTE);//分
int second = cal.get(Calendar.SECOND);//秒
Date time = cal.getTime(); // 获取当前时间
4. Calendar与Date相互转换
1)Calendar转换为Date
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
2)Date转换为Calendar
Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.setTime(date);