一、第一代日期类
1、Date:精确到毫秒,代表特定的瞬间
2、SimpleDateFormat:格式化和解析日期的类,它允许进行格式化(日期->文本),解析(文本->日期)和规范化
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Date01 {
public static void main(String[] args) throws ParseException {
//获取当前系统时间
//这里的Date类是在java.util包
//默认输出的日期格式是国外的方式,因此要对其进行格式转换
Date d1 = new Date();
System.out.println(d1);
Date date = new Date(999999999);//通过指定毫秒数得到时间
System.out.println(date);//获取某个时间对应的毫秒数
//创建 SimpleDateFormat 对象可以指定相应的格式
//格式使用的字母是规定好的,不能乱写
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");
String format = sdf.format(d1);//format() 将日期转换成指定格式的字符串
System.out.println(format);
//可以把一个格式化的字符串转换成对应的 Date
//得到的Date输出是还是国外形式,如果希望指定格式输出,需要转换
//在把String -> Date 使用的 sdf 格式需要和你给的String格式一样,否则会抛出异常
String s = "1999年02月02日 02:02:02 星期二";
Date parse = sdf.parse(s);
System.out.println(sdf.format(parse));
}
}
二、第二代日期类
1、第二代日期类,主要就是 Calendar 类(日历)
2、Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法
import java.util.Calendar;
public class Calendar01 {
public static void main(String[] args) {
//1.Calendar 是一个抽象类,并且构造器是 privater
//2.可以通过 getInstance() 来获取实例
//3.提供大量的方法和字段给程序员
Calendar c = Calendar.getInstance();//创建日历类对象,比较简单,自由
System.out.println(c);
//获取日历对象的某个日历字段.
System.out.println("年:" + c.get(Calendar.YEAR));
//因为 Calendar 返回月时从0开始编号 所以需要+1
System.out.println("月:" + (c.get(Calendar.MONTH)+1));
System.out.println("日:" + c.get(Calendar.DAY_OF_MONTH));
System.out.println("小时:" + c.get(Calendar.HOUR_OF_DAY));
System.out.println("分钟:" + c.get(Calendar.MINUTE));
System.out.println("秒:" + c.get(Calendar.SECOND));
//Calendar没有专门的格式化方法,所以需要程序员自己组合(更加灵活)
System.out.println(c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH)+1) + "月" +
c.get(Calendar.DAY_OF_MONTH) + "日 " + c.get(Calendar.HOUR_OF_DAY) + ":" +
c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND) + ":");
}
}
三、第三代日期类
前面两代日期类的不足
JDK1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK1.1引入Calendar类之后被弃用了。而Calendar也存在问题
1、可变性:像日期和时间这样的类应该是不可变的
2、偏移性:Date中的年份是从1900开始的,而月份都是从0开始
3、格式化:格式化只对Date有用,Calendar则不行
4、此外,它们也不是线程安全的,不能处理闰秒等(每隔2天,多出1秒)
第三代日期类的常见方法
1、LocalDate(日期)、LocalTime(时间)、LocalDateTime(日期时间) JDK8加入
LocalDate只包含日期,可以获取日期字段
LocalTime只包含时间,可以获取时间字段
LocalDateTime包含日期+时间,可以获取日期和字段时间
2、DateTimeFormatter:格式日期类
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTime_ {
public static void main(String[] args) {
//使用now() 返回表示当前日期时间的对象
LocalDateTime now = LocalDateTime.now();
LocalDate now1 = LocalDate.now();//获取年月日
LocalTime now2 = LocalTime.now();//获取时分秒
System.out.println(now);
//使用 DateTimeFormatter 对象来进行格式化
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String format = dTF.format(now);
System.out.println(format);
System.out.println("年:" + now.getYear());
System.out.println("月:" + now.getMonth());
System.out.println("月:" + now.getMonthValue());
System.out.println("日:" + now.getDayOfMonth());
System.out.println("时:" + now.getHour());
System.out.println("分:" + now.getMinute());
System.out.println("秒:" + now.getSecond());
}
}
3、Instant:时间戳,类似于Date,提供了一系列和Date类转换的方式 Instant ------> Date
import java.time.Instant;
import java.util.Date;
public class Instant_ {
public static void main(String[] args) {
//通过静态方法 now() 获取表示当前时间戳的对象
Instant now = Instant.now();
System.out.println(now);
//通过 form 可以把 Instant 转成 Date
Date from = Date.from(now);
//通过date.toInstant 可以把 Date 转成 Instant
Instant instant = from.toInstant();
}
}
4、第三代日期类的更多方法
LocalDtaeTime类
MonthDay类:检查重复事件
是否是闰年
增加日期的某个部分
使用plus方法测试增加时间的某个部分
使用minus方法测试查看一年前和一年后的日期
其他更多方法查看API手册
package com.javase.date_;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTime_ {
public static void main(String[] args) {
//使用now() 返回表示当前日期时间的对象
LocalDateTime now = LocalDateTime.now();
LocalDate now1 = LocalDate.now();//获取年月日
LocalTime now2 = LocalTime.now();//获取时分秒
System.out.println(now);
//使用 DateTimeFormatter 对象来进行格式化
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String format = dTF.format(now);
System.out.println(format);
//通过 plus 和 minus 方法可以对当前的时间进行加或减
//查看888天后的日期
LocalDateTime localDateTime = now.plusDays(888);
System.out.println("888天后:" + dTF.format(localDateTime));
//查看888天前的日期
LocalDateTime localDateTime1 = now.minusDays(888);
System.out.println("888天前:" + dTF.format(localDateTime1));
}
}