JDK8之前常用的日期时间类
-
System.currentTimeMillis()
:获取当前毫秒数(long类型) -
java.util.Date
:通用Date类import java.util.Date;Date date = new Date(); // 空参构造器 System.out.println(date.getTime()); // 获取当前时间毫秒数 1730619267197 System.out.println(date.toString()); // 返回日期的字符串 Sun Nov 03 15:34:27 CST 2024 Date date1 = new Date(1232324325421L); // 带参构造器 System.out.println(date1.getTime()); // 获取当前时间毫秒数 1232324325421 System.out.println(date1.toString()); // 返回日期的字符串 Mon Jan 19 08:18:45 CST 2009
-
java.sql.Date
:sql中的Date类import java.sql.Date;Date date = new Date(1232324325421L); System.out.println(date.getTime()); // 获取当前时间毫秒数 1232324325421 System.out.println(date.toString()); // 返回日期的字符串 2009-01-19
-
SimpleDateFormat(String formatStr)
:用于将java.util.Date类的日期格式化和解析import java.text.SimpleDateFormat; import java.util.Date;// 默认格式化形式 SimpleDateFormat sdf = new SimpleDateFormat(); String strDate = sdf.format(new Date()); // 格式化日期 System.out.println(strDate); // 2024/11/3 下午3:43 Date date = sdf.parse(strDate); System.out.println(date.toString()); // Sun Nov 03 15:43:00 CST 2024// 指定格式化形式 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String strDate1 = sdf1.format(new Date()); System.out.println(strDate1); // 2024-11-03 16:05:58 Date date1 = sdf1.parse(strDate1); System.out.println(date1.toString()); // Sun Nov 03 16:05:58 CST 2024
-
Calendar
日历类
Calendar是一个抽象类,所以不能创建Calendar的对象,但提供了一个getInstace()可以获取到其子类的对象Calendar calendar = Calendar.getInstance(); // 等同于 Calendar calendar1 = new GregorianCalendar();
常用方法
①get(int field)
:获取Calendar calendar = Calendar.getInstance(); int num = calendar.get(Calendar.DAY_OF_YEAR); // 获取今天是今年的第几天 int num1 = calendar.get(Calendar.DAY_OF_MONTH); // 获取今天是这个月的第几天 System.out.println(num); // 308 System.out.println(num1); // 3
②
set(int field, xx)
:设置Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 20); int num = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(num); // 20
③
add(int filed, xx)
:在当前基础上增加xxCalendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 20); calendar.add(Calendar.DAY_OF_MONTH, 5); // + 5 天 int num = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(num); // 25 calendar.add(Calendar.DAY_OF_MONTH, -4); // - 4 天 int num1 = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(num1); // 21
④
getTime()
:将日历对象转为Date对象Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); System.out.println(date); // Sun Nov 03 16:41:26 CST 2024
⑤
setTime(Date date)
:重置日历对象Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 1); Date date = calendar.getTime(); System.out.println(date); // Fri Nov 01 16:45:44 CST 2024 calendar.setTime(new Date()); Date date1 = calendar.getTime(); System.out.println(date1); // Sun Nov 03 16:45:44 CST 2024
JDK8新增的日期时间类
-
LocalDate
、LocalTime
、LocalDateTime
① 实例化日期对象(当前时间):now()
LocalDate date1 = LocalDate.now(); LocalTime date2 = LocalTime.now(); LocalDateTime date3 = LocalDateTime.now(); System.out.println(date1); // 2024-11-03 System.out.println(date2); // 19:35:16.405199400 System.out.println(date3); // 2024-11-03T19:35:16.405199400
② 实例化日期对象(指定时间):
of()
LocalDate date1 = LocalDate.of(2024, 12, 12); System.out.println(date1); // 2024-12-12 LocalTime date2 = LocalTime.of(19, 41, 55); System.out.println(date2); // 19:41:55 LocalDateTime date3 = LocalDateTime.of(2024, 12, 12, 19, 41, 55); System.out.println(date3); // 2024-12-12T19:41:55
③ 获取:
getxxx()
LocalDate date1 = LocalDate.now(); int year = date1.getYear(); int month = date1.getMonthValue(); int day = date1.getDayOfMonth(); System.out.println(year + "-" + month + "-" + day);
④ 修改:
withxxx()
LocalDate date1 = LocalDate.now(); LocalDate date2 = date1.withYear(2026).withMonth(3).withDayOfMonth(8); System.out.println(date1); // 2024-11-3 System.out.println(date2); // 2026-03-08
⑤ 增加:
plusxxx()
LocalDate date1 = LocalDate.now(); LocalDate date2 = date1.plusYears(3).plusMonths(1).plusDays(2); System.out.println(date1); // 2024-11-3 System.out.println(date2); // 2027-12-05
⑥ 减少:
minusxxx()
LocalDate date1 = LocalDate.now(); LocalDate date2 = date1.minusYears(3).minusMonths(1).minusDays(2); System.out.println(date1); // 2024-11-3 System.out.println(date2); // 2021-10-01
-
Instant
:与Date类似Instant instant1 = Instant.now(); // 获取当前时间的时间戳(差8个时区) OffsetDateTime odt = instant1.atOffset(ZoneOffset.ofHours(8)); System.out.println(instant1); // 伦敦时间:2024-11-03T12:21:05.092270500Z System.out.println(odt); // 北京时间:2024-11-03T20:21:05.092270500+08:00 Instant instant2 = Instant.ofEpochMilli(1234234333335L); // 设置指定时间的日期对象 System.out.println(instant1.toEpochMilli()); // 获取日期对象的时间戳:1730636465092
-
DateTimeFormatter
:针对LocalDate、LocalTime、LocalDateTime日期的格式化和解析DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime date1 = LocalDateTime.now(); String strDate = dtf.format(date1); System.out.println(strDate); // 2024-11-03 21:01:39 LocalDateTime date2 = LocalDateTime.from(dtf.parse(strDate)); // 需要使用LocalDateTime.from()转成LocalDateTime对象 System.out.println(date2); // 2024-11-03T21:01:39