package com.xxxx.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class SuperDateUtils {
public static final SimpleDateFormat year = new SimpleDateFormat("yyyy");
public static final SimpleDateFormat year_mouth = new SimpleDateFormat("yyyy-MM");
public static final SimpleDateFormat year_mouth_day = new SimpleDateFormat("yyyy-MM-dd");
public static final SimpleDateFormat year_mouth_day_hms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat year_mouth_withoutseparater = new SimpleDateFormat("yyyyMM");
public static final SimpleDateFormat year_mouth_day_withoutseparater = new SimpleDateFormat("yyyyMMdd");
public static final SimpleDateFormat year_mouth_day_hms_withoutseparater = new SimpleDateFormat("yyyyMMddHHmmss");
public static final SimpleDateFormat china_year = new SimpleDateFormat("yyyy年");
public static final SimpleDateFormat china_year_mouth = new SimpleDateFormat("yyyy年MM月");
public static final SimpleDateFormat china_year_mouth_day = new SimpleDateFormat("yyyy年MM月dd日");
public static final SimpleDateFormat china_year_mouth_day_hms = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
public static final SimpleDateFormat china_mouth_day = new SimpleDateFormat("MM月dd日");
public static String getYear(Date date) {
if (date != null) {
return year.format(date);
}
return null;
}
public static String getYM(Date date) {
if (date != null) {
return year_mouth.format(date);
}
return null;
}
public static String getYMD(Date date) {
if (date != null) {
return year_mouth_day.format(date);
}
return null;
}
public static String getYMDHMS(Date date) {
if (date != null) {
return year_mouth_day_hms.format(date);
}
return null;
}
public static String getYMDHMSWithoutSeparater(Date date) {
if (date != null) {
return year_mouth_day_hms_withoutseparater.format(date);
}
return null;
}
public static Date getDayBeginTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
date = cal.getTime();
return date;
}
public static Date getDayEndTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
date = cal.getTime();
return date;
}
public static Date getMonthBeginTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
date = cal.getTime();
return date;
}
public static Date getMonthEndTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
date = cal.getTime();
return date;
}
public static Date getQuarterBeginTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int quarter = getQuarter(date);
switch (quarter) {
case 1:
setTime(cal, Calendar.JANUARY, 1, 0, 0, 0);
break;
case 2:
setTime(cal, Calendar.APRIL, 1, 0, 0, 0);
break;
case 3:
setTime(cal, Calendar.JULY, 1, 0, 0, 0);
break;
case 4:
setTime(cal, Calendar.OCTOBER, 1, 0, 0, 0);
break;
default:
break;
}
return cal.getTime();
}
public static Date getQuarterEndTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int quarter = getQuarter(date);
switch (quarter) {
case 1:
setTime(cal, Calendar.MARCH, 31, 23, 59, 59);
break;
case 2:
setTime(cal, Calendar.JUNE, 30, 23, 59, 59);
break;
case 3:
setTime(cal, Calendar.SEPTEMBER, 30, 23, 59, 59);
break;
case 4:
setTime(cal, Calendar.DECEMBER, 31, 23, 59, 59);
break;
default:
break;
}
return cal.getTime();
}
public static Date getYearBeginTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
setTime(cal, Calendar.JANUARY, 1, 0, 0, 0);
return cal.getTime();
}
public static Date getYearEndTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
setTime(cal, Calendar.DECEMBER, 31, 23, 59, 59);
return cal.getTime();
}
private static void setTime(Calendar cal, int mouth, int day, int hour, int minute, int second) {
cal.set(Calendar.MONTH, mouth);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
}
public static int getQuarter(Date date) {
int quarter = 0;
Calendar c = Calendar.getInstance();
c.setTime(date);
int month = c.get(Calendar.MONTH);
switch (month) {
case Calendar.JANUARY:
case Calendar.FEBRUARY:
case Calendar.MARCH:
quarter = 1;
break;
case Calendar.APRIL:
case Calendar.MAY:
case Calendar.JUNE:
quarter = 2;
break;
case Calendar.JULY:
case Calendar.AUGUST:
case Calendar.SEPTEMBER:
quarter = 3;
break;
case Calendar.OCTOBER:
case Calendar.NOVEMBER:
case Calendar.DECEMBER:
quarter = 4;
break;
default:
break;
}
return quarter;
}
public static String getYearName(Date date) {
return china_year.format(date);
}
public static List<String> getYearNameList(Date form, Date to) {
List<String> yearNameList = new ArrayList<>();
Calendar formCal = Calendar.getInstance();
formCal.setTime(form);
setTime(formCal, Calendar.JANUARY, 1, 0, 0, 0);
while (!formCal.getTime().after(to)) {
String yearName = getYearName(formCal.getTime());
if (!yearNameList.contains(yearName)) {
yearNameList.add(yearName);
}
formCal.add(Calendar.YEAR, 1);
}
return yearNameList;
}
public static String getQuarterName(Date date) {
String year = getYear(date);
int quarter = getQuarter(date);
StringBuffer sb = new StringBuffer();
sb.append(year).append("年").append(quarter).append("季度");
return sb.toString();
}
public static List<String> getQuarterNameList(Date form, Date to) {
List<String> quarterNameList = new ArrayList<>();
Calendar formCal = Calendar.getInstance();
formCal.setTime(form);
setTime(formCal, formCal.get(Calendar.MONTH), 1, 0, 0, 0);
while (!formCal.getTime().after(to)) {
String quarterName = getQuarterName(formCal.getTime());
if (!quarterNameList.contains(quarterName)) {
quarterNameList.add(quarterName);
}
formCal.add(Calendar.MONTH, 1);
}
return quarterNameList;
}
public static String getMouthName(Date date) {
return china_year_mouth.format(date);
}
public static List<String> getMouthNameList(Date form, Date to) {
List<String> mouthNameList = new ArrayList<>();
Calendar formCal = Calendar.getInstance();
formCal.setTime(form);
setTime(formCal, formCal.get(Calendar.MONTH), 1, 0, 0, 0);
while (!formCal.getTime().after(to)) {
String mouthName = getMouthName(formCal.getTime());
mouthNameList.add(mouthName);
formCal.add(Calendar.MONTH, 1);
}
return mouthNameList;
}
public static String getDayName(Date date) {
return china_year_mouth_day.format(date);
}
public static List<String> getDayNameList(Date form, Date to) {
List<String> dayNameList = new ArrayList<>();
Calendar formCal = Calendar.getInstance();
formCal.setTime(form);
setTime(formCal, formCal.get(Calendar.MONTH), formCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
while (!formCal.getTime().after(to)) {
String dayName = getDayName(formCal.getTime());
dayNameList.add(dayName);
formCal.add(Calendar.DAY_OF_MONTH, 1);
}
return dayNameList;
}
public static int getDays(Date from, Date to) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(from);
int cnt = 0;
while (calendar.getTime().compareTo(to) != 0) {
calendar.add(Calendar.DATE, 1);
cnt++;
}
return cnt;
}
public static String addDays(Date date, Integer addDay, boolean chinesename) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, addDay);
if (chinesename) {
return china_year_mouth_day.format(calendar.getTime());
}
return year_mouth_day.format(calendar.getTime());
}
}