【Java】常用的时间类API

目录

Date(时间和日期)

SimpleDateFormat(解析字符串时间成为时间对象)

Calendar(系统此刻时间对应的日历)

LocalDate(年、月、日)

LocalTime(时、分、秒)

LocalDateTime(年、月、日、时、分、秒)

ZoneId(时区)

ZoneDateTime(带时区的时间)

Instant(时间戳/时间线:获取总秒数+不够1秒的纳秒数)

DateTimeFormatter(时间的格式化和解析)(线程安全)

Period(时间间隔(年、月、日))

Duration(时间间隔(时、分、秒、纳秒))


Date、SimpDateFormat、Calendar、LocallDate、LocalTime、LocalDateTime、ZoneId、ZoneDateTime、Instant、DateTimeFormatter、Period、Duration时间类的常用API

Date(时间和日期)

构造器作用
public Date()创建一个Date对象,代表系统当前此刻时间
public Date()把时间毫秒值转换成Date日期对象
常见方法作用
public long getTime()返回从1970年1月1日 00:00:00到此刻的时间毫秒值
public void setTime(long time)设置日期对象为当前时间毫秒值对应的时间

SimpleDateFormat(解析字符串时间成为时间对象)

常见构造器作用
public SimpleDateFormat(String pattern)创建简单日期格式化对象,并封装时间的格式
常见方法作用
public final String format(Date date)将日期格式化成日期/时间字符串
public final String format(Object time)将时间毫秒值转换为日期/时间字符串
public Date parse(String source)把字符串时间解析为日期对象

 时间格式

Calendar(系统此刻时间对应的日历)

//获取日历对象
Calendar c = Calendar.getInstance();
常见方法作用
public static Calendar getInstance()获取当前日历对象
public int get(int field)获取日历中的某个信息
public final Date getTime()过去日期对象
public long getTimeMillis()获取时间毫秒值
public void set(int field,int value)修改日历中的某个信息
public void add(int field,int amount)为某个信息增加/减少指定的值

LocalDate(年、月、日)

常见方法作用
public static LocalDate now()获取本地日期对象
public int getYear/MonthValue/DayOfMonth/DayOfYear()获取日期对象中的信息
public int getDayOfWeek().getValue()获取星期几
public LocalDate withYear/Month/DayOfMonth/DayOfYear(int xxx)直接修改某个信息
public LocalDate plusYears/Months/Days/DayWeeks(long xxx)把某个信息加多少
public LocalDate minusYears/Months/Days/DayWeeks(long xxx)把某个信息减多少
public static LocalDate of(int year,int month,int day)获取指定信息的LocalDate对象
public boolean equals/isBefore/isAfter(Object object)判断两个日期对象是否相同,在前还是在后

LocalTime(时、分、秒)

常见方法作用
public static LocalTime now()获取本地日期对象
public int getHour/Minute/Second/Nano()获取日期对象中的信息
public LocalTime withHour/Minute/Second/Nano(int xxx)直接修改某个信息
public LocalTime plusHours/Minutes/Seconds/Nanos()(long xxx)把某个信息加多少
public LocalTime minusHours/Minutes/Seconds/Nanos()(long xxx)把某个信息减多少
public static LocalDate of(int hour,int minute,int second)获取指定信息的LocalDate对象
public boolean equals/isBefore/isAfter(Object object)判断两个日期对象是否相同,在前还是在后

LocalDateTime(年、月、日、时、分、秒)

常见方法作用
public static LocalDateTime now()

获取本地日期对象

public LocalDate toLocalDate()将LocalDateTime对象转换为LocalDate对象
public LocalDate toLocalDate()将LocalDateTime对象转换为LocalTime对象
public static LocalDateTime of(LocalDate locaDate,LocalTime localTime)将LocalDate和LocalTIme对象转换为LocalDateTime对象

LocalDateTime提供的格式化解析时间的方法

方法作用
public String format(DateTimeFormatter formatter)格式化时间
public static LocalDateTime parse(CharSequence text,DateTimeFormatter formatter)解析时间

其他常见方法包含LocalDate和LocalTime中的常见方法

ZoneId(时区)

常见方法作用
public static Set<String> getAvailableZoneIds()获取Java中支持的所有地区
public static ZoneId systemDefault()获取系统默认时区
public static ZoneId of(String zoneId)指定一个时区

ZoneDateTime(带时区的时间)

常见方法作用
public static ZonedDateTime now()获取当前时区的ZonedDateTime对象
public static ZonedDateTime now(Zoneld zone)获取指定时区的ZonedDateTime对象
getYear、getMonthValue、getDayOfMonth、getDayOfYear、getDayOfWeek、getHour、getMinute、getSecond、getNano获取年、月、日、时、分、秒、纳秒等信息
public ZonedDateTime withxxx(时间)修改时间系列的方法
public ZonedDateTime plusxxx(时间)增加时间系列的方法
public ZonedDateTime minusxxx(时间)减少时间系列的方法

Instant(时间戳/时间线:获取总秒数+不够1秒的纳秒数)

常见方法作用
public static Instant now()获取当前时间的Instant对象(标准时间)
public long getEpochSecond()获取从1970...开始记录的秒数
public int getNano()从时间线开始,获取第二个开始的纳秒数==>不够1s的纳秒数
plusMillis、plusSecond、plusNanos增加系列的方法
minusMillis、minusSecond、minusNanos减少系列的方法
equals、isBefore、isAfter判断系列的方法

DateTimeFormatter(时间的格式化和解析)(线程安全)

常见方法作用
public static DateTimeFormatter ofPattern(时间格式)获取格式化器对象
public String format(时间对象)格式化时间

Period(时间间隔(年、月、日))

常见方法作用
public static Period between(LocalDate start,LocalDate end)传入两个日期对象,得到Period对象
public int getYears()返回隔几年
public int getMonths()返回隔几月
public int getDays()返回隔几天

Duration(时间间隔(时、分、秒、纳秒))

常用方法作用
public static Duration between(开始事件对象,结束事件对象)传入两个事件对象,各道Duration对象
public long toDays()计算间隔多少天,并返回
public long toHours()计算间隔多少小时,并返回
public long toMinutes()计算间隔多少分钟,并返回
public long toSeconds()计算间隔多少秒,并返回
public long toMillis()计算间隔多少毫秒,并返回
public long toNanos()计算间隔多少纳秒,并返回

  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是只菜鸟呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值