| | |
| | | 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 lombok.extern.slf4j.Slf4j; |
| | | |
| | |
| | | public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd"); |
| | | public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM"); |
| | | public static SimpleDateFormat format_yyymmdd = new SimpleDateFormat("yyyyMMdd"); |
| | | public static SimpleDateFormat format_yyyy = new SimpleDateFormat("yyyy"); |
| | | private static DateTimeFormatter format_ymdhmssss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); |
| | | private static DateTimeFormatter format_ymds = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | private static DateTimeFormatter format_yms = DateTimeFormatter.ofPattern("yyyyMM"); |
| | | private static DateTimeFormatter format_y = DateTimeFormatter.ofPattern("yyyy"); |
| | | |
| | | /** |
| | | * 将日期对象格式化成指定的字符串格式 |
| | |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | // Date date= new Date(); |
| | | // Date after = new Date(); |
| | | // System.out.println(getFirstDayOfMonthString()); |
| | | // System.out.println(getDayOfMonthString()); |
| | | Date date= getLastMonthFirst(); |
| | | Date after = getLastMonthEnd(); |
| | | System.out.println(date); |
| | | System.out.println(after); |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年月字符串 |
| | | * @return 年月字符串 |
| | | */ |
| | | public static String getCurrentDateString_YYYY_MM() { |
| | | String nowtime = getCurrentDate().format(format_yms); |
| | | return nowtime; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年字符串 |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateString_YYYY() { |
| | | String nowtime = getCurrentDate().format(format_y); |
| | | return nowtime; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年第一天 |
| | | * @return 当前年第一天时间 |
| | | */ |
| | | public static Date getFirstDayOfYear() { |
| | | Calendar currCal=Calendar.getInstance(); |
| | | int currentYear = currCal.get(Calendar.YEAR); |
| | | return getYearFirst(currentYear); |
| | | } |
| | | |
| | | /** |
| | | * 获取某年第一天日期 |
| | | * @param year 年份 |
| | | * @return Date |
| | | */ |
| | | public static Date getYearFirst(int year){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.clear(); |
| | | calendar.set(Calendar.YEAR, year); |
| | | Date currYearFirst = calendar.getTime(); |
| | | return currYearFirst; |
| | | } |
| | | |
| | | /** |
| | | * 获取上月第一天时间 |
| | | * @return 上月第一天时间 |
| | | */ |
| | | public static Date getLastMonthFirst(){ |
| | | // 上月起始 |
| | | Calendar lastMonthFirstDateCal = Calendar.getInstance(); |
| | | lastMonthFirstDateCal.add(Calendar.MONTH,-1); |
| | | lastMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1); |
| | | String lastMonthFirstTime = yyyyMMdd_format.format(lastMonthFirstDateCal.getTime()) + " 00:00:00"; |
| | | return stringToDateStandard(lastMonthFirstTime); |
| | | } |
| | | |
| | | /** |
| | | * 获取上月最后一天时间 |
| | | * @return 最后一天时间 |
| | | */ |
| | | public static Date getLastMonthEnd(){ |
| | | // 上月末尾 |
| | | Calendar lastMonthEndDateCal = Calendar.getInstance(); |
| | | lastMonthEndDateCal.add(Calendar.MONTH,-1); |
| | | lastMonthEndDateCal.set(Calendar.DAY_OF_MONTH, lastMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| | | String lastMonthEndTime = yyyyMMdd_format.format(lastMonthEndDateCal.getTime()) + " 23:59:59"; |
| | | return stringToDateStandard(lastMonthEndTime); |
| | | } |
| | | |
| | | /** |
| | | * 获取近20天日期 |
| | | * @return 近20天日期 |
| | | */ |
| | | public static List<Date> getOldDays() { |
| | | List<Date> dates = new ArrayList<>(); |
| | | Date date = new Date(); |
| | | for (int i = 19; i >= 0; i--) { |
| | | Date date1 = DateUtils.addDay(date, -i); |
| | | dates.add(date1); |
| | | } |
| | | return dates; |
| | | } |
| | | } |