| | |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.DateFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.*; |
| | |
| | | public static SimpleDateFormat ymdhms_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd"); |
| | | |
| | | public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM"); |
| | | |
| | | |
| | | |
| | |
| | | return newTime; |
| | | } |
| | | |
| | | public static boolean isValidDate(String str) { |
| | | boolean convertSuccess=true; |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); |
| | | try { |
| | | format.setLenient(false); |
| | | format.parse(str); |
| | | } catch (ParseException e) { |
| | | convertSuccess=false; |
| | | } |
| | | return convertSuccess; |
| | | } |
| | | |
| | | public static Date toValidDate(String str) { |
| | | Date date = null; |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); |
| | | try { |
| | | format.setLenient(false); |
| | | date = format.parse(str); |
| | | } catch (ParseException e) { |
| | | log.error("党员导入日期格式错误"); |
| | | date = new Date(); |
| | | } |
| | | return date; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将日期对象格式化成yyyy-MM-dd格式的字符串 |
| | | * |
| | | * @param date 日期对象 |
| | | * @return String |
| | | */ |
| | | public static String getDateStringYMD(Date date) { |
| | | |
| | | String dateString = ""; |
| | | if (date != null) { |
| | | dateString = yyyyMMdd_format.format(date); |
| | | } |
| | | return dateString; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月第一天 |
| | | * @return |
| | | */ |
| | | public static Date getFirstDayOfMonth() { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.add(Calendar.MONTH,0); |
| | | int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年月的第一天时间 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 时间 |
| | | */ |
| | | public static Date getYearMonthStart(Integer year,Integer month) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 设置年份 |
| | | cal.set(Calendar.YEAR, year); |
| | | // 设置月份 |
| | | cal.set(Calendar.MONTH, month - 1); |
| | | // 获取某月最小天数 |
| | | int firstDay = cal.getMinimum(Calendar.DATE); |
| | | // 设置日历中月份的最小天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, firstDay); |
| | | int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年月的最后一天时间 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 时间 |
| | | */ |
| | | public static Date getYearMonthEnd(Integer year,Integer month) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 设置年份 |
| | | cal.set(Calendar.YEAR, year); |
| | | // 设置月份 |
| | | cal.set(Calendar.MONTH, month - 1); |
| | | // 获取某月最大天数 |
| | | int lastDay = cal.getMaximum(Calendar.DATE); |
| | | // 设置日历中月份的最大天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, lastDay); |
| | | int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | public static Date getDateM(Date start, int min) { |
| | | Calendar date1 = Calendar.getInstance(); |
| | | date1.setTime(start); |
| | | date1.add(Calendar.MONTH, min); |
| | | return date1.getTime(); |
| | | } |
| | | |
| | | public static Date parseDateYM(String str) { |
| | | try { |
| | | return yyyyMM_format.parse(str); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static int getMonth(Date date){ |
| | | Calendar instance = Calendar.getInstance(); |
| | | instance.setTime(date); |
| | | return instance.get(Calendar.MONTH); |
| | | } |
| | | |
| | | public static boolean before(Date start, Date end){ |
| | | Calendar date1 = Calendar.getInstance(); |
| | | date1.setTime(start); |
| | | Calendar date2 = Calendar.getInstance(); |
| | | date2.setTime(end); |
| | | return date1.before(date2); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public static void main(String[]args)throws Exception{ |
| | | isValidDate("2020/12/4"); |
| | | // Date date= new Date(); |
| | | // Date after = new Date(); |
| | | // System.out.println(calTimeDifference(date,after)); |
| | | System.out.println(getYearMonthStart(2021,2)); |
| | | |
| | | } |
| | | } |