本文最后更新于:2022年8月12日 上午
前言
Java 8 已经面世很长时间,目前连 Java 13、Java 14 都已经出来了,但自己对于 Java8 中的一些 API 用法还不是很熟悉,今年才算全面切换到 Java8。在日常开发过程中,不可避免地会使用到 Java 8 的新时间 API,比如 LocalDateTime、LocalDate 等,放弃使用难用的 Date;但是每次需要进行转换操作时,总是需要重复去 Google 搜索,索性一次性过一遍所有的时间 API,总结一些常用的转换方法,比如时间戳与 LocalDateTime 互转,Date 与 LocalDateTime 互转。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| public class Test1 { public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); public static final DateTimeFormatter MONTH_DAY_FORMATTER = DateTimeFormatter.ofPattern("MM-dd"); public static final DateTimeFormatter YEAR_FORMATTER = DateTimeFormatter.ofPattern("yyyy"); public static final DateTimeFormatter YEAR_MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
public static void main(String[] args) {
System.out.println("--------LocalDate---------"); LocalDate today = LocalDate.now(); System.out.println("当前日期:" + today); System.out.println("当前日期加上 3 天:" + today.plusDays(3)); System.out.println("当前日期减去 3 天:" + today.minusDays(3));
System.out.println("年:" + today.getYear()); System.out.println("月 (EN):" + today.getMonth()); System.out.println("月 (1-12):" + today.getMonthValue());
System.out.println("当周第几天:" + today.getDayOfWeek()); System.out.println("当月第几天:" + today.getDayOfMonth()); System.out.println("当年第几天:" + today.getDayOfYear()); System.out.println("是否闰年:" + today.isLeapYear()); System.out.println("当月共多少天:" + today.lengthOfMonth()); System.out.println("当年共多少天:" + today.lengthOfYear());
LocalDate nextDay = today.plusDays(1); Period until = today.until(nextDay); long days = ChronoUnit.DAYS.between(today, nextDay); System.out.println("isBefore:" + today.isBefore(nextDay)); System.out.println("isAfter: " + today.isAfter(nextDay));
LocalDate parseDate = LocalDate.parse("2020-01-24"); System.out.println("parseDate:" + parseDate);
System.out.println("--------LocalDateTime---------"); LocalDateTime nowLocalDateTime = LocalDateTime.now(); System.out.println("当前时间:" + nowLocalDateTime.format(DATE_TIME_FORMATTER)); System.out.println("当前时间加上 3 小时:" + nowLocalDateTime.plusHours(3)); System.out.println("当前时间减去 3 小时:" + nowLocalDateTime.minusHours(3));
System.out.println("年:" + nowLocalDateTime.getYear()); System.out.println("月 (EN):" + nowLocalDateTime.getMonth()); System.out.println("月 (1-12):" + nowLocalDateTime.getMonthValue()); System.out.println("月 (1-12):" + nowLocalDateTime.getMonthValue());
LocalDateTime nextDayNow = nowLocalDateTime.plusDays(1); System.out.println("isBefore:" + nowLocalDateTime.isBefore(nextDayNow)); System.out.println("isAfter:" + nowLocalDateTime.isAfter(nextDayNow));
LocalDateTime parseDateTime = LocalDateTime.parse("2020-01-21 18:05:35", DATE_TIME_FORMATTER); System.out.println("parseDateTime:" + parseDateTime);
System.out.println("--------LocalTime---------"); LocalTime localTime = LocalTime.now(Clock.systemDefaultZone()); System.out.println("当前时间:" + localTime.format(TIME_FORMATTER));
System.out.println("--------MonthDay---------"); MonthDay monthDay = MonthDay.now(); System.out.println("当前月天:" + monthDay.format(MONTH_DAY_FORMATTER));
System.out.println("--------Year---------"); Year year = Year.now(); System.out.println("当前年:" + year.format(YEAR_FORMATTER));
System.out.println("--------YearMonth---------"); YearMonth yearMonth = YearMonth.now(); System.out.println("当前年月:" + yearMonth.format(YEAR_MONTH_FORMATTER));
System.out.println("--------DayOfWeek---------"); DayOfWeek dayOfWeek = DayOfWeek.from(LocalDateTime.now()); System.out.println("当前星期第几天:" + dayOfWeek.getValue());
System.out.println("--------Month---------"); Month month = Month.from(LocalDate.now()); System.out.println("当年第几月" + month.getValue()); System.out.println("当月天数:" + month.length(Year.now().isLeap()));
System.out.println("--------clock---------"); Clock clock = Clock.systemDefaultZone(); System.out.println("当前时间时间戳:" + clock.millis());
System.out.println("--------duration---------"); Duration duration = Duration.between(nowLocalDateTime, nowLocalDateTime.plusHours(2)); System.out.println("秒:" + duration.getSeconds()); System.out.println("毫秒:" + duration.toMillis()); System.out.println("分钟:" + duration.toMinutes()); System.out.println("小时:" + duration.toHours()); System.out.println("天:" + duration.toDays());
System.out.println("--------period---------"); Period period = Period.between(LocalDate.now(), LocalDate.now() .plusYears(1) .plusMonths(2) .plusDays(3)); System.out.println("天:" + period.getDays()); System.out.println("月:" + period.getMonths()); System.out.println("年:" + period.getYears()); System.out.println("总月数:" + period.toTotalMonths());
System.out.println("--------Instant---------"); Instant instant = Instant.now(); System.out.println("毫秒:" + instant.toEpochMilli());
System.out.println("--------实用方法---------"); LocalDateTime todayStartTime = today.atStartOfDay(); System.out.println("指定日期的开始时间:" + todayStartTime.format(DATE_TIME_FORMATTER)); LocalDateTime todayStart = LocalDateTime.of(today, LocalTime.MIN); System.out.println("指定日期的开始时间: " + todayStart.format(DATE_TIME_FORMATTER));
LocalDateTime todayEnd = LocalDateTime.of(today, LocalTime.MAX); System.out.println("指定日期的结束时间:" + todayEnd.format(DATE_TIME_FORMATTER));
System.out.println("毫秒时间戳转 LocalDateTime:" + LocalDateTime.ofInstant(Instant.ofEpochMilli(1579646281991L), ZoneOffset.ofHours(8)).toString()); System.out.println("LocalDateTime 转时间戳:" + nowLocalDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
System.out.println("LocalDateTime 转 yyyy-MM-dd HH:mm:ss 格式 String:" + LocalDateTime.now().format(DATE_TIME_FORMATTER)); LocalDateTime parseLocalDateTime = LocalDateTime.parse("2020-01-22 23:59:59", DATE_TIME_FORMATTER); System.out.println("yyyy-MM-dd HH:mm:ss 格式 String 转 LocalDateTime:" + parseLocalDateTime);
Date nowDate = new Date(); LocalDateTime date2LocalTime = LocalDateTime.ofInstant(nowDate.toInstant(), ZoneOffset.ofHours(8)); System.out.println("Date 转 LocalDateTime:" + date2LocalTime);
Date localDateTime2Date = Date.from(nowLocalDateTime.toInstant(ZoneOffset.ofHours(8))); System.out.println("LocalDateTime 转 Date:" + localDateTime2Date); LocalDateTime localDate2LocalDateTime = LocalDateTime.of(today, LocalTime.parse("00:00:00"));
LocalDate localDateTime2LocalDate = nowLocalDateTime.toLocalDate();
} }
|
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| --------LocalDate--------- 当前日期:2020-01-22 当前日期加上 3 天:2020-01-25 当前日期减去 3 天:2020-01-19 年:2020 月 (EN):JANUARY 月 (1-12):1 当周第几天:WEDNESDAY 当月第几天:22 当年第几天:22 是否闰年:true 当月共多少天:31 当年共多少天:366 today-nextDay 间隔天数: 1 isBefore:true isAfter: false parseDate:2020-01-24 --------LocalDateTime--------- 当前时间:2020-01-22 07:06:13 当前时间加上 3 小时:2020-01-22T10:06:13.979 当前时间减去 3 小时:2020-01-22T04:06:13.979 年:2020 月 (EN):JANUARY 月 (1-12):1 月 (1-12):1 isBefore:true isAfter:false parseDateTime:2020-01-21T18:05:35 --------LocalTime--------- 当前时间:07:06:13 --------MonthDay--------- 当前月天:01-22 --------Year--------- 当前年:2020 --------YearMonth--------- 当前年月:2020-01 --------DayOfWeek--------- 当前星期第几天:3 --------Month--------- 当年第几月 1 当月天数:31 --------clock--------- 当前时间时间戳:1579647973982 --------duration--------- 秒:7200 毫秒:7200000 分钟:120 小时:2 天:0 --------period--------- 天:3 月:2 年:1 总月数:14 --------Instant--------- 毫秒:1579647973983 --------实用方法--------- 指定日期的开始时间:2020-01-22 00:00:00 指定日期的开始时间: 2020-01-22 00:00:00 指定日期的结束时间:2020-01-22 23:59:59 毫秒时间戳转 LocalDateTime:2020-01-22T06:38:01.991 LocalDateTime 转时间戳:1579647973979 LocalDateTime 转 yyyy-MM-dd HH:mm:ss 格式 String:2020-01-22 07:06:13 yyyy-MM-dd HH:mm:ss 格式 String 转 LocalDateTime:2020-01-22T23:59:59 Date 转 LocalDateTime:2020-01-22T07:06:13.984 LocalDateTime 转 Date:Wed Jan 22 07:06:13 CST 2020
|
参考