| | |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | @Slf4j |
| | |
| | | public static DateTimeFormatter format_ymdhms = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | public static DateTimeFormatter format_ymd = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | public static DateTimeFormatter format_ymdhms_string = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); |
| | | public static DateTimeFormatter format_ymdhms_yyyyMMddmmHHssSSS = DateTimeFormatter.ofPattern("yyyyMMddmmHHssSSS"); |
| | | public static DateTimeFormatter format_ymdhms_no_signal = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); |
| | | public static SimpleDateFormat yyyyMMdd_format = new SimpleDateFormat("yyyy-MM-dd"); |
| | | public static SimpleDateFormat yyyy_MM_dd_format = new SimpleDateFormat("yyyy/MM/ddHH:mm:ss"); |
| | |
| | | |
| | | long day = diff / nd; |
| | | |
| | | long hour = diff % nd / nh; |
| | | long hour = diff / nh; |
| | | |
| | | long min = diff % nd % nh / nm; |
| | | long min = diff / nm; |
| | | |
| | | return (int)day; |
| | | return (int)hour; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | Date date= getLastMonthFirst(); |
| | | Date after = getLastMonthEnd(); |
| | | System.out.println(date); |
| | | System.out.println(after); |
| | | |
| | | // List<String> beforeDays = getBeforeDays(15); |
| | | // System.out.println(beforeDays); |
| | | List<String> yearMonths = getYearMonths(); |
| | | System.out.println(yearMonths); |
| | | List<String> latest12Month = getLatest12Month(LocalDate.now(), 10); |
| | | System.out.println(latest12Month); |
| | | |
| | | } |
| | | |
| | |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | return format_yyymmdd.format(calendar.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 获取某年的月份最后一天时间 |
| | | * @param year 年份 |
| | | * @return 月份list |
| | | */ |
| | | public static List<String> getYearLastMonths(Integer year){ |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(getLastDayOfMonth(year,1)); |
| | | list.add(getLastDayOfMonth(year,2)); |
| | | list.add(getLastDayOfMonth(year,3)); |
| | | list.add(getLastDayOfMonth(year,4)); |
| | | list.add(getLastDayOfMonth(year,5)); |
| | | list.add(getLastDayOfMonth(year,6)); |
| | | list.add(getLastDayOfMonth(year,7)); |
| | | list.add(getLastDayOfMonth(year,8)); |
| | | list.add(getLastDayOfMonth(year,9)); |
| | | list.add(getLastDayOfMonth(year,10)); |
| | | list.add(getLastDayOfMonth(year,11)); |
| | | list.add(getLastDayOfMonth(year,12)); |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取某年的月份第一天时间 |
| | | * @param year 年份 |
| | | * @return 月份list |
| | | */ |
| | | public static List<String> getYearFirstMonths(Integer year){ |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(getFirstDayOfMonth(year,1)); |
| | | list.add(getFirstDayOfMonth(year,2)); |
| | | list.add(getFirstDayOfMonth(year,3)); |
| | | list.add(getFirstDayOfMonth(year,4)); |
| | | list.add(getFirstDayOfMonth(year,5)); |
| | | list.add(getFirstDayOfMonth(year,6)); |
| | | list.add(getFirstDayOfMonth(year,7)); |
| | | list.add(getFirstDayOfMonth(year,8)); |
| | | list.add(getFirstDayOfMonth(year,9)); |
| | | list.add(getFirstDayOfMonth(year,10)); |
| | | list.add(getFirstDayOfMonth(year,11)); |
| | | list.add(getFirstDayOfMonth(year,12)); |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取某年某月最后一天 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 某年某月最后一天 |
| | | */ |
| | | public static String getLastDayOfMonth(int year,int month) |
| | | { |
| | | Calendar cal = Calendar.getInstance(); |
| | | //设置年份 |
| | | cal.set(Calendar.YEAR,year); |
| | | //设置月份 |
| | | cal.set(Calendar.MONTH, month-1); |
| | | //获取某月最大天数 |
| | | int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); |
| | | //设置日历中月份的最大天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, lastDay); |
| | | //格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String lastDayOfMonth = sdf.format(cal.getTime()); |
| | | return lastDayOfMonth; |
| | | } |
| | | |
| | | /** |
| | | * 获取某年某月的第一天 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 某年某月第一天 |
| | | */ |
| | | public static String getFirstDayOfMonth(int year,int month) |
| | | { |
| | | Calendar cal = Calendar.getInstance(); |
| | | //设置年份 |
| | | cal.set(Calendar.YEAR,year); |
| | | //设置月份 |
| | | cal.set(Calendar.MONTH, month-1); |
| | | //获取某月最小天数 |
| | | int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | //设置日历中月份的最小天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, firstDay); |
| | | //格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String firstDayOfMonth = sdf.format(cal.getTime()); |
| | | |
| | | return firstDayOfMonth; |
| | | } |
| | | |
| | | /** |
| | | * 获取20天前那天时间 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getTwentyDay() { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(new Date()); |
| | | cal.add(Calendar.DATE, -20); |
| | | |
| | | return DateUtil.format(cal.getTime(),"yyyy-MM-dd"); |
| | | } |
| | | |
| | | /** |
| | | * 获取之前的日期 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getBeforeDay(Integer amount) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(new Date()); |
| | | cal.add(Calendar.DATE, -amount); |
| | | return DateUtil.format(cal.getTime(),"yyyy-MM-dd"); |
| | | } |
| | | |
| | | /** |
| | | * 获取最近本月20天数据 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getMonthTwentyDay() { |
| | | Date date = new Date(); |
| | | Long offset = DateUtil.between(new Date(), DateUtil.beginOfMonth(date), DateUnit.DAY); |
| | | if (offset >= 20) { |
| | | return getTwentyDay(); |
| | | } else { |
| | | //距离月初不足20天 |
| | | Calendar calendar = new GregorianCalendar(); |
| | | calendar.setTime(date); |
| | | //获得本月第一天 |
| | | calendar.add(Calendar.MONTH, 0); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | return DateUtil.format(calendar.getTime(),"yyyy-MM-dd"); |
| | | } |
| | | |
| | | } |
| | | |
| | | public static List<String> getBeforeDays(int before) { |
| | | List<String> dates = new ArrayList<>(); |
| | | for (int i = before; i >= 0; i--) { |
| | | dates.add(getBeforeDay(i)); |
| | | } |
| | | return dates; |
| | | } |
| | | |
| | | /** |
| | | * 获取当年所有月份 |
| | | * |
| | | * @return |
| | | */ |
| | | public static List<String> getYearMonths() { |
| | | Calendar instance = Calendar.getInstance(); |
| | | int year = instance.get(Calendar.YEAR); |
| | | List<String> months = new ArrayList<>(); |
| | | for (int i = 1; i <= 12; i++) { |
| | | String result = String.valueOf(year); |
| | | if (i < 10) { |
| | | result = result + "-0" +i; |
| | | } else { |
| | | result = result + "-"+i; |
| | | } |
| | | months.add(result); |
| | | } |
| | | return months; |
| | | } |
| | | |
| | | //获取当前12个月 |
| | | public static List<String> getLatest12Month(LocalDate date,int num){ |
| | | List<String> monthList = new ArrayList<>(); |
| | | for(int i = 0;i <= num-1; i++){ |
| | | LocalDate localDate = date.minusMonths(i); |
| | | String month = localDate.toString().substring(0,7); |
| | | monthList.add(month); |
| | | } |
| | | return monthList; |
| | | } |
| | | |
| | | |
| | | } |