package com.example.httpdemo2.utils;
import java.text.DateFormat;
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 MyDateUtil {
public static final long DAYMILLI = 24 * 60 * 60 * 1000;
public static final long HOURMILLI = 60 * 60 * 1000;
public static final long MINUTEMILLI = 60 * 1000;
public static final long SECONDMILLI = 1000;
public static final String TIMETO = " 23:59:59";
public static final transient int BEFORE = 1;
public static final transient int AFTER = 2;
public static final transient int EQUAL = 3;
public static final String TIME_PATTERN_LONG = "dd/MMM/yyyy:HH:mm:ss +0900";
public static final String TIME_PATTERN_LONG2 = "dd/MM/yyyy:HH:mm:ss +0900";
public static final String DB_TIME_PATTERN = "YYYY-MM-DD HH24:MI:SS";
public static final String DB_TIME_PATTERN_1 = "YYYYMMDDHH24MISS";
public static final String TIME_PATTERN_SHORT = "dd/MM/yy HH:mm:ss";
public static final String TIME_PATTERN_SHORT_1 = "yyyy/MM/dd HH:mm";
public static final String TIME_PATTERN_SHORT_2 = "yyyy年MM月dd日 HH:mm:ss";
public static final String TIME_PATTERN_SESSION = "yyyyMMddHHmmss";
public static final String TIME_PATTERN_MILLISECOND = "yyyyMMddHHmmssSSS";
public static final String DATE_FMT_0 = "yyyyMMdd";
public static final String DATE_FMT_1 = "yyyy/MM/dd";
public static final String DATE_FMT_2 = "yyyy/MM/dd hh:mm:ss";
public static final String DATE_FMT_3 = "yyyy-MM-dd";
public static final String DATE_FMT_4 = "yyyy年MM月dd日";
public static final String DATE_FMT_5 = "yyyy-MM-dd HH";
public static final String DATE_FMT_6 = "yyyy-MM";
public static final String DATE_FMT_7 = "MM月dd日 HH:mm";
public static final String DATE_FMT_8 = "HH:mm:ss";
public static final String DATE_FMT_9 = "yyyy.MM.dd";
public static final String DATE_FMT_10 = "HH:mm";
public static final String DATE_FMT_11 = "yyyy.MM.dd HH:mm:ss";
public static final String DATE_FMT_12 = "MM月dd日";
public static final String DATE_FMT_13 = "yyyy年MM月dd日HH时mm分";
public static final String DATE_FMT_14 = "yyyyMM";
public static final String DATE_FMT_15 = "MM-dd HH:mm:ss";
public static final String DATE_FMT_16 = "yyyyMMddHHmm";
public static final String DATE_FMT_17 = "HHmmss";
public static final String DATE_FMT_18 = "yyyy";
public static final String TIME_PATTERN1 = "yyyy-MM-dd HH:mm:ss";
public static final String TIME_PATTERN2 = "yyyy-MM-dd";
public static final String TIME_PATTERN3 = "yyyyMMdd";
public static void calendarMeths(Date date){
Calendar instance = Calendar.getInstance();
instance.setTime(date);
Integer year = instance.get(Calendar.YEAR);
Integer month = instance.get(Calendar.MONTH)+1;
Integer day_moneth=instance.get(Calendar.DAY_OF_MONTH);
Integer day_week=instance.get(Calendar.DAY_OF_WEEK);
}
public static String dateToString(Date date, String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(date);
}
public static Date getStrToDate(String str, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = null;
try {
date = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public static List<String> getDays(String startTime, String endTime) {
List<String> days = new ArrayList<String>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
public static int getDaysInMonth(Date date) {
Calendar instance = Calendar.getInstance();
instance.setTime(date);
int year = instance.get(Calendar.YEAR);
int month = instance.get(Calendar.MONTH)+1;
int days = 0;
if (month != 2) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
}
} else {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
days = 29;
else
days = 28;
}
System.out.println("当月有" + days + "天!");
return days;
}
public static int differentDaysByMillisecond(Date date1,Date date2)
{
int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
return days;
}
public boolean compareDateTime(Date time1, Date time2) throws ParseException {
return time1.before(time2);
}
public boolean compareDate(Date time1, Date time2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strDate1 = sdf.format(time1);
String strDate2 = sdf.format(time2);
Date date1 = sdf.parse(strDate1);
Date date2 = sdf.parse(strDate2);
return date1.before(date2);
}
public static Date afterOneHourToNowDate(Date date, int hours) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hours);
return calendar.getTime();
}
public static String getDayAgoOrAfterString(Date date,int num){
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date);
calendar1.add(Calendar.DATE, num);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
return sdf1.format(calendar1.getTime());
}
public static String getYears(String pattern,int num) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, num);
Date y = c.getTime();
return format.format(y);
}
public String getLastStartDates(String startDate) {
String lastDate = null;
int startDay = Integer.parseInt(startDate.substring(8));
Date strToDate = getStrToDate(startDate, "yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(strToDate);
c.add(Calendar.MONTH, -1);
int lastMonthDays = getDaysInMonth(c.getTime());
String lastMonth = dateToString(c.getTime(), "yyyy-MM");
if (startDay > lastMonthDays) {
lastDate = lastMonth + "-" + lastMonthDays;
} else {
lastDate = lastMonth + "-" + startDate.substring(8);
}
return lastDate;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性