日期和时间类
1. 概述
Date类在java.util.Date这个包中
Date类表示特定的瞬间,精确到毫秒,1秒 = 1000毫秒
时间原点(0毫秒):1970年1月1日 00:00:00(英国格林尼治时间 GMT),但是我们中国属于东八区,所以我们的时间原点要加上8个小时,应该是:1970年1月1日 08:00:00。
当前的时间点(毫秒值):计算从时间原点到现在一共经历了多少毫秒,可以用方法System.currentTimeMillis()
,它的值是long类型的
public static void main(String[] args) {
System.out.println(System.currentTimeMillis()); //1564188624026
}
2. Date类
-
2.1 构造函数:
-
无参构造Date(),获取当前系统的日期和时间
public static void main(String[] args) { Date date = new Date(); System.out.println(date); //Sat Jul 27 09:01:39 CST 2019 }
直接打印无参构造出来的对象date,结果是
Sat Jul 27 09:01:39 CST 2019
,含义是2019年7月27日 星期六 9点1分39秒,其中的CST是 China Standard Time(中国标准时间,一般是北京时间)。 -
有参构造Date (long date),传进去一个毫秒值,它会打印出从时间原点开始经历了该毫秒值之后的日期是多少,比如我传一个 0 毫秒进去,它打印出来的应该是中国标准时间的时间原点
public static void main(String[] args) { Date date = new Date(0L); System.out.println(date); //Thu Jan 01 08:00:00 CST 1970 }
打印的是1970年1月1日 星期四 8时0分0秒 中国标准时间,
-
-
2.2 常用方法:
-
getTime(),返回值是自时间原点以来,date对象经历的毫秒值,和
System.currentTimeMillis()
有着一样的作用public static void main(String[] args) { Date date = new Date(); long time = date.getTime(); //返回值是long类型的 System.out.println(time); //1564190172860 System.out.println(System.currentTimeMillis()); //1564190172860 }
注意,我这里的date对象是空参构造出来的,这样date对象代表的是当前的日期,date.getTime()得到的是当前日期所代表的的毫秒数;如果我用有参构造,比如0L来构造date,那么date.getTime()得到的将会是0。
-
3. DateFormat类
DateFormat类在java.text.DateFormat包中
DateFormat类,Date 日期,Format 格式化,所以这个类是用来做日期和文本间的转换、格式化等工作,也就是在Date对象和String对象之间进行来回转换
格式化:按照规定的格式,从Date对象转换为String对象
解析:按照规定的格式,从String对象转换为Date对象
3.1 构造函数:
注意:DateFormat类是一个抽象类,所以不能用它来直接构造对象,但是我们可以用它的子类SimpleDateFormat来构造
-
SimpleDateFormat (String pattern) 用给定的模式来构造
模式中的主要字符的含义:
y 年
M 月
d 日
H 时
m 分
s 秒
这些符号是不可以改变的,但是我们可以用它们来随意的组合
比如定义的模式是“yyyy-MM-dd HH:mm:ss”或者“yyyy年MM月dd日 HH时mm分ss秒”
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); //------------或者---------- SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
3.2 常用方法
-
public String format(Date date) 格式化(日期 ----> 文本)
按照指定的模式,将date对象代表的日期格式化为符合模式的字符串
public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date date = new Date(); // 把date对象格式化为模式的前后对比一下 //没有格式化,直接将date对象打印 System.out.println(date); // Sat Jul 27 09:55:37 CST 2019 //格式化为我们指定的模式:yyyy年MM月dd日 HH时mm分ss秒,用String来接收 String text = sdf.format(date); System.out.println(text); // 2019 年07 月27 日 09 时55 分37 秒 }
-
public Date parse (String source) throws ParseException 解析 (文本 ----> 日期)
和format方法刚好反了过来,注意:parse方法声明了一个异常叫ParseException
如果传进来的字符串和构造方法的模式不一样,那么程序就会抛出异常
调用一个抛出异常的方法,就必须处理这个异常,要么继续throws,要么自己try…catch,我们这里就继续throws吧
public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date date = new Date(); //按照我们的模式来定义一个字符串"2019年08月01日 08时08分08秒" String text = new String("2019年08月01日 08时08分08秒"); //将传进来的字符串解析为date日期 date = sdf.parse(text); //打印date对象 System.out.println(date); // Thu Aug 01 08:08:08 CST 2019 }
4. Calendar类
calendar类,一看字面就知道它肯定与日历有关
Calendar类为特定的瞬间(毫秒值)与 一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
Calendar类是抽象类,不能直接构造对象,但是它提供了一个静态方法getInstance,该方法可获得Calendar类的通用对象
Calendar rightNow = new Calendar.getInstance();
4.1 常用方法
public int get(int field); 返回给定日历字段的值
public void set(int field, int value); 将给定的日历字段设置为给定值
public abstract void add(int field, int amount);根据日历的规则,为给定的日历字段添加或减去指定的时间量
public Date getTime(); 返回一个表示此Calendar的时间值(从历元到现在的毫秒偏移量)的Date对象
这几个方法里面几乎都有一个参数叫int field,field就是字段,可用一个数字表示,在Calendar类里面有些特定的数字被赋予特定的含义。下面列举出部分Calendar类的常用静态成员变量:
public static final int YEAR = 1; 年
public static final int MONTH = 2;月
public static final int DAY_OF_MONTH = 5; 月中的某一天
public static final int HOUR = 10;时
public static final int MINUTE = 10; 分
public static final int SECOND = 13; 秒
我们下面开始对所提到的四个方法分别举例
- 4.2 public int get(int field); 返回给定日历字段的值
public static void main(String[] args){
//获取Calendar类的通用对象
Calendar calendar = Calendar.getInstance();
//获取年
int year = calendar.get(Calendar.YEAR); //也可以写calendar.get(1)
//获取月
int month = calendar.get(calendar.MONTH);
//获取日
int day = calendar.get(calendar.DAY_OF_MONTH);
//把年月日打印一下
System.out.println(year + "年" + month + "月" + day + "日"); //2019年6月27日
}
打印出来的是2019年6月27日,不应该是7月27日吗?
注意:西方的月份是从0开始数的,最多只有11月份,这套方法用的是西方的日历
-
4.3 public void set(int field, int value); 将给定的日历字段设置为给定值
还可以一次传三个参数,函数重载,同时设置年月日
public static void main(String[] args){
//获取Calendar类的通用对象
Calendar calendar = Calendar.getInstance();
//设置成1997年
calendar.set(Calendar.YEAR, 1997);
//把设置过的年打印一下
System.out.println(calendar.get(1) + "年");
//一次性设置成1998年8月1日
calendar.set(1998,8,1);
System.out.println(calendar.get(1) + "年" + calendar.get(2) + "月" + calendar.get(5) + "日");
}
- 4.4 public abstract void add(int field, int amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量
public static void main(String[] args){
//获取Calendar类的通用对象
Calendar calendar = Calendar.getInstance();
//把现在的年打印一下
System.out.println(calendar.get(1) + "年"); //2019
//正数增加,负数就是减少,那我增加两年
calendar.add(calendar.YEAR, 2);
//把现在的年打印一下
System.out.println(calendar.get(1) + "年"); //2021
}
- 4.5 public Date getTime(); 返回一个表示此Calendar的时间值(从历元到现在的毫秒偏移量)的Date对象
public static void main(String[] args){
//获取Calendar类的通用对象
Calendar calendar = Calendar.getInstance();
//新建一个Date类对象
Date date = new Date();
//把日历里面的字段信息转换成Date类的格式
date = calendar.getTime();
//打印date
System.out.println(date); // Sat Jul 27 13:02:14 CST 2019
//对比一下,我直接打印calendar对象,是什么结果
System.out.println(calendar); //额,是一堆很长的结果,包括了变量名、年月日时分秒
}