Date:
全世界的时间有一个唯一的计算标准
格林威治时间GMT
原子钟:利用铯原子的振动频率计算出来的时间称为世界标准时间(UTC)
中国标准时间:世界标准时间+8小时
Date类是一个JDK写好的Javabean类,用来描述时间,精确到毫秒
利用空参构造创建的对象默认表示当前的时间
public Date()
利用有参构造创建的对象表示指定的时间
public Date(long date)
设置/修改毫秒值:
public void setTime(long time)
获取时间对象的毫秒值
public long getTime()
SimpleDateFormat:
格式化:把时间转换成指定的格式
解析:把字符串转化为日期Date格式
格式化时间形式的常用模式对应关系:
y:年,M:月,d:日,H:小时,m:分钟,s:秒钟
构造方法:
public SimpleDateFormat():构造一个SimpleDateFormat,使用默认格式
public SimpleDateFormat(String pattern):构造一个SimpleDateFormat,使用指定的格式
成员方法(都是SimpleDateFormat类型对象的方法):
public final String format(Date date):格式化,日期对象——>字符串
public Date parse(String source):解析,字符串——>日期对象
Calendar:
代表了系统当前时间的日历对象,可以单独修改、获取时间中的年,月,日
细节:Calendar是一个抽象类,不能直接创建对象,只能通过一个静态方法获取到子类对象
Calendar c = Calendar.getInstance():
会根据系统的不同时区来获取不同的日历对象,默认表示当前时间
将纪元、年、月、日、时、分、秒、星期等等存储到一个数组中
0:纪元,1:年,2:月,3:一年中的第几周,4:一个月中的第几周,5:一个月中的第几天
常用方法:
public final Date getTime():获取日期对象
public final void setTime(Date date):给日历设置日期对象
public long getTimeInMillis():拿到日期毫秒值
public void setTimeInMillis(long millis):给日历设置时间毫秒值
public int get(int field):取日历中的某个字段信息
public void set(int field,int value):修改日历的某个字段信息
public void add(int field,int amount):为某个字段增加/减少指定的值
field:Calendar.YEAR/MONTH,DAY_OF_MONTH/DAY_OF_WEEK
JDK8中时间日期对象都是不可变的
ZoneID时区:
洲名/城市名
国家名/城市名
常用方法:
static Set<String> getAvailableZoneIds():获取Java中支持的所有时区
static ZoneId systemDefault():获取系统默认时区
static ZoneId of(String zoneId):获取一个指定的时区
Instant时间戳:
常用方法:
static Instant now():获取当前时间的Instant对象(标准时间)
static Instant ofXxxx(long epochMilli):根据(秒/毫秒/纳秒)获取Instant对象
ZonedDateTime atZone(ZoneId zone):指定时区
boolean isXxxx(Instant otherInstant):判断系统的方法
Instant minuxXxxx(long millisToSubstract):以当前时间为基准减少时间系列的方法
Instant plusXxxx(long millisToSubstract):以当前时间为基准增加时间系列的方法
ZoneDateTime带有时区的时间:
常用方法:
static ZonedDateTime now():获取当前时间的ZonedDateTime对象
static ZonedDateTime ofXxxxx(...):获取指定时间的ZonedDateTime对象
ZonedDateTime withXxxx(时间):修改时间系列的方法
ZonedDateTime minusXxxx(时间):减少时间系列的方法
ZonedDateTime plusXxxx(时间):增加时间系列的方法
JDK8新增的时间对象都是不可变的,如果修改后会得到一个新的对象
DateTimeFormatter用于时间的格式化和解析:
方法名:
static DateTimeFormatter ofPattern(格式):获取格式对象
String format(时间对象):按照指定方式格式化
LocalDate年月日、LocalTime时分秒、LocalDateTime年月日时分秒:
常用方法:
static XXX now():获取当前时间的对象
static XXX of(...):获取指定时间的对象
get开头的方法:获取日历中的年、月、日、时、分、秒等信息
isBefore/isAfter:比较两个LocalDate
with开头的:修改时间系列的方法
minus开头的:减少时间系列的方法
plus开头的:增加时间系列的方法
public LocalDate toLocalDate():将LocalDateTime转换成一个LocalDate对象
public LocalTime toLocalTime():将LocalDateTime转换成一个LocalTime对象
Duartion时间间隔(秒、纳秒)、Period时间间隔(年、月、日)、ChronoUnit计算两个日期间隔:
常用方法:
between()
toXXX()