一:第一代日期类

- Date: 精确到毫秒,代表特定的时间
SimpleDateFormat: 格式化和解析日期的具体类。他允许进行格式化(日期 -> 文本)和规范化
public class Test00 {public static void main(String[] args) {//1: 获取当前的时间//2: 这里的Date类是在Java.util包//3: 默认输出的日期格式是国外的方式,因此通常需要对格式进行装换Date time = new Date();System.out.println(time);//1: 创建 SimpleDateFormat 对象,可以指定相应的格式//2. 这里的格式使用的字母是规定好,不能乱写//3: 先指定格式,然后将time放到指定的格式里面用字符输出SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");String format = simpleDateFormat.format(time);System.out.println(format);//可以把一个格式化的 String 转成对应的 DateString s = "1996年01月01日 10:20:30 星期一";//转回外国的形式Date time2 = simpleDateFormat.parse(s);System.out.println(time2);//再转回来System.out.println(simpleDateFormat.format(time2));}}
二:第二代日期类
第二代日期类,主要就是Canlendar类( 日历 )
Calendar类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH.DAY_OF MONTH、HOUR等日历室段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
public class Test01 {public static void main(String[] args) {//1. Calendar 是一个抽象类, 并且构造器是 private//2. 可以通过 getInstance() 来获取实例Calendar calendar = Calendar.getInstance();//直接输入的话,会包含全部的字段,并且看不懂System.out.println(calendar);//获取日历对象的某个日历字段System.out.println("年:" + calendar.get(Calendar.YEAR));System.out.println("月:" + (calendar.get(Calendar.MONTH) + 1));System.out.println("日:" + calendar.get(Calendar.DAY_OF_MONTH));System.out.println("小时:" + calendar.get(Calendar.HOUR));System.out.println("分钟:" + calendar.get(Calendar.MINUTE));System.out.println("秒:" + calendar.get(Calendar.SECOND));//也可以自己组合显示内容System.out.println(calendar.get(Calendar.YEAR) + "-"+ (calendar.get(Calendar.MONTH) + 1)+ "-"+ calendar.get(Calendar.DAY_OF_MONTH));}}
:::info 存在的问题是:
可变性:像日期和时间这样的类应该是不可变的
便宜性:Date中的年份是从1900开始的,而月份都从0开始。
格式化:格式化只对Date有用,Calendar则不行。
此外,它们也不是线程安全的;不能处理闰秒等(每隔2天,多出1s)。 :::三:第三代日期类

public class Test01 {public static void main(String[] args) {//1. 使用 now() 返回表示当前日期时间的 对象LocalDateTime time = LocalDateTime.now();System.out.println(time);LocalDate time2 = LocalDate.now();System.out.println(time2);LocalTime time3 = LocalTime.now();System.out.println(time3);System.out.println("年=" + time.getYear());System.out.println("月=" + time.getMonth());System.out.println("月=" + time.getMonthValue());System.out.println("日=" + time.getDayOfMonth());System.out.println("时=" + time.getHour());System.out.println("分=" + time.getMinute());System.out.println("秒=" + time.getSecond());}}
四:DateTimeFormatter格式日期类
public class Test00 {public static void main(String[] args) {LocalDateTime time = LocalDateTime.now();//首先创建一个DateTimeFormatter对象DateTimeFormatter dateTimeFormatter =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//将时间转换成指定的格式,然后用字符船接收String Time1 = dateTimeFormatter.format(time);System.out.println(Time1);}}
五:Instant时间戳
public class Test01 {public static void main(String[] args) {//1.通过 静态方法 now() 获取表示当前时间戳的对象Instant now = Instant.now();System.out.println(now);//2.通过from可以把Instant转成DateDate date = Date.from(now);//3. 通过 date 的 toInstant() 可以把 date 转成 Instant 对象Instant instant = date.toInstant();System.out.println(instant);}}
六:第三代日期类的更多方法
public class Test01 {public static void main(String[] args) {//提供 plus 和 minus 方法可以对当前时间进行加或者减//看看 890 天后,是什么时候 把 年月日-时分秒LocalDateTime localDateTime1 = localDateTime.plusDays(890);System.out.println("890 天后= " + localDateTime);//看看在 3456 分钟前是什么时候,把 年月日-时分秒输出LocalDateTime localDateTime2 = localDateTime.minusMinutes(3456);System.out.println("3456 分钟前 日期= " + localDateTime2);}}
