Calendar工具类


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

public class DateUtil {
public static final String YYYY_MM_DD_COLON_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String YYYYMM = "yyyyMM";
public static final String YYYYMMDD = "yyyyMMdd";
public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public DateUtil() {
}

public static Date getAfterDayDate(String dateStr, Integer day) {
Date date = getStringToDate(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(5, day);
return calendar.getTime();
}

public static Date getBeforeMinutesDate(Integer minute) {
Calendar calendar = Calendar.getInstance();
calendar.add(12, -minute);
return calendar.getTime();
}

public static Date getBeforeDaysDate(Integer day) {
Calendar calendar = Calendar.getInstance();
calendar.add(6, -day);
return calendar.getTime();
}

public static void main(String[] df) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Date date = getBeforeDaysDate(5);
System.out.println(format.format(date));
}

public static Date getStringToDate(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {
return sdf.parse(strDate);
} catch (ParseException var3) {
var3.printStackTrace();
return new Date();
}
}

public static Date getStringToDate2(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

try {
return sdf.parse(strDate);
} catch (ParseException var3) {
var3.printStackTrace();
return new Date();
}
}

public static Date getStringToDate3(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {
return sdf.parse(strDate);
} catch (ParseException var3) {
var3.printStackTrace();
return new Date();
}
}

public static String getDateToString(Date date) {
return sdf.format(date);
}

public static String getDateToStringShor(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}

public static String getDateToStringShor(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}

public static List<Date> getBetweenDates(Date start, Date end) {
List<Date> result = new ArrayList();
result.add(start);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
tempStart.add(6, 1);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);

while(tempStart.before(tempEnd)) {
result.add(tempStart.getTime());
tempStart.add(6, 1);
}

return result;
}

public static Date getAfterHourDate(Date date, Integer hour) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(11, hour);
return cal.getTime();
}

public static Date getTimesMorning() {
Calendar cal = new GregorianCalendar();
cal.set(11, 0);
cal.set(12, 0);
cal.set(13, 0);
cal.set(14, 0);
return cal.getTime();
}

public static Date getTimesNight() {
Calendar cal = new GregorianCalendar();
cal.set(11, 23);
cal.set(13, 59);
cal.set(12, 59);
cal.set(14, 0);
return cal.getTime();
}

public static Date getTimesMonthMorning() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(1), cal.get(2), cal.get(5), 0, 0, 0);
cal.set(5, cal.getActualMinimum(5));
return cal.getTime();
}

public static Date getTimesMonthNight() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(1), cal.get(2), cal.get(5), 0, 0, 0);
cal.set(5, cal.getActualMaximum(5));
cal.set(11, 23);
cal.set(13, 59);
cal.set(12, 59);
return cal.getTime();
}

public static String getTimesMonthMorning(String dateStr) {
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

try {
SimpleDateFormat df = new SimpleDateFormat("yyyyMM", Locale.US);
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(dateStr));
cal.set(cal.get(1), cal.get(2), cal.get(5), 0, 0, 0);
cal.set(5, cal.getActualMinimum(5));
return df1.format(cal.getTime());
} catch (ParseException var4) {
var4.printStackTrace();
return df1.format(new Date());
}
}

public static String getTimesMonthNight(String dateStr) {
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

try {
SimpleDateFormat df = new SimpleDateFormat("yyyyMM", Locale.US);
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(dateStr));
cal.set(cal.get(1), cal.get(2), cal.get(5), 0, 0, 0);
cal.set(5, cal.getActualMaximum(5));
cal.set(11, 23);
cal.set(13, 59);
cal.set(12, 59);
return df1.format(cal.getTime());
} catch (ParseException var4) {
var4.printStackTrace();
return df1.format(new Date());
}
}

public static String getReduction(String dataSring, int day) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

try {
Date dt = sdf.parse(dataSring);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(6, day);
return sdf.format(rightNow.getTime());
} catch (ParseException var5) {
var5.printStackTrace();
return sdf.format(new Date());
}
}

public static String getMonth(String dataStr, int monthDay) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMM", Locale.US);

try {
Date date = df.parse(dataStr);
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(2, monthDay);
date = cl.getTime();
return df.format(date);
} catch (ParseException var5) {
var5.printStackTrace();
return df.format(new Date());
}
}

public static String getCheckOutMonth(String dataStr) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMM", Locale.US);

try {
Date date = df.parse(dataStr);
Calendar cl = Calendar.getInstance();
cl.setTime(date);
if (Calendar.getInstance().get(5) > 27) {
cl.add(2, 2);
} else {
cl.add(2, 1);
}

date = cl.getTime();
return df.format(date);
} catch (ParseException var4) {
var4.printStackTrace();
return df.format(new Date());
}
}

public static String getWeekDate(String dateStr, int type) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(7) - 1;
int offset = false;
int offset;
if (type == 1) {
offset = 1 - dayOfWeek;
} else {
offset = 7 - dayOfWeek;
}

calendar.add(5, offset - 7);
Date timePre = calendar.getTime();
return simpleDateFormat.format(timePre);
}

public static Boolean isComparisonTime(String dateStr, String format) throws Exception {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date2 = sdf.parse(dateStr);
return date.getTime() < date2.getTime() ? false : true;
}
}

posted on 2019-12-27 21:38  日思日睿  阅读(437)  评论(0编辑  收藏  举报