package com.zy.kingdee.utils; import com.alibaba.excel.util.StringUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class ERPDateUtil { public static int getDiffBetweenMonths(Date date1, Date date2) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(date1); c2.setTime(date2); int result = c2.get(2) - c1.get(2); int month = (c2.get(1) - c1.get(1)) * 12; return Math.abs(result + month); } public static Date getNextMonthDate(Date date, int afterMonth) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(2, calendar.get(2) + afterMonth); Date next = calendar.getTime(); return next; } public static Date getFirstDate(String string) throws ParseException { if (StringUtils.isEmpty(string)) { return null; } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = simpleDateFormat.parse(string + "-01 00:00:00"); return date; } public static String getNextDateStr(Date date, int afterDay) { Date nextDate = getNextDate(date, afterDay); String dateStr = getDateStr(nextDate, "yyyy-MM-dd"); return dateStr; } public static Date getNextDate(Date date, int afterDay) { Calendar calendar = new GregorianCalendar(); calendar.setTime(date); Integer day = Integer.valueOf(calendar.get(5)); calendar.set(5, day.intValue() + afterDay); Date newDate = calendar.getTime(); return newDate; } public static String getDateStr(Date date, String format) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); String s = simpleDateFormat.format(date); return s; } public static Date parseStrToDate(String string, String format) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); Date parse = simpleDateFormat.parse(string); return parse; } public static int getMonth(String string) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date parse = simpleDateFormat.parse(string); int month = parse.getMonth() + 1; return month; } public static void main(String[] args) throws ParseException { Date a = getFirstDate("2018-8"); System.out.print(a); } }