一、Date
/** * 创建人:LYY * 创建时间:2022/5/1 */public class DateDemo { public static void main(String[] args) { // 获取当前时间 Date date = new Date(); // 返回自1970年1月1日至当前日期的毫秒数 long time = date.getTime(); // sql包下的date类 Date dateSql = new java.sql.Date(time); System.out.println(date); System.out.println(dateSql); }}
二、SimpleDateFormat
/** * 创建人:LYY * 创建时间:2022/5/1 */public class SimpleDateFormatTest { public static void main(String[] args) { StringBuffer sss = new StringBuffer("sss"); // 指定日期格式 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 将日期转换为字符串 String format = simpleDateFormat.format(new Date()); System.out.println("format = " + format); // 将字符串转换为日期 try { Date parse = simpleDateFormat.parse(format); System.out.println(parse); } catch (ParseException e) { e.printStackTrace(); } }}
三、LocalDate、LocalTime、LocalDateTime
/** * 创建人:LYY * 创建时间:2022/5/3 */public class LocalDateTimeTest { /** * LocalDateTime的常用方法 */ @Test public void getLocalDateTimeMethod() { LocalDateTime localDateTime = LocalDateTime.of(2022,05,04,13,32,55); // 获取当前年 int year = localDateTime.getYear(); System.out.println("year = " + year); // 获取当前天是当前年的第几天 int dayOfYear = localDateTime.getDayOfYear(); System.out.println("dayOfYear = " + dayOfYear); // 获取当前月 Month month = localDateTime.getMonth(); System.out.println("month = " + month); // 获取当前月 int monthValue = localDateTime.getMonthValue(); System.out.println("monthValue = " + monthValue); // 获取当前月的第几天 int dayOfMonth = localDateTime.getDayOfMonth(); System.out.println("dayOfMonth = " + dayOfMonth); // 获取当前星期 DayOfWeek dayOfWeek = localDateTime.getDayOfWeek(); System.out.println("dayOfWeek = " + dayOfWeek); // 修改时间 LocalDateTime中的修改都是with开头 LocalDateTime localDateTime1 = localDateTime.withHour(16); System.out.println("localDateTime1 = " + localDateTime1); } /** * 获取指定时间 */ @Test public void setDateTime() { // 获取23点30分25秒 LocalTime localTime = LocalTime.of(23, 30, 25); // 获取2022-05-01 LocalDate localDate = LocalDate.of(2022, 05, 01); // 获取指定日期和时间 2022-05-05 14:25:30 LocalDateTime localDateTime = LocalDateTime.of(2022, 05, 05, 14, 25, 30); System.out.println("localDate = " + localDate); System.out.println("localTime = " + localTime); System.out.println("localDateTime = " + localDateTime); } /** * 获取当前时间 */ @Test public void getDateTime() { // 获取当前日期 LocalDate localDate = LocalDate.now(); // 获取当前时间 LocalTime localTime = LocalTime.now(); // 获取当前日期+时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDate = " + localDate); System.out.println("localTime = " + localTime); System.out.println("localDateTime = " + localDateTime); }}
四、instant
五、DateTimeFormatter
/** * 创建人:LYY * 创建时间:2022/5/3 * @author LYY * LocalDateTime格式化 */public class DateTimeFormatterTest { /** * 自定义格式转换日期 */ @Test public void customDateTimeFormatter() { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 将日期转换成指定格式字符串 String format = dateTimeFormatter.format(LocalDateTime.now()); System.out.println("format = " + format); // 字符串转换成日期 TemporalAccessor parse = dateTimeFormatter.parse(format); System.out.println("parse = " + parse); } /** * DateTimeFormatter本地相关的格式化 * FULL * LONG * MEDIUM * SHORT */ @Test public void ofLocalDateTime() { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG); String format = dateTimeFormatter.format(LocalDateTime.now()); System.out.println("format = " + format); } /** * DateTimeFormatter预定义的标准格式 */ @Test public void defaultDateTimeFormatter() { // DateTimeFormatter类中预定义的标准格式 ISO_LOCAL_DATE_TIME DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME; String format = isoLocalDateTime.format(LocalDateTime.now()); System.out.println("format = " + format); }}
六、其他Api
- Zoneld: 所有时区信息
- ZonedDateTime: 一个在ISO-8601日历系统时区的日期时间
- Clock: 使用时区提供对当前即时、日期和时间的访问的时钟
- Duration: 持续时间 用于计算两个”时间”间隔
- Period: 用于计算连两个”日期”间隔
- TemporalAdjuster: 时间校正器
- TemporalAdjusters: 上方类的工具类