| | |
| | | |
| | | public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd"); |
| | | |
| | | public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM"); |
| | | |
| | | |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将日期对象格式化成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)); |
| | | |
| | | } |
| | | } |