| | |
| | | |
| | | 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 SimpleDateFormat ymdhm_format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| | | |
| | | public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd"); |
| | | |
| | | public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM"); |
| | | |
| | | |
| | | |
| | |
| | | return now; |
| | | } |
| | | |
| | | /** |
| | | * 返回当前事件的时间戳 |
| | | * @return |
| | | */ |
| | | public static Long getCurrentDateTimeStamp(){ |
| | | return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); |
| | | } |
| | | /** |
| | | * 获取当前时间字符串,格式为yyyy-MM-dd HH:mm:ss |
| | | * @return |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * date2比date1多的天数 |
| | | * @param date1 |
| | | * @param date2 |
| | | * @return |
| | | */ |
| | | public static int differentDays(Date date1,Date date2) |
| | | { |
| | | Calendar cal1 = Calendar.getInstance(); |
| | | cal1.setTime(date1); |
| | | |
| | | Calendar cal2 = Calendar.getInstance(); |
| | | cal2.setTime(date2); |
| | | int day1= cal1.get(Calendar.DAY_OF_YEAR); |
| | | int day2 = cal2.get(Calendar.DAY_OF_YEAR); |
| | | |
| | | int year1 = cal1.get(Calendar.YEAR); |
| | | int year2 = cal2.get(Calendar.YEAR); |
| | | if(year1 != year2) //同一年 |
| | | { |
| | | int timeDistance = 0 ; |
| | | for(int i = year1 ; i < year2 ; i ++) |
| | | { |
| | | if(i%4==0 && i%100!=0 || i%400==0) //闰年 |
| | | { |
| | | timeDistance += 366; |
| | | } |
| | | else //不是闰年 |
| | | { |
| | | timeDistance += 365; |
| | | } |
| | | } |
| | | |
| | | return timeDistance + (day2-day1) ; |
| | | } |
| | | else //不同年 |
| | | { |
| | | // System.out.println("判断day2 - day1 : " + (day2-day1)); |
| | | return day2-day1; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月第一天 |
| | | * @return |
| | | */ |
| | | public static String getFirstDayOfMonthString() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | |
| | | calendar.add(Calendar.MONTH, 0); |
| | | |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(calendar.getTime())+" 00:00:00"; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月最后一天 |
| | | * @return |
| | | */ |
| | | public static String getLastDayOfMonthString() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | |
| | | calendar.add(Calendar.MONTH, 1); |
| | | |
| | | calendar.set(Calendar.DAY_OF_MONTH, 0); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(calendar.getTime())+" 23:59:59"; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间(年月日) |
| | | * @return |
| | | */ |
| | | public static String getDayOfMonthString() { |
| | | Calendar today = Calendar.getInstance(); |
| | | today.set(Calendar.HOUR, 0); |
| | | today.set(Calendar.MINUTE, 0); |
| | | today.set(Calendar.SECOND, 0); |
| | | today.set(Calendar.MILLISECOND, 0); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(today.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 日期转周 |
| | | * |
| | | * @param datetime |
| | | * @return |
| | | */ |
| | | public static String dateToWeek(String datetime) { |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; |
| | | Calendar cal = Calendar.getInstance(); // 获得一个日历 |
| | | Date datet = null; |
| | | try { |
| | | if (StringUtils.isNotEmpty(datetime)) { |
| | | datet = f.parse(datetime); |
| | | cal.setTime(datet); |
| | | } |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个周中的某天。 |
| | | if (w < 0) |
| | | w = 0; |
| | | return weekDays[w]; |
| | | } |
| | | |
| | | |
| | | |
| | | public static void main(String[]args)throws Exception{ |
| | | // Date date= new Date(); |
| | | // Date after = new Date(); |
| | | // System.out.println(calTimeDifference(date,after)); |
| | | // System.out.println(getFirstDayOfMonthString()); |
| | | // System.out.println(getDayOfMonthString()); |
| | | |
| | | } |
| | | } |