/*** 获取当前时间的上月当天* @param formatPattern 指定日期格式* @return*/public static String getNowLastMonthThisDay(String formatPattern){LocalDateTime now1 = w();//获取上个月的当天(这一天)miuns()获取前面月份的方法,参数1为减去的数量,参数2为减去的对象(日,月)LocalDateTime minus = now1.minus(1, ChronoUnit.MONTHS);String lastMonthThisDay = minus.format(DateTimeFormatter.ofPattern(formatPattern));return lastMonthThisDay;}/*** 获取当前时间的去年当月* @param formatPattern* @return*/public static String getNowLastYearThisMonth(String formatPattern){LocalDateTime now1 = w();//获取上个月的当天(这一天)miuns()获取前面月份的方法,参数1为减去的数量,参数2为减去的对象(日,月)LocalDateTime minus = now1.minus(1, ChronoUnit.YEARS);String lastMonthThisDay = minus.format(DateTimeFormatter.ofPattern(formatPattern));return lastMonthThisDay;}/*** 获取指定日期的上月当天* @param assignTime 指定日期* @param formatPattern 指定日期格式* @return*/public static String getAssignLastMonthThisDay(String assignTime,String formatPattern){DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime now1 = LocalDateTime.parse(assignTime,format);//获取上个月的当天(这一天)miuns()获取前面月份的方法,参数1为减去的数量,参数2为减去的对象(日,月)LocalDateTime minus = now1.minus(1, ChronoUnit.MONTHS);String lastMonthThisDay = minus.format(DateTimeFormatter.ofPattern(formatPattern));return lastMonthThisDay;}/*** 获取指定日期的去年当月* @param assignTime* @param formatPattern* @return*/public static String getAssignLastYearThisMonth(String assignTime,String formatPattern){DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime now1 = LocalDateTime.parse(assignTime,format);//获取上个月的当天(这一天)miuns()获取前面月份的方法,参数1为减去的数量,参数2为减去的对象(日,月)LocalDateTime minus = now1.minus(1, ChronoUnit.YEARS);String lastMonthThisDay = minus.format(DateTimeFormatter.ofPattern(formatPattern));return lastMonthThisDay;}
//获取当前时间的上月当天String nowLastMonthThisTime = getNowLastMonthThisDay("yyyy-MM-dd");System.out.println("当前时间的上月当天" + nowLastMonthThisTime);//获取指定时间的上月当天String assign1 = getAssignLastMonthThisDay("2023-05-31 09:45:20", "yyyy-MM-dd HH:mm:ss");System.out.println("指定时间"+"2023-05-31 09:45:20"+"的上月当天" + assign1);String assign2 = getAssignLastMonthThisDay("2023-05-30 09:45:20", "yyyyMMdd");System.out.println("指定时间"+"2023-05-30 09:45:20"+"的上月当天" + assign2);String assign3 = getAssignLastMonthThisDay("2023-03-28 09:45:20", "yyyyMMdd");System.out.println("指定时间"+"2023-03-28 09:45:20"+"的上月当天" + assign3);String assign4 = getAssignLastMonthThisDay("2023-03-29 09:45:20", "yyyy-MM-dd HH:mm:ss");System.out.println("指定时间"+"2023-03-29 09:45:20"+"的上月当天" + assign4);String nowLastYearThisMonth = getNowLastYearThisMonth( "yyyyMMdd");System.out.println("当前时间的去年当月" + nowLastYearThisMonth);String assign5 = getAssignLastYearThisMonth("2023-02-29 09:45:20", "yyyyMM");System.out.println("指定时间"+"2023-02-29 09:45:20"+"的去年当月" + assign5);
本文发布于:2024-01-28 21:13:43,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170644762610325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |