一、背景
Java8的特性已经不再是“新特性”,很多Java8的类也逐渐被更多的程序员在使用。
但是项目中的日期工具类,有一些还用的是Calandar类,非常不方便。
本文简单给出几个LocalDate的封装,展示一下基本用法。
大家可以多使用Java 8的时间相关类,功能更强大,代码更简洁。
二、上代码
Demo日期工具类
import java.math.BigDecimal; import java.math.RoundingMode; import java.time.*; import java.time.format.DateTimeFormatter; import static java.time.temporal.TemporalAdjusters.firstDayOfMonth; /** * @author 明明如月 * @date 2018/11/14 */ public class DateUtil { public static DateTimeFormatter defaultDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static DateTimeFormatter simpleDateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd"); public static DateTimeFormatter defaultFormatterWithTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * '前一天的日期 */ public static LocalDate lastDay(String date, DateTimeFormatter dateTimeFormatter) { LocalDate localDate = LocalDate.parse(date, dateTimeFormatter); return localDate.minusDays(1); } /** * 本周一的日期 */ public static LocalDate thisWeekFirstDay(String date, DateTimeFormatter dateTimeFormatter) { LocalDate localDate = LocalDate.parse(date, dateTimeFormatter); return localDate.with(DayOfWeek.MONDAY); } /** * 上周一 */ public static LocalDate lastWeekFirstDay(String date, DateTimeFormatter dateTimeFormatter) { LocalDate localDate = LocalDate.parse(date, dateTimeFormatter); return localDate.minusDays(7).with(DayOfWeek.MONDAY); } /** * 本月第一天 */ public static LocalDate thisMonthFirstDay(String date, DateTimeFormatter dateTimeFormatter) { LocalDate localDate = LocalDate.parse(date, dateTimeFormatter); return localDate.with(firstDayOfMonth()); } /** * 上月第一天 */ public static LocalDate lastMonthFirstDay(String date, DateTimeFormatter dateTimeFormatter) { LocalDate localDate = LocalDate.parse(date, dateTimeFormatter); return localDate.minusMonths(1).with(firstDayOfMonth()); } /** * 两个时间之间间隔了多少年 */ public static BigDecimal year(long low, long high) { Instant instant1 = Instant.ofEpochMilli(low); Instant instant2 = Instant.ofEpochMilli(high); LocalDate localDate1 = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault()).toLocalDate(); LocalDate localDate2 = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault()).toLocalDate(); Period period = Period.between( localDate1 , localDate2 ); int years = period.getYears(); int months = period.getMonths(); BigDecimal monthToYear = new BigDecimal(months).divide(BigDecimal.valueOf(12), 2, RoundingMode.DOWN); return BigDecimal.valueOf(years).add(monthToYear); } /** * 是否是同一天 */ public static boolean isSameDateByUnixTime(long time1, long time2) { LocalDateTime dateTime1 = Instant.ofEpochMilli(time1).atZone(ZoneId.systemDefault()).toLocalDateTime(); LocalDateTime dateTime2 = Instant.ofEpochMilli(time2).atZone(ZoneId.systemDefault()).toLocalDateTime(); return dateTime1.toLocalDate().isEqual(dateTime2.toLocalDate()); } }
测试类
import com.chujianyun.common.java8.date.DateUtil; import org.junit.Assert; import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.ParseException; import java.time.LocalDate; import java.time.format.DateTimeFormatter; class DateUtilTest { private String today = "2019-05-10"; private DateTimeFormatter dateTimeFormatter = DateUtil.defaultDateTimeFormatter; @Test void lastDay() { LocalDate localDate = DateUtil.lastDay(today, dateTimeFormatter); Assert.assertEquals("2019-05-09", dateTimeFormatter.format(localDate)); } @Test void thisWeekFirstDay() { LocalDate localDate = DateUtil.thisWeekFirstDay(today, dateTimeFormatter); Assert.assertEquals("2019-05-06", dateTimeFormatter.format(localDate)); } @Test void lastWeekFirstDay() { LocalDate localDate = DateUtil.lastWeekFirstDay(today, dateTimeFormatter); Assert.assertEquals("2019-04-29", dateTimeFormatter.format(localDate)); } @Test void thisMonthFirstDay() { LocalDate localDate = DateUtil.thisMonthFirstDay(today, dateTimeFormatter); Assert.assertEquals("2019-05-01", dateTimeFormatter.format(localDate)); } @Test void lastMonthFirstDay() { LocalDate localDate = DateUtil.lastMonthFirstDay(today, dateTimeFormatter); Assert.assertEquals("2019-04-01", dateTimeFormatter.format(localDate)); } @Test public void test() throws ParseException { // 2018-01-01 --> 1514736000000 // 2018-01-01 --> 1546272000000 BigDecimal year = DateUtil.year(1514736000000L, 1546272000000L); BigDecimal expect = new BigDecimal(1); BigDecimal bigDecimal = expect.setScale(2, BigDecimal.ROUND_HALF_UP); Assert.assertEquals(bigDecimal, year); } @Test public void isSameDay() { boolean sameDateByUnixTime = DateUtil.isSameDateByUnixTime(1514779260000L, 1514818860000L); Assert.assertTrue(sameDateByUnixTime); } }