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   日思日睿  阅读(437)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示