java8新特性-日期类

在JDK 8之前,其实有不少的API都存在着一些问题,功能上也并不强大,日期时间等相关类同样如此。所以从JDK 8开始,Java做了较大的改动,出现了很多新特性。其中,java.time包中了就提供了不少强大的日期和时间API,如下:

  • 本地日期和时间类(业务常用,重点):LocalDateTime(日期和时间),LocalDate(日期),LocalTime(时间)
  • 带时区的日期和时间类:ZonedDateTime
  • 时刻类:Instant
  • 时间间隔:Duration

在格式化操作方面,也推出了一个新的格式化类DateTimeFormatter

和之前那些旧的API相比,新的时间API严格区分了时刻、本地日期、本地时间和带时区的日期时间,日期和时间的运算更加方便,功能更加强大,后续可以深刻体会到。这些新的API类型几乎全都是final不变类型,我们不必担心其会被修改。并且新的API还修正了旧API中一些不合理的常量设计:

● Month的范围采用1~12,表示1月到12月

● Week的范围采用1~7,表示周一到周日

注:由于LocalDateTime是包含日期和时间,LocalDate只包含日期,LocalTime只包含时间,我们可以根据业务要求选择不同的日期时间类,这三个的操作基本一致,以LocalDateTime为例

LocalDateTime

​ LocalDateTime是JDK 8之后出现的,用来表示本地日期和时间的类。我们可以通过now()方法,默认获取到本地时区的日期和时间。与之前的旧API不同,LocalDateTime、LocalDate和LocalTime默认会严格按照ISO 8601规定的日期和时间格式进行打印。

获取当前时间

在LocalDateTime中,我们可以通过now()方法获取当前的日期和时间

System.out.println(LocalDateTime.now());

输出结果

2024-01-07T14:49:46.865

可以看得出来LocalDateTime的now()方法可以获取的本地时区的日期和时间,时间可以精确到毫秒

我们发现输出的时间格式并不是我们想要的格式,我们可以通过DateTimeFormatter进行实践格式化

        //指定时间的格式DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");//获取当前时间LocalDateTime now = LocalDateTime.now();//格式化System.out.println(now.format(ofPattern));

输出结果

2024-01-07 03:04:55

如果我们只想通过LocalDateTime获取日期或者只获取时间呢?可以通过toLocalDate/toLocalTime进行转化实现。

        //获取当前时间LocalDateTime now = LocalDateTime.now();//获取日期System.out.println(now.toLocalDate());//获取时间System.out.println(now.toLocalTime());

输出结果

2024-01-07
15:12:07.561

判断两个日期时间前后顺序

业务中比较两个日期时间的先后顺序常用的方法为isEqual()、isAfter()、isBefore()

isEqual():返回true标识两个日期时间相等

isAfter():返回true表示前一个日期时间在另一个日期时间之后,false则相反

isBefore():返回true表示前一个日期时间在另一个日期时间之前,false则相反

        //获取当前时间LocalDateTime begin = LocalDateTime.now();Thread.sleep(1000);LocalDateTime end = LocalDateTime.now();//时间比较System.out.println("俩时间是否相等:"+(begin.isEqual(end)?"相等":"不相等"));System.out.println(begin.isAfter(end)?"begin时间在end之后":"begin时间在end之前");System.out.println(begin.isBefore(end)?"begin时间在end之前":"begin时间在end之后");    

对日期时间进行加减操作

在LocalDateTime中,给我们提供了一系列的加减操作方法,使得我们可以很轻易的实现时间的加减操作,比如给某个日期或时间进行加或减的操作。

时间增加

使用plus()方法并指定时间和单位可以实现时间增加

        //获取当前时间LocalDateTime now = LocalDateTime.now();System.out.println(now);//时间增加System.out.println("时间增加了一天:"+now.plus(1, ChronoUnit.DAYS));System.out.println("时间增加了一小时:"+now.plus(1, ChronoUnit.HOURS));System.out.println("时间增加了一分钟:"+now.plus(1, ChronoUnit.MINUTES));System.out.println("时间增加了一秒:"+now.plus(1, ChronoUnit.SECONDS));System.out.println("时间增加了一毫秒:"+now.plus(1, ChronoUnit.MILLIS));

