Java8之前的日期相关类
System类的概述
基本概念:java.lang.System类中提供了一些类字段和方法
常用方法:
static long currentTimeMillis() 返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差
public class SystemTest {public static void main(String[] args) {//1.获取当前系统时间距离1970年1月1日0分0秒的参数long msec = System.currentTimeMillis();System.out.println("当前系统时间距离好描述为:"+msec);//通常用于测试某一段代码的执行效率}}
Date类的概述
基本概念:java.util.Date类主要用于描述特定的时间,年月日时分秒,可以精确到毫秒
常用的方法:
| Date() | 使用无参的方式构造对象,也就是当前系统时间 |
|---|---|
| Date(long date) | 根据参数指定毫秒数构造对象, 参数为距离1970年1月1日0时0分0秒的毫秒数 |
| long getTime() | 获取调用对象距离1970年1月1日0时0分0秒的毫秒数 |
| void setTime(longtime) | 设置调用对象为距离基准时间time毫秒的时间点 |
package com.lagou.task13;import java.util.Date;/*** @author lijing* @date 2020/9/30 14:05* @description*/public class DateTest {public static void main(String[] args) {//1.使用无参方式构造Date对象并打印Date d1 = new Date();System.out.println(d1);System.out.println("------------");long mses = System.currentTimeMillis();Date d2 = new Date(mses);System.out.println(d2);System.out.println("------------");//获取调用对象距离1970年1月1日0时0分0秒的好描述long time = d1.getTime();System.out.println(time);System.out.println("------------");//设置调用对象所表示的时间点作为参数指定的好描述d2.setTime(2000);System.out.println(d2);//1970 1 1 08 00 02}}
SimpleDateFormat类的概述
基本概念:java.text.SimpleDateFormat类主要用于实现日期和文本之间的转换
| SimpleDateFormat() | 使用无参方式构造对象 |
|---|---|
| SimpleDateFormat(String pattern) | 根据参数指定的模式来构造对象,模式主要有: y-年 M-月 d-日H-时 m-分 s-秒 |
| final String format(Date date) | 用于将日期类型转换为文本类型 |
| Date parse(String source) | 用于将文本类型转换为日期类型 |
public class SimpleDateFormatTest {public static void main(String[] args) throws ParseException {//1.获取当前系统时间Date date = new Date();System.out.println(date);//2.构造SimpleDateFormat类型的对象并指定格式SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//3.实现日期类型向文件类型的转换打印String format = sdf.format(date);System.out.println(format);//4.实现文本类型到日期类型并打印Date parse = sdf.parse(format);System.out.println(parse);}}
Calendar类的概述
- java.util.Calender类主要用于描述特定的瞬间,取代Date类中的过时方法实现全球化。
- 该类是个抽象类,因此不能实例化对象,其具体子类针对不同国家的日历系统,其中应用最广泛的是GregorianCalendar(格里高利历),对应世界上绝大多数国家/地区使用的标准日历系统。
|
static Calendar getInstance() |
用于获取Calendar类型的引用 | | —- | —- | |
void set(int year, int month, int date, int hourOfDay, int minute, int second) |
用于设置年月日时分秒信息 | |
Date getTime() |
用于将Calendar类型转换为Date类型 | |
void set(int field, int value) |
设置指定字段的数值 | |
void add(int field, int amount) |
向指定字段增加数值 |
package com.lagou.task13;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/*** @author lijing* @date 2020/9/30 15:09* @description*/public class CalendarTest {public static void main(String[] args) {Calendar instance = Calendar.getInstance();instance.set(2008,8-1,8,8,8,8);Date time = instance.getTime();System.out.println(time);SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// instance.add();String format = sdf.format(time);System.out.println(format);instance.add(Calendar.MONTH,2);System.out.println(sdf.format(instance.getTime()));//考点一:既然Calendar类是抽象类不能创建对象,那么是如何获得Calendar类型的引用呢?//解析:利用多态,由源码可知,返回的并不是Calendar类型的对象,而是Calendar的子类的对象,形成了多台//多态的适用场合之三}}
多态的使用场合
通过方法的参数传递形成多态;
public static void draw(Shape s){s.show();}draw(new Rect(1, 2, 3, 4));
在方法体中直接使用多态的语法格式
Account acc = new FixedAccount();
通过方法的返回值类型形成多态
Calender getInstance(){return new GregorianCalendar(zone, aLocale);}
Java8中的日期相关类
由来和概述
JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了。而Calendar并不比Date好多少。它们面临的问题是:
- Date类中的年份是从1900开始的,而月份都从0开始。
- 格式化只对Date类有用,对Calendar类则不能使用。
- 非线程安全等.
- Java 8通过发布新的Date-Time API来进一步加强对 日期与时间的处理。
- java.time包:该包日期/时间API的基础包。
- java.time.chrono包:该包提供对不同日历系统的访问。
- java.time.format包:该包能够格式化和解析日期时间对象。
- java.time.temporal包:该包包含底层框架和扩展特性。
- java.time.zone包:该包支持不同时区以及相关规则的类。
LocalDate类
基本概念:java.time.LocalDate类主要用于描述年月日格式的日期信息,该类不表示时间和时区信息
常用方法:
static LocalDate now() |
在默认时区中从系统时钟获取当前日期 |
|---|---|
LocalTime类
基本概念:java.time.LocalTime类主要用于描述时间信息,可以描述时分秒以及纳秒
常用方法:
static LocalTime now() |
从默认时区的系统时间中获取当前时间 |
|---|---|
static LocalTime now(Zoneld zone) |
获取指定时区的当前时间 |
LocalDateTime类
基本概念:java.time.LocalDateTime类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12-03T10:15:21
static LocalDateTime now() |
从默认时区的系统时间中获取 当前日期时间 |
|---|---|
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) |
根据参数指定的年月日时分秒信息来设置日期时间 |
int getYear() int getMonthValue() int getDayOfMonth() int getHour() int getMinute() int getSecond() |
获取年份字段的数值 获取1到12之间的月份字段 获取日期字段 获取小时数 获取分钟数 获取秒数 |
LocalDateTime withYear(int year)
LocalDateTime withMonth(int month)
LocalDateTime withDayOfMonth(int dayOfMonth)
LocalDateTime withHour(int hour)
LocalDateTime withMinute(int minute)
LocalDateTime withSecond(int second)
|
设置为参数指定的年
设置为参数指定的月
设置为参数指定的日
设置为参数指定的时
设置为参数指定的分
设置为参数指定的秒
|
|
LocalDateTime plusYears(long years)
LocalDateTime plusMonths(long months)
LocalDateTime plusDays(long days)
LocalDateTime plusHours(long hours)
LocalDateTime plusMinutes(long minutes)
LocalDateTime plusSeconds(long seconds)
|
加上参数指定的年
加上参数指定的月
加上参数指定的日
加上参数指定的时
加上参数指定的分
加上参数指定的秒
|
|
LocalDateTime minusYears(long years)
LocalDateTime minusMonths(long months)
LocalDateTime minusDays(long days)
LocalDateTime minusHours(long hours)
LocalDateTime minusMinutes(long minutes)
LocalDateTime minusSeconds(long seconds) |
减去参数指定的年
减去参数指定的月
减去参数指定的日
减去参数指定的时
减去参数指定的分
减去参数指定的秒 |
package com.lagou.task13;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;/*** @author lijing* @date 2020/9/30 15:48* @description*/public class LocalDateTimeTest {public static void main(String[] args) {LocalDate now = LocalDate.now();System.out.println("获取到的当前日期为:"+now);LocalTime now1 = LocalTime.now();System.out.println("获取到的当前时间为:"+now1);LocalDateTime now2 = LocalDateTime.now();System.out.println("获取到的当前日期时间为:"+now2);System.out.println("--------------------------");//4.设置指定年月日时分秒LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8);//使用ctrl+F12来查找指定的方法System.out.println("指定的日期时间为:"+of);//自动调用toStringSystem.out.println("年:"+of.getYear());System.out.println("月:"+of.getMonthValue());System.out.println("日:"+of.getDayOfMonth());System.out.println("时:"+of.getHour());System.out.println("分:"+of.getMinute());System.out.println("秒:"+of.getSecond());System.out.println("--------------------------");//5.实现特征的设置并打印//与String类型相似,调用本身的数据内容不会改变,返回值相当于创建了一个新的对象,由此证明了不可变性LocalDateTime localDateTime = of.withYear(2012);System.out.println("localDateTime="+localDateTime);//localDateTime=2012-08-08T20:08:08System.out.println("of="+of);//localDateTime=2012-08-08T20:08:08System.out.println("--------------------------");//6.实现特征的增加并打印LocalDateTime localDateTime1 = of.plusYears(2);System.out.println("localDateTime1="+localDateTime1);//localDateTime1=2010-08-08T20:08:08System.out.println("--------------------------");//7.实现特征的介减小并打印LocalDateTime localDateTime2 = of.minusYears(4);System.out.println("localDateTime2="+localDateTime2);}}
instant类
基本概念:java.time.Instant类主要用于描述瞬间的时间点信息。
常用方法:
static Instant now() |
从系统时钟上获取当前时间 |
|---|---|
OffsetDateTime atOffset(ZoneOffset offset) |
将此瞬间与偏移量组合以创建偏移日期时间 |
static Instant ofEpochMilli(long epochMilli) |
根据参数指定的毫秒数来构造对象,参数为距离1970年1月1 日0时0分0秒的毫秒数 |
long toEpochMilli() |
获取距离1970年1月1日0时0分0秒的毫秒数 |
package com.lagou.task13;import java.time.Instant;import java.time.LocalDateTime;import java.time.OffsetDateTime;import java.time.ZoneOffset;/*** @author lijing* @date 2020/9/30 16:11* @description*/public class InstantTest {public static void main(String[] args) {//本初子午线Instant now = Instant.now();System.out.println("获取到的当前系统时间:"+now);//东八区LocalDateTime now1 = LocalDateTime.now();System.out.println("获取到的默认系统时间:"+now1);//2.加上时区所差的八个小时OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));System.out.println("偏移后的时间:"+offsetDateTime);System.out.println("-----------------------------");//3.获取距离1970.1.1.0.0的毫秒数long l = now.toEpochMilli();System.out.println("获取到的毫秒差为:"+l);Instant instant = Instant.ofEpochMilli(l);System.out.println("参数指定的好描述构造出来的对象:"+instant);}}
DateTimeFormatter类
基本概念:java.time.format.DateTimeFormatter类主要用于格式化和解析日期。
常用方法:
static DateTimeFormatter ofPattern(String pattern) |
根据参数指定的模式来获取对象 |
|---|---|
String format(TemporalAccessor temporal) |
将参数指定日期时间转换为字符串 |
TemporalAccessor parse(CharSequence text) |
将参数指定字符串转换为日期时间 |
总结
- java8之前的日期时间类
date类 Calendar SimpleDateFormat类 概念 和常用方法
- java8中的日期实践类
LocalDate类 LocalTime类 LocalDateTime类 Instant类 DateTimeFormatter类 概念和常用方法
