1、Date
时间毫秒值:指的是从 1970 年 1 月 1 日 00:00:00 走到此刻的总的毫秒数,1 s = 1000 ms = 1000000 μs = 1000000000 ns
方法名 |
说明 |
public Date() |
创建一个 Date 对象,代表的是系统当前此刻日期时间 |
public Date(long time) |
把时间毫秒值转换成 Date 日期对象 |
public long getTime() |
获取时间对象的时间毫秒值 |
public void setTime(long time) |
设置日期对象的时间为当前时间毫秒值对应的时间 |
public boolean after(Date when) |
this 在 when 之后? |
public boolean before(Date when) |
this 在 when 之前? |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| |
| Date date = new Date(); |
| System.out.println(sdf.format(date)); |
| |
| Thread.sleep(2000); |
| |
| date.setTime(System.currentTimeMillis()); |
| System.out.println(sdf.format(date)); |
2、Instant
Instant 类由一个静态的工厂方法 now() 创建,时间戳是包含日期和时间的
Instant 与 java.util.Date 很类似,事实上 Instant 就是类似 JDK8 以前的 Date,Instant 和 Date 这两个类可以进行转换
| Instant instant = Instant.now(); |
| System.out.println("当前时间戳是:" + instant); |
| |
| Instant instant = Instant.now(); |
| System.out.println(instant.atZone(ZoneId.systemDefault())); |
| |
| Date date = Date.from(instant); |
| System.out.println("当前时间是:" + date); |
| |
| instant = date.toInstant(); |
| System.out.println(instant); |
可以对 Date 对象或时间毫秒值格式化成我们喜欢的时间形式,也可以把字符串的时间形式解析成日期对象(年月日时分秒 y M d H m s)
构造方法 |
说明 |
public SimpleDateFormat() |
构造一个 SimpleDateFormat,使用默认格式 |
public SimpleDateFormat(String pattern) |
构造一个 SimpleDateFormat,使用指定的格式 |
格式化方法 |
说明 |
public final String format(Date date) |
将日期格式化成:日期 / 时间字符串 |
public final String format(Object time) |
将时间毫秒值式化成:日期 / 时间字符串 |
public Date parse(String source) |
从给定字符串的开始解析文本以生成日期,会抛异常 |
| |
| Date date = new Date(); |
| long time = date.getTime(); |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| |
| String rs1 = sdf.format(date); |
| String rs2 = sdf.format(time); |
| |
| |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| Date d = sdf.parse("2021-08-04 11:11:11"); |
4、Calendar
calendar 是可变日期对象,一旦修改后其对象本身表示的时间将产生变化(从 0 开始记月)
方法名 |
说明 |
public static Calendar getInstance() |
获取当前日历对象 |
public int get(int field) |
取日期中的某个字段信息 |
public void set(int field, int value) |
修改日历的某个字段信息 |
public void add(int field, int amount) |
为某个字段增加 / 减少指定的值 |
public final Date getTime() |
拿到此刻日期对象 |
public long getTimeInMillis() |
拿到此刻时间毫秒值 |
| |
| Calendar cal = Calendar.getInstance(); |
| System.out.println(cal); |
| |
| |
| int year = cal.get(Calendar.YEAR); |
| System.out.println(year); |
| |
| int mm = cal.get(Calendar.MONTH) + 1; |
| System.out.println(mm); |
| |
| int days = cal.get(Calendar.DAY_OF_YEAR); |
| System.out.println(days); |
| |
| |
| cal.set(Calendar.HOUR, 12); |
| System.out.println(cal); |
| |
| |
| |
| cal.add(Calendar.DAY_OF_YEAR, 64); |
| cal.add(Calendar.MINUTE, 59); |
| |
| |
| Date d = cal.getTime(); |
| System.out.println(d); |
| |
| |
| long time = cal.getTimeInMillis(); |
| System.out.println(time); |
5、LocalDate、LocalTime、LocalDateTime
它们的类的实例是不可变的对象,它们三者构建对象和 API 都是通用的
方法名 |
说明 |
public static Xxxx now() |
静态方法,根据当前时间创建对象 |
public static Xxxx of(…) |
静态方法,指定日期 / 时间创建对象 |
方法名 |
说明 |
public int geYear() |
获取年 |
public int getMonthValue() |
获取月份(1-12) |
Public int getDayOfMonth() |
获取月中第几天(日) |
Public int getDayOfYear() |
获取年中第几天 |
Public DayOfWeek getDayOfWeek() |
获取星期 |
方法名 |
说明 |
public LocalDate toLocalDate() |
LocalDateTime 对象转换成一个 LocalDate 对象 |
public LocalTime toLocalTime() |
LocalDateTime 对象转换成一个 LocalTime 对象 |
方法名 |
说明 |
plusDays、plusWeeks、plusMonths、plusYears |
向当前 LocalDate 对象添加几天、 几周、几个月、几年 |
minusDays、minusWeeks、minusMonths、minusYears |
从当前 LocalDate 对象减去几天、 几周、几个月、几年 |
withDayOfMonth、withDayOfYear、withMonth、withYear |
将月份天数、年份天数、月份、年份修改为指定的值并返回新的 LocalDate 对象 |
isBefore、isAfter |
比较两个 LocalDate |
创建对象
| LocaDate localDate = LocalDate.now(); |
| LocalTime llocalTime = LocalTime.now(); |
| LocalDateTime localDateTime = LocalDateTime.now(); |
| |
| LocalDate localDate = LocalDate.of(2099, 11, 11); |
| LocalTime localTime = LocalTime.of(11, 11, 11); |
| LocalDateTime localDateTime = LocalDateTime.of(2020, 10, 6, 13, 23, 43); |
LocalDate
| |
| LocalDate nowDate = LocalDate.now(); |
| System.out.println("今天的日期:" + nowDate); |
| |
| int year = nowDate.getYear(); |
| System.out.println("year:" + year); |
| |
| int month = nowDate.getMonthValue(); |
| System.out.println("month:" + month); |
| |
| int day = nowDate.getDayOfMonth(); |
| System.out.println("day:" + day); |
| |
| |
| int dayOfYear = nowDate.getDayOfYear(); |
| System.out.println("dayOfYear:" + dayOfYear); |
| |
| |
| System.out.println(nowDate.getDayOfWeek()); |
| System.out.println(nowDate.getDayOfWeek().getValue()); |
| |
| |
| System.out.println(nowDate.getMonth()); |
| System.out.println(nowDate.getMonth().getValue()); |
| |
| LocalDate bt = LocalDate.of(1991, 11, 11); |
| System.out.println(bt); |
| System.out.println(LocalDate.of(1991, Month.NOVEMBER, 11)); |
LocalTime
| |
| LocalTime nowTime = LocalTime.now(); |
| System.out.println("今天的时间:" + nowTime); |
| |
| int hour = nowTime.getHour(); |
| System.out.println("hour:" + hour); |
| |
| int minute = nowTime.getMinute(); |
| System.out.println("minute:" + minute); |
| |
| int second = nowTime.getSecond(); |
| System.out.println("second:" + second); |
| |
| int nano = nowTime.getNano(); |
| System.out.println("nano:" + nano); |
| |
| System.out.println(LocalTime.of(8, 20)); |
| System.out.println(LocalTime.of(8, 20, 30)); |
| System.out.println(LocalTime.of(8, 20, 30, 150)); |
| LocalTime mTime = LocalTime.of(8, 20, 30, 150); |
| |
| System.out.println(LocalDateTime.of(1991, 11, 11, 8, 20)); |
| System.out.println(LocalDateTime.of(1991, Month.NOVEMBER, 11, 8, 20)); |
| System.out.println(LocalDateTime.of(1991, 11, 11, 8, 20, 30)); |
| System.out.println(LocalDateTime.of(1991, Month.NOVEMBER, 11, 8, 20, 30)); |
| System.out.println(LocalDateTime.of(1991, 11, 11, 8, 20, 30, 150)); |
| System.out.println(LocalDateTime.of(1991, Month.NOVEMBER, 11, 8, 20, 30, 150)); |
LocalDateTime
| |
| LocalDateTime nowDateTime = LocalDateTime.now(); |
| System.out.println("今天是:" + nowDateTime); |
| |
| System.out.println(nowDateTime.getYear()); |
| System.out.println(nowDateTime.getMonthValue()); |
| System.out.println(nowDateTime.getDayOfMonth()); |
| System.out.println(nowDateTime.getHour()); |
| System.out.println(nowDateTime.getMinute()); |
| System.out.println(nowDateTime.getSecond()); |
| System.out.println(nowDateTime.getNano()); |
| |
| System.out.println("dayOfYear:" + nowDateTime.getDayOfYear()); |
| |
| System.out.println(nowDateTime.getDayOfWeek()); |
| System.out.println(nowDateTime.getDayOfWeek().getValue()); |
| |
| System.out.println(nowDateTime.getMonth()); |
| System.out.println(nowDateTime.getMonth().getValue()); |
| |
| |
| LocalDate ld = nowDateTime.toLocalDate(); |
| System.out.println(ld); |
| |
| LocalTime lt = nowDateTime.toLocalTime(); |
| System.out.println(lt.getHour()); |
| System.out.println(lt.getMinute()); |
| System.out.println(lt.getSecond()); |
时间运算
| LocalTime nowTime = LocalTime.now(); |
| System.out.println(nowTime); |
| |
| System.out.println(nowTime.minusHours(1)); |
| System.out.println(nowTime.minusMinutes(1)); |
| System.out.println(nowTime.minusSeconds(1)); |
| System.out.println(nowTime.minusNanos(1)); |
| |
| System.out.println(nowTime.plusHours(1)); |
| System.out.println(nowTime.plusMinutes(1)); |
| System.out.println(nowTime.plusSeconds(1)); |
| System.out.println(nowTime.plusNanos(1)); |
| |
| System.out.println(nowTime); |
| |
| |
| LocalDate myDate = LocalDate.of(2018, 9, 5); |
| LocalDate nowDate = LocalDate.now(); |
| |
| System.out.println("今天是2018-09-06吗?" + nowDate.equals(myDate)); |
| System.out.println(myDate + "是否在" + nowDate + "之前?" + myDate.isBefore(nowDate)); |
| System.out.println(myDate + "是否在" + nowDate + "之后?" + myDate.isAfter(nowDate)); |
| |
| |
| LocalDate birDate = LocalDate.of(1996, 8, 5); |
| LocalDate nowDate = LocalDate.now(); |
| |
| MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth()); |
| MonthDay nowMd = MonthDay.from(nowDate); |
| |
| System.out.println("今天是你的生日吗?" + birMd.equals(nowMd)); |
| LocalDateTime ldt = LocalDateTime.now(); |
| System.out.println(ldt); |
| |
| DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| |
| |
| |
| String ldtStr1 = ldt.format(dtf); |
| System.out.println(ldtStr1); |
| |
| String ldtStr2 = dtf.format(ldt); |
| System.out.println(ldtStr2); |
| |
| LocalDateTime ldt = LocalDateTime.now(); |
| System.out.println(ldt); |
| |
| |
| DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a"); |
| |
| |
| System.out.println(dtf.format(ldt)); |
| |
| System.out.println(ldt.format(dtf)); |
| |
| |
| DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| |
| LocalDateTime ldt = LocalDateTime.parse("2019-11-11 11:11:11", dtf); |
| System.out.println(ldt); |
| System.out.println(ldt.getDayOfYear()); |
7、Period、Duration
Period:方法 getYears(),getMonths() 和 getDays(),只能精确到年月日,用于 LocalDate 之间的比较(计算 "日期" 间隔)
| LocalDate today = LocalDate.now(); |
| System.out.println(today); |
| |
| LocalDate birthDate = LocalDate.of(1995, 1, 11); |
| System.out.println(birthDate); |
| |
| Period period = Period.between(birthDate, today); |
| |
| System.out.println(period.getYears()); |
| System.out.println(period.getMonths()); |
| System.out.println(period.getDays()); |
Duration:用于 LocalDateTime 之间的比较,也可用于 Instant 之间的比较(计算 "时间" 间隔)
| LocalDateTime today = LocalDateTime.now(); |
| System.out.println(today); |
| |
| LocalDateTime birthDate = LocalDateTime.of(1990, 10, 1, 10, 50, 30); |
| System.out.println(birthDate); |
| |
| Duration duration = Duration.between(birthDate, today); |
| |
| System.out.println(duration.toDays()); |
| System.out.println(duration.toHours()); |
| System.out.println(duration.toMinutes()); |
| System.out.println(duration.toMillis()); |
| System.out.println(duration.toNanos()); |
8、ChronoUnit
ChronoUnit 类可用于在单个时间单位内测量一段时间,这个工具类是最全的了,可以用于比较所有的时间单位
| LocalDateTime today = LocalDateTime.now(); |
| System.out.println(today); |
| |
| LocalDateTime birthDate = LocalDateTime.of(1990, 10, 1, 10, 50, 30); |
| System.out.println(birthDate); |
| |
| System.out.println("相差的年数: " + ChronoUnit.YEARS.between(birthDate, today)); |
| System.out.println("相差的月数: " + ChronoUnit.MONTHS.between(birthDate, today)); |
| System.out.println("相差的周数: " + ChronoUnit.WEEKS.between(birthDate, today)); |
| System.out.println("相差的天数: " + ChronoUnit.DAYS.between(birthDate, today)); |
| |
| System.out.println("相差的时数: " + ChronoUnit.HOURS.between(birthDate, today)); |
| System.out.println("相差的分数: " + ChronoUnit.MINUTES.between(birthDate, today)); |
| System.out.println("相差的秒数: " + ChronoUnit.SECONDS.between(birthDate, today)); |
| |
| System.out.println("相差的毫秒数: " + ChronoUnit.MILLIS.between(birthDate, today)); |
| System.out.println("相差的微秒数: " + ChronoUnit.MICROS.between(birthDate, today)); |
| System.out.println("相差的纳秒数: " + ChronoUnit.NANOS.between(birthDate, today)); |
| |
| System.out.println("相差的半天数: " + ChronoUnit.HALF_DAYS.between(birthDate, today)); |
| System.out.println("相差的十年数: " + ChronoUnit.DECADES.between(birthDate, today)); |
| |
| System.out.println("相差的世纪(百年)数: " + ChronoUnit.CENTURIES.between(birthDate, today)); |
| System.out.println("相差的千年数: " + ChronoUnit.MILLENNIA.between(birthDate, today)); |
| System.out.println("相差的纪元数: " + ChronoUnit.ERAS.between(birthDate, today)); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步