当然也可使用plusXxx()对进行某一单位进行时间增加

  • plusDays方法增加天数
  • plusWeeks方法增加一周数
  • plusHours方法增加小时数
  • plusMinutes方法增加分钟数
  • plusSeconds增加秒数
        //获取当前时间LocalDateTime now = LocalDateTime.now();System.out.println(now);//时间增加System.out.println("时间增加了一年:"+now.plusYears(1));System.out.println("时间增加了一月:"+now.plusMonths(1));System.out.println("时间增加了一周:"+now.plusWeeks(1));System.out.println("时间增加了一天:"+now.plusDays(1));System.out.println("时间增加了一小时:"+now.plusHours(1));System.out.println("时间增加了一分钟:"+now.plusMinutes(1));System.out.println("时间增加了一秒:"+now.plusSeconds(1));

运行结果

2024-01-07T16:16:27.937
时间增加了一年:2025-01-07T16:16:27.937
时间增加了一月:2024-02-07T16:16:27.937
时间增加了一周:2024-01-14T16:16:27.937
时间增加了一天:2024-01-08T16:16:27.937
时间增加了一小时:2024-01-07T17:16:27.937
时间增加了一分钟:2024-01-07T16:17:27.937
时间增加了一秒:2024-01-07T16:16:28.937

时间减少

使用minus()方法并指定时间和单位可以实现时间增加,使用方法同时间增加方法一致

当然也可使用minusXxx()对进行某一单位进行时间增加,使用方法同时间增加方法一致

计算两个日期时间间隔数

       //获取当前时间LocalDateTime date1 = LocalDateTime.now();//在当前时间增加一天零一个小时LocalDateTime date2 = date1.plusDays(2).plusHours(1);System.out.println("两个时间相差天数:"+date1.until(date2, ChronoUnit.DAYS));System.out.println("两个时间相差月数:"+date1.until(date2, ChronoUnit.MONTHS));System.out.println("两个时间相差年数:"+date1.until(date2, ChronoUnit.YEARS));System.out.println("两个时间相差小时数:"+date1.until(date2, ChronoUnit.HOURS));  // 24 * 2 +1 = 49。。。。。 

执行结果

两个时间相差天数:2
两个时间相差月数:0
两个时间相差年数:0
两个时间相差小时数:49

创建指定日期时间的LocalDateTime类

我们可以通过of()方法,根据指定的日期和时间来创建一个LocalDateTime对象

方法一:传入年月日时分秒对应的数字

LocalDateTime data = LocalDateTime.of(2024, 1, 7, 12, 0, 0);System.out.println(data);

方法二:传入LocalDate对象作为日期,传入LocalTime对象作为时间

LocalDateTime dataNow = LocalDateTime.of(LocalDate.now(), LocalTime.now());System.out.println(dataNow);

复杂的日期时间运算

LocalDateTime中有一个通用的with()方法,允许我们进行更复杂的运算,比如获取当月或下个月的第一天、最后一天、第一个周一等操作。

如下:

       //获取当前时间LocalDateTime now = LocalDateTime.now();System.out.println("当前时间为:"+now);//获取本年最后一天System.out.println("本年最后一天为:"+now.with(TemporalAdjusters.lastDayOfYear()));//获取本月最后一天System.out.println("本月最后一天为:"+now.with(TemporalAdjusters.lastDayOfMonth()));//获取下个月第一天System.out.println("下个月第一天为:"+now.with(TemporalAdjusters.firstDayOfNextMonth()));

执行结果


DateTimeFormatter类详解

在学习Date日期时间对象时,知道该对象默认输出的时间格式其实是不符合大多数的使用场景的,所以就需要我们对其进行格式化设置,我们常常通过SimpleDateFormat类来实现。但是当我们使用新的LocalDateTime或ZonedDateTime需要进行格式化显示时,就要使用新的DateTimeFormatter类了。

和SimpleDateFormat不同的是,DateTimeFormatter不但是不可变的对象,且还是线程安全的。在代码中,我们可以创建出一个DateTimeFormatte实例对象,到处引用。而之前的SimpleDateFormat是线程不安全的,使用时只能在方法内部创建出一个新的局部变量。

2. 创建方式

我们要想使用DateTimeFormatter,首先得创建出一个DateTimeFormatter对象,一般有如下两种方式:

● DateTimeFormatter.ofPattern(String pattern):pattern是待传入的格式化字符串;

● DateTimeFormatter.ofPattern(String pattern,Locale locale):locale是所采用的本地化设置。

3. 基本使用

DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");String localDate = LocalDateTime.now().format(ofPattern);System.out.println(localDate);

包装日期工具类

public class LocalDateUtils {/*** 年-月-日 时:分:秒:毫秒 最全版*/public static final String DEFAULT_PATTERN_DATETIME_FULL = "yyyy-MM-dd HH:mm:ss.SSS";/*** 年-月-日 时:分:秒:毫秒 中文版*/public static final String DEFAULT_PATTERN_DATETIME_FULL_CHINESE = "yyyy年MM月dd日 HH:mm:ss.SSS";/*** 年-月-日 时:分:秒:毫秒 中文版全*/public static final String DEFAULT_PATTERN_DATETIME_CHINESE_FULL = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";/*** 年-月-日 时:分:秒 标准样式*/public static final String DEFAULT_PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";/*** 年-月-日 时:分:秒 中文版1*/public static final  String DEFAULT_PATTERN_DATETIME_CHINESE_ONE = "yyyy年MM月dd日 HH:mm:ss";/*** 年-月-日 时:分:秒 中文版2*/public static final String DEFAULT_PATTERN_DATETIME_CHINESE_TWO = "yyyy年MM月dd日 HH时mm分ss秒";/*** 年-月-日*/public static final String DEFAULT_PATTERN_DATE = "yyyy-MM-dd";/*** 年-月-日 中文版*/public static final String DEFAULT_PATTERN_DATE_CHINESE = "yyyy年MM月dd日";/*** 年-月 中文版*/public static final String DEFAULT_PATTERN_DATE_YEAR_MONTH_CHINESE = "yyyy年MM月";/*** 月-日*/public static final String DEFAULT_PATTERN_MONTH = "MM-dd";/*** 日*/public static final String DEFAULT_PATTERN_DAY = "dd";/*** 时:分:秒*/public static final String DEFAULT_PATTERN_TIME = "HH:mm:ss";/*** 年月日时分秒毫秒  紧凑版*/public static final String DEFAULT_PATTERN_DATETIME_SIMPLE_FULL = "yyyyMMddHHmmssSSS";/*** 年月日时分秒*/public static final String DEFAULT_PATTERN_DATETIME_SIMPLE = "yyyyMMddHHmmss";/*** 年月日*/public static final String DEFAULT_PATTERN_DATETIME_DATE = "yyyyMMdd";/*** 月日*/public static final String DEFAULT_PATTERN_DATETIME_MONTH = "MMdd";/*** 时分秒毫秒*/public static final String DEFAULT_PATTERN_DATETIME_TIME_FULL = "HHmmss";/*** 年月*/public static final String DEFAULT_PATTERN_YEAR_MOTH = "yyyy-MM";/*** 格式化年月日为字符串** @param localDate 传入需要格式化的日期* @param pattern   需要格式化的格式* @return String 返回格式化的日期*/public static String formatterLocalDateToString(LocalDate localDate, String pattern) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);return getLocalDateFormat(localDate, dateTimeFormatter);}/*** 格式化年月日时分秒为字符串** @param localDateTime 传入需要格式化的日期* @param pattern       需要格式化的格式* @return String 返回格式化的日期*/public static String formatterLocalDateTimeToString(LocalDateTime localDateTime, String pattern) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);return getLocalDateTimeFormat(localDateTime, dateTimeFormatter);}/*** 时区格式化年月日为字符串** @param localDate 传入需要格式化的日期* @param pattern   需要格式化的格式* @param locale    国际时区   Locale.CHINA* @return String 返回格式化的日期*/public static String formatterLocalDateToString(LocalDate localDate,String pattern,Locale locale) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern, locale);return getLocalDateFormat(localDate, dateTimeFormatter);}/*** 时区格式化年月日时分秒为字符串** @param localDateTime 传入需要格式化的日期* @param pattern       需要格式化的格式* @param locale        国际时区 Locale.CHINA* @return String 返回格式化的日期*/public static String formatterLocalDateTimeToString(LocalDateTime localDateTime,String pattern,Locale locale) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern, locale);return getLocalDateTimeFormat(localDateTime, dateTimeFormatter);}/*** LocalDate格式化日期为日期格式** @param localDate 传入需要格式化的日期* @param pattern   需要格式化的格式* @return String 返回格式化的日期*/public static LocalDate formatterStringToLocalDate(String localDate, String pattern) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);return getLocalDateParse(localDate, dateTimeFormatter);}/*** localDateTime格式化日期为日期格式** @param localDateTime 传入需要格式化的日期* @param pattern       需要格式化的格式* @return String 返回格式化的日期*/public static LocalDateTime formatterStringToLocalDateTime(String localDateTime, String pattern) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);return getLocalDateTimeParse(localDateTime, dateTimeFormatter);}/*** 日期格式化日期,转化为日期格式 localDate 转 LocalDate** @param localDate 传入的日期* @param pattern   转化的格式* @return 返回结果 LocalDate*/public static LocalDate formatterDateToLocalDateTime(LocalDate localDate,String pattern) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);String formatterDateTime = getLocalDateFormat(localDate, dateTimeFormatter);return getLocalDateParse(formatterDateTime);}/*** 日期格式化日期,转化为日期格式 localDateTime 转 localDateTime** @param localDateTime 传入需要格式化的日期* @param pattern       需要格式化的格式* @return String 返回格式化的日期*/public static LocalDateTime formatterDateTimeToLocalDateTime(LocalDateTime localDateTime,String pattern) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);String formatterDateTime = getLocalDateTimeString(localDateTime, dateTimeFormatter);return getLocalDateTimeParse(formatterDateTime, dateTimeFormatter);}/*** 格式化日期格式 yyyy-MM-dd HH:mm:ss** @param localDateTime 传入需要格式化的日期* @return 返回格式化后的日期字符串*/public static String getAllFormatterLocalDateTime(LocalDateTime localDateTime) {DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(DEFAULT_PATTERN_DATETIME);return getLocalDateTimeFormat(localDateTime, dateTimeFormatter);}/*** 获取LocalDate转化为字符串** @param formatterDateTime 需要转化的数据* @return LocalDate*/private static LocalDate getLocalDateParse(String formatterDateTime) {return LocalDate.parse(formatterDateTime);}/*** 获取LocalDate按要求转化为字符串** @param formatterDateTime 需要转化的数据* @param dateTimeFormatter 格式化* @return LocalDate*/private static LocalDate getLocalDateParse(String formatterDateTime, DateTimeFormatter dateTimeFormatter) {return LocalDate.parse(formatterDateTime, dateTimeFormatter);}/*** 获取LocalDate按照要求转化为字符串** @param localDate         需要转化的数据* @param dateTimeFormatter 转化的格式* @return 转化后返回字符串*/private static String getLocalDateFormat(LocalDate localDate, DateTimeFormatter dateTimeFormatter) {return dateTimeFormatter.format(localDate);}/*** 获取LocalDateTime按照要求转化为字符串** @param localDateTime     需要转化的数据* @param dateTimeFormatter 转化的格式* @return 返回结果*/private static String getLocalDateTimeFormat(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {return localDateTime.format(dateTimeFormatter);}/*** 获取数据按照国际标准转化的值** @param pattern 需要转化的数据* @param locale  传入国际时间* @return 返回格式结果*/private static DateTimeFormatter getDateTimeFormatter(String pattern, Locale locale) {return DateTimeFormatter.ofPattern(pattern, locale);}/*** 获取localDateTime按照国际标准转化的值** @param localDateTime     需要转化的数据* @param dateTimeFormatter 格式化* @return 返回 LocalDateTime*/private static LocalDateTime getLocalDateTimeParse(String localDateTime, DateTimeFormatter dateTimeFormatter) {return LocalDateTime.parse(localDateTime, dateTimeFormatter);}/*** 获取格式化的结果** @param pattern 传入的格式* @return 返回格式化结果*/private static DateTimeFormatter getDateTimeFormatter(String pattern) {return DateTimeFormatter.ofPattern(pattern);}/*** 获取格式化LocalDateTime结果** @param localDateTime     传入的数据* @param dateTimeFormatter 格式化的结果* @return 返回字符串*/private static String getLocalDateTimeString(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {return dateTimeFormatter.format(localDateTime);}/*** 获取今天星期几** @param instant JDK8 代替Date使用的类* @return 当前的星期*/private static DayOfWeek getDayOfWeek(Instant instant) {// ZoneId.systemDefault() 设置当前时区为系统默认时区LocalDateTime localDateTime = getLocalDateAboutInstant(instant);return localDateTime.getDayOfWeek();}/*** 获取instant 转化的日期格式** @param instant DK8 代替Date使用的类* @return 时间格式*/private static LocalDateTime getLocalDateAboutInstant(Instant instant) {return instantToLocalDateTime(instant);}/*** 获取本周开始时间** @param localDateTime 输入日期* @return 返回本周开始时间*/public static LocalDateTime getTodayFirstOfWeek(LocalDateTime localDateTime) {TemporalAdjuster temporalAdjuster = getFirstTemporalAdjuster();return localDateTime.with(temporalAdjuster);}/*** 获取本周结束时间** @param localDateTime 输入日期* @return 本周结束时间*/public static LocalDateTime getTodayLastOfWeek(LocalDateTime localDateTime) {TemporalAdjuster temporalAdjuster = getLastTemporalAdjuster();return localDateTime.with(temporalAdjuster);}/*** 本周开始时间** @return 返回本周开始时间*/private static TemporalAdjuster getFirstTemporalAdjuster() {return TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));}/*** 本周结束时间** @return 返回本周结束时间*/private static TemporalAdjuster getLastTemporalAdjuster() {return TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));}/*** 获取天的开始时间** @param day 0 今天  1 明天 -1 昨天* @return 开始时间*/public static LocalDateTime getTodayStartTime(int day) {return LocalDate.now().plusDays(day).atStartOfDay();}/*** 获取某月的开始时间** @param month 0 本月  1 下月 -1 上月* @return 月开始的时间*/public static LocalDate getMonthStartTime(int month) {return LocalDate.now().plusMonths(month).with(TemporalAdjusters.firstDayOfMonth());}/*** 获取某年的开始时间** @param year 0 今年  1 明年 -1 前年* @return 年的开始时间*/public static LocalDate getYearStartTime(int year) {return LocalDate.now().plusYears(year).with(TemporalAdjusters.firstDayOfYear());}/*** 获取某天的结束时间** @return 天的结束时间  ZoneId.systemDefault()系统默认时区,如果需要改变时区使用ZoneId.of("时区")*/public static LocalDateTime getTodayEndTime() {return LocalDateTime.of(LocalDate.now(ZoneId.systemDefault()), LocalTime.MIDNIGHT);}/*** 获取某月的结束时间** @param day 0 本月  1 下月 -1 上月* @return 月的结束时间*/public static LocalDate getMonthEndTime(int day) {return LocalDate.now().plusDays(day).with(TemporalAdjusters.lastDayOfMonth());}/*** 获取某年的结束时间** @param year 0 今年  1 明年 -1 前年* @return 年的结束时间*/public static LocalDate getYearEndTime(int year) {return LocalDate.now().plusYears(year).with(TemporalAdjusters.lastDayOfYear());}/*** 获取今天中午的时间** @return 今天中午的时间*/public static LocalDateTime getTodayNoonTime() {return LocalDateTime.of(LocalDate.now(ZoneId.systemDefault()), LocalTime.NOON);}/*** 获取月份** @param month 月份* @return 数值*/private static int getMonth(Month month) {return month.getValue();}/*** 在日期上增加数个整天** @param instant 输入日期* @param day     增加或减少的天数* @return 增加或减少后的日期*/public static LocalDateTime addDay(Instant instant,int day) {LocalDateTime localDateAboutInstant = getLocalDateAboutInstant(instant);return localDateAboutInstant.plusDays(day);}/*** 在日期上增加/减少(负数)数个小时** @param instant 输入时间* @param hour    增加/减少的小时数* @return 增加/减少小时后的日期*/public static LocalDateTime addDateHour(Instant instant, int hour) {LocalDateTime localDateAboutInstant = getLocalDateAboutInstant(instant);return localDateAboutInstant.plusHours(hour);}/*** 在日期上增加/减少数个分钟** @param instant 输入时间* @param minutes 增加/减少的分钟数* @return 增加/减少分钟后的日期*/public static LocalDateTime addDateMinutes(Instant instant,int minutes) {LocalDateTime localDateAboutInstant = getLocalDateAboutInstant(instant);return localDateAboutInstant.plusMinutes(minutes);}/*** 得到两个日期时间的差额(毫秒)** @param startTime 开始时间* @param endTime   结束时间* @return 两个时间相差多少秒*/public static long differenceDateMillis(LocalDateTime startTime,LocalDateTime endTime) {Duration between = Duration.between(startTime, endTime);return between.toMillis();}/*** 得到两个日期时间的差额(分)** @param startTime 开始时间* @param endTime   结束时间* @return 两个时间相差多少分*/public static long differenceDateMinutes(LocalDateTime startTime,LocalDateTime endTime) {Duration between = Duration.between(startTime, endTime);return between.toMinutes();}/*** 得到两个日期时间的差额(小时)** @param startTime 开始时间* @param endTime   结束时间* @return 两个时间相差多少小时*/public static long differenceDateHours(LocalDateTime startTime,LocalDateTime endTime) {Duration between = Duration.between(startTime, endTime);return between.toHours();}/*** 得到两个日期时间的差额(天)** @param startTime 开始时间* @param endTime   结束时间* @return 两个时间相差多少天*/public static long differenceDateDays(LocalDateTime startTime,LocalDateTime endTime) {Duration between = Duration.between(startTime, endTime);return between.toDays();}/*** 获取指定日期的月份** @param localDateTime 输入日期* @return 返回指定日期的月份*/public static int getMonthAboutLocalTime(LocalDateTime localDateTime) {Month month = localDateTime.getMonth();return getMonth(month);}/*** 获取指定日期的年份** @param localDateTime 输入日期* @return 返回指定日期的年份*/public static int getYearAboutLocalTime(LocalDateTime localDateTime) {return localDateTime.getYear();}/*** 获取指定日期的天数** @param localDateTime 输入日期* @return 返回指定日期的天数*/public static int getDayAboutLocalTime(LocalDateTime localDateTime) {return localDateTime.getDayOfMonth();}/*** 获取指定日期的星期** @param localDateTime 输入日期* @return 返回指定日期的星期*/public static int getWeekDayAboutLocalTime(LocalDateTime localDateTime) {return localDateTime.getDayOfWeek().getValue();}/*** 获取指定日期的时间** @param localDateTime 输入日期* @return 返回指定日期的时间*/public static int getHouryAboutLocalTime(LocalDateTime localDateTime) {return localDateTime.getHour();}/*** 获取指定日期的所在月的最后一天** @param localDateTime 输入日期* @return 返回指定日期的时间*/public static int getLateDayFromMonth(LocalDateTime localDateTime) {return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();}/*** 获取指定日期的所在月的第一天** @param localDateTime 输入日期* @return 返回指定日期的时间*/public static LocalDateTime getFirstDayFromMonth(LocalDateTime localDateTime) {return localDateTime.with(TemporalAdjusters.firstDayOfMonth());}public static LocalDateTime getDayOfWeek(Instant instant, int week) {LocalDateTime localDateTime = instantToLocalDateTime(instant);DayOfWeek plus = localDateTime.getDayOfWeek().plus(week);return addDay(localDateTime.atZone(ZoneId.systemDefault()).toInstant(), plus.getValue() - week);}/*** 获取到指定日期一个月有几天** @param instant 输入日期* @return 天数*/public static int getDayCountOfMonth(Instant instant) {LocalDateTime localDateTime = instantToLocalDateTime(instant);LocalDateTime startTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth());LocalDateTime endTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth());Duration between = Duration.between(startTime, endTime);return (int) between.toDays() + 1;}/*** 当前时间10位毫秒数** @return 10位秒*/public static long getNowOfSecond() {LocalDateTime localDateTime = LocalDateTime.now();return localDateTimeToInstant(localDateTime).plusMillis(TimeUnit.HOURS.toMillis(8)).getEpochSecond();}/*** 当前时间13位毫秒数** @return 13位毫秒数*/public static long getNowOfMillion() {LocalDateTime localDateTime = LocalDateTime.now();return localDateTimeToInstant(localDateTime).toEpochMilli();}/*** 当前时间13位毫秒数** @return 13位毫秒数*/public static long getNowOfMillions() {LocalDateTime localDateTime = LocalDateTime.now();return localDateTimeToInstant(localDateTime).plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();}/*** LocalDateTime 转化为 Instant** @param localDateTime 输入的时间* @return Instant的日期类型*/public static Instant localDateTimeToInstant(LocalDateTime localDateTime) {return localDateTime.atZone(ZoneId.systemDefault()).toInstant();}/*** LocalDateTime 转化为 LocalDate** @param localDateTime 输入的时间* @return LocalDate的日期类型*/public static LocalDate localDateTimeTolocalDate(LocalDateTime localDateTime) {return localDateTime.toLocalDate();}/*** LocalDate 转化为 LocalDateTime** @param localDate 输入的时间* @return LocalDateTime的日期类型*/public static LocalDateTime localDateTimeTolocalDate(LocalDate localDate) {return localDate.atStartOfDay(ZoneOffset.ofHours(8)).toLocalDateTime();}/*** instant  转化为 LocalDateTime** @param instant 输入的时间* @return LocalDateTime的日期类型*/public static LocalDateTime instantToLocalDateTime(Instant instant) {return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());}/*** instant  转化为 LocalDate** @param instant 输入的时间* @return LocalDate的日期类型*/public static LocalDate instantToLocalDate(Instant instant) {return instant.atZone(ZoneOffset.ofHours(8)).toLocalDate();}/*** 获取本周的周一日期** @return 获取本周的周一日期*/public static LocalDateTime getMondayThisWeek() {LocalDateTime todayFirstOfWeek = getTodayFirstOfWeek(getTodayStartTime(0));return todayFirstOfWeek.with(getFirstTemporalAdjuster());}/*** 获取本周的周日日期** @return 获取本周的周日日期*/public static LocalDateTime getSundayThisWeek() {LocalDateTime todayFirstOfWeek = getTodayLastOfWeek(getTodayStartTime(0));return todayFirstOfWeek.with(getFirstTemporalAdjuster());}/*** 获取上周周一的日期** @return 获取上周周一的日期*/public static LocalDateTime getMondayPreviousWeek() {return getMondayThisWeek().minusDays(7);}/*** 获取上周周日的日期** @return 获取上周周日的日期*/public static LocalDateTime getSundayPreviousWeek() {return getSundayThisWeek().minusDays(8);}/*** LocalDate转时间戳** @param localDate 时间输入* @return 时间戳*/public static Long getLocalDateToMillis(LocalDate localDate) {return localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();}/*** LocalDateTime转时间戳** @param localDateTime 时间输入* @return 时间戳*/public static Long getLocalDateTimeToMillis(LocalDateTime localDateTime) {return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();}/*** 毫秒值转LocalDate** @param timeStamp 时间戳毫秒值* @return LocalDate*/public static LocalDate getMillisToLocalDate(long timeStamp) {return Instant.ofEpochMilli(timeStamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();}/*** 毫秒值转LocalDateTime** @param timeStamp 时间戳毫秒值* @return LocalDateTime*/public static LocalDateTime getMillisToLocalDateTime(long timeStamp) {return Instant.ofEpochMilli(timeStamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();}/*** 毫秒值加一年后的毫秒值** @param original 毫秒值* @return 毫秒值*/public static long getNextYear(long original) {LocalDateTime millisToLocalDateTime = getMillisToLocalDateTime(original);return millisToLocalDateTime.minus(1, ChronoUnit.YEARS).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/605885.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

可怜的小猪

题目 有 buckets 桶液体,其中 正好有一桶 含有毒药,其余装的都是水。它们从外观看起来都一样。为了弄清楚哪只水桶含有毒药,你可以喂一些猪喝,通过观察猪是否会死进行判断。不幸的是,你只有 minutesToTest 分钟时间来…

msvcp140.dll丢失的解决方法,从两个方向解决msvcp140.dll丢失

在Windows操作系统上,msvcp140.dll是Visual C Redistributable for Visual Studio 2015的一部分,如果msvcp140.dll文件丢失,可能在尝试启动使用C运行时库的程序时遇到错误,应用程序可能也会相应的无法打开,那么有什么m…

「HDLBits题解」Vector1

本专栏的目的是分享可以通过HDLBits仿真的Verilog代码 以提供参考 各位可同时参考我的代码和官方题解代码 或许会有所收益 题目链接:Vector1 - HDLBits default_nettype none // Disable implicit nets. Reduces some types of bugs. module top_module( input…

Qt/C++摄像头采集/二维码解析/同时采集多路/图片传输/分辨率帧率可调/自动重连

一、前言 本地摄像头的采集可以有多种方式,一般本地摄像头会通过USB的方式连接,在嵌入式上可能大部分是CMOS之类的软带的接口,这些都统称本地摄像头,和网络摄像头最大区别就是一个是通过网络来通信,一个是直接本地通信…

浅谈顺序表基本操作

🤷‍♀️🤷‍♀️🤷‍♀️ 今天给大家带来的是数据结构——顺序表的实现(增删查改)。 清风的CSDN博客主页 🎉欢迎👍点赞✍评论❤️收藏 😛😛😛希望我的文章能对你有所帮助&#xff…

JavaWeb基础(2)- Web概述、HTTP协议、Servlet、Request与Response

JavaWeb基础(2)- Web概述、HTTP协议、Servlet、Request与Response 文章目录 JavaWeb基础(2)- Web概述、HTTP协议、Servlet、Request与Response3 Web概述3.1 Web和JavaWeb的概念3.2 JavaWeb技术栈3.2.1 B/S架构**3.2.2 静态资源**3…

不带控制器打包exe,转pdf文件时失败的原因

加了注释的两条代码后,控制器会显示一个docx转pdf的进度条。这个进度条需要控制器的实现,如果转exe不带控制器的话,当点击转换为pdf的按钮就会导致程序出错和闪退。 __init__.py文件的入口

Netplan介绍

1 介绍 1.1 简介 Netplan是一个抽象网络配置描述器。通过netplan命令,你只需用一个 YAML文件描述每个网络接口所需配置。netplan并不关系底层管理工具是NetworkManager还是networkd。 它是一个在 Linux 系统上进行网络配置的实用程序。您创建所需接口的描述并定义…

MAC OS更新系统后IDEA中的SVN报错无法使用

MAC OS更新系统后IDEA中的SVN报错无法使用 有没有大神懂

java: 5-3 for

文章目录 1. for1.1 基本语法1.2 练习1.3 执行流程1.4 细节1.5 编程思想 (练习) 1. for 1.1 基本语法 for 关键字,表示循环控制for 有四要素: (1)循环变量初始化(2)循环条件(3)循环操作(4)循环变量迭代循环操作 , 这里可以有多条语句,也就是我们要循环…

FreeRTOS学习第6篇–任务状态挂起恢复删除等操作

目录 FreeRTOS学习第6篇--任务状态挂起恢复删除等操作任务的状态设计实验IRReceiver_Task任务相关代码片段实验现象本文中使用的测试工程 FreeRTOS学习第6篇–任务状态挂起恢复删除等操作 本文目标:学习与使用FreeRTOS中的几项操作,有挂起恢复删除等操作…

在MeshLab中创建简单的几何对象

文章目录 立方体和平面网格正多面体圆形相关球类隐式曲面 在Filters->Create New Mesh Layer的子菜单中,提供了大量几何对象,列表如下 菜单指令图形菜单指令图形Dodecahedron正十二面体Icosahedron正二十面体Tetrahedron正四面体Octahedron正八面体B…

Kafka(五)生产者

目录 Kafka生产者1 配置生产者bootstrap.serverskey.serializervalue.serializerclient.id""acksallbuffer.memory33554432(32MB)compression.typenonebatch.size16384(16KB)max.in.flight.requests.per.connection5max.request.size1048576(1MB)receive.buffer.byte…

file.seek规定从txt文档的某个区间开始读取文件内容

with open(f,r,encodingutf-8) as file:file.seek(10)# 定位到文件中的第10个字节,10个字节之后开始读取content file.read(100) # 读取100个字节的内容print(content:\n,content)打印结果: 未截取的,全部内容 1200001 1233331 1244441 0000121 1200001…

xdoj托普利兹矩阵

#include <stdio.h> int main() {char Hn0,Cn0;int i0,n,j,h[10],c[10],a[10][10];while(Hn!\n)//输入 行向量{scanf("%d",&h[i]);i;scanf("%c",&Hn);}i0;while(Cn!\n)//输入 列向量{scanf("%d",&c[i]);i;scanf("%c&quo…

面试官,我准备好了!——亲身体验Java面试与攻略分享

面试官,我准备好了!——亲身体验Java面试与攻略分享 从大学时代频繁在电脑前敲下System.out.println("Hello, World!");的那一刻起,我就知道自己的未来与Java结下了不解之缘。时间过得飞快,转眼间我已经穿梭于各类Java面试中,积累了相当的经验。今天,我将和大…

目标检测中的常见指标

概念引入&#xff1a; TP&#xff1a;True Positive IoU > 阈值 检测框数量 FP: False Positive IoU < 阈值 检测框数量 FN: False Negative 漏检框数量 Precision:查准率 Recall:查全率&#xff08;召回率&#xff09; AP&am…

【精通C语言】:分支结构switch语句的灵活运用

&#x1f3a5; 屿小夏 &#xff1a; 个人主页 &#x1f525;个人专栏 &#xff1a; C语言详解 &#x1f304; 莫道桑榆晚&#xff0c;为霞尚满天&#xff01; 文章目录 &#x1f4d1;前言一、switch语句1.1 语法1.2 代码示例 二、switch的控制语句2.1 break2.2 defualt子句 三、…

PKI 公钥基础设施,公钥私钥,信息摘要,数字签名,数字证书

PKI 公钥基础设施 https 基于 PKI 技术。PKI&#xff08;Public Key Infrastructure&#xff0c;公钥基础设施&#xff09;是一种安全体系结构&#xff0c;用于管理数字证书和密钥对&#xff0c;以确保安全的数据传输和身份验证。PKI 采用了公钥加密技术&#xff0c;其中每个实…

【中小型企业网络实战案例 八】配置映射内网服务器和公网多出口、业务测试和保存配置

相关学习文章&#xff1a; 【中小型企业网络实战案例 一】规划、需求和基本配置 【中小型企业网络实战案例 二】配置网络互连互通【中小型企业网络实战案例 三】配置DHCP动态分配地址 【中小型企业网络实战案例 四】配置OSPF动态路由协议【中小型企业网络实战案例 五】配置可…