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;
/**
* @author :lifh
* @date : 2022/11/13
* 本人做项目时遇到的处理时间的方法
*/
public class MyDateUtil {
/**
* the milli second of a day
*/
public static final long DAYMILLI = 24 * 60 * 60 * 1000;
/**
* the milli seconds of an hour
*/
public static final long HOURMILLI = 60 * 60 * 1000;
/**
* the milli seconds of a minute
*/
public static final long MINUTEMILLI = 60 * 1000;
/**
* the milli seconds of a second
*/
public static final long SECONDMILLI = 1000;
/**
* added time
*/
public static final String TIMETO = " 23:59:59";
/**
* flag before
*/
public static final transient int BEFORE = 1;
/**
* flag after
*/
public static final transient int AFTER = 2;
/**
* flag equal
*/
public static final transient int EQUAL = 3;
/**
* date format dd/MMM/yyyy:HH:mm:ss +0900
*/
public static final String TIME_PATTERN_LONG = "dd/MMM/yyyy:HH:mm:ss +0900";
/**
* date format dd/MM/yyyy:HH:mm:ss +0900
*/
public static final String TIME_PATTERN_LONG2 = "dd/MM/yyyy:HH:mm:ss +0900";
/**
* date format YYYY-MM-DD HH24:MI:SS
*/
public static final String DB_TIME_PATTERN = "YYYY-MM-DD HH24:MI:SS";
/**
* date format YYYYMMDDHH24MISS
*/
public static final String DB_TIME_PATTERN_1 = "YYYYMMDDHH24MISS";
/**
* date format dd/MM/yy HH:mm:ss
*/
public static final String TIME_PATTERN_SHORT = "dd/MM/yy HH:mm:ss";
/**
* date format dd/MM/yy HH24:mm
*/
public static final String TIME_PATTERN_SHORT_1 = "yyyy/MM/dd HH:mm";
/**
* date format yyyy年MM月dd日 HH:mm:ss
*/
public static final String TIME_PATTERN_SHORT_2 = "yyyy年MM月dd日 HH:mm:ss";
/**
* date format yyyyMMddHHmmss
*/
public static final String TIME_PATTERN_SESSION = "yyyyMMddHHmmss";
/**
* date format yyyyMMddHHmmssSSS
*/
public static final String TIME_PATTERN_MILLISECOND = "yyyyMMddHHmmssSSS";
/**
* date format yyyyMMdd
*/
public static final String DATE_FMT_0 = "yyyyMMdd";
/**
* date format yyyy/MM/dd
*/
public static final String DATE_FMT_1 = "yyyy/MM/dd";
/**
* date format yyyy/MM/dd hh:mm:ss
*/
public static final String DATE_FMT_2 = "yyyy/MM/dd hh:mm:ss";
/**
* date format yyyy-MM-dd
*/
public static final String DATE_FMT_3 = "yyyy-MM-dd";
/**
* date format yyyy年MM月dd日
*/
public static final String DATE_FMT_4 = "yyyy年MM月dd日";
/**
* date format yyyy-MM-dd HH
*/
public static final String DATE_FMT_5 = "yyyy-MM-dd HH";
/**
* date format yyyy-MM
*/
public static final String DATE_FMT_6 = "yyyy-MM";
/**
* date format MM月dd日 HH:mm
*/
public static final String DATE_FMT_7 = "MM月dd日 HH:mm";
/**
* date format MM月dd日 HH:mm
*/
public static final String DATE_FMT_8 = "HH:mm:ss";
/**
* date format MM月dd日 HH:mm
*/
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";
/**
* date format yyyy年MM月dd日
*/
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";
/**
* date format yyyy-MM-dd HH:mm:ss
*/
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";
/**
* 文档注释 (方法或类注释):一般用于对类和方法的说明
* 快捷键:光标------/**+回车键(EnTer
* @param date
*/
/**
* Calendar常用API
* @param date
*/
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; //获取月(月份从0开始,如果按照中国的习惯,需要加一)
Integer day_moneth=instance.get(Calendar.DAY_OF_MONTH);//获取日(月中的某一天)
Integer day_week=instance.get(Calendar.DAY_OF_WEEK);//获取一周内的某一天
}
/**
* 日期转string
* @param date
* @param pattern
* @return
*/
public static String dateToString(Date date, String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(date);
}
/**
* string转日期
* @param str
* @param pattern
* @return
*/
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;
}
/**
* 获取两个日期之间的所有日期
* @param startTime 开始日期
* @param endTime 结束日期
* @return
*/
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);// 日期加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;
}
/**
* 根据时间获取当月多少天
* @param date
* @return
*/
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; //获取月(月份从0开始,如果按照中国的习惯,需要加一)
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;
}
/**
* 判断两个时间的间隔的天数(通过秒毫秒数)
* @param date1
* @param date2
* @return
*/
public static int differentDaysByMillisecond(Date date1,Date date2)
{
int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
return days;
}
/**
* 时间比较: before after compare
* @param time1
* @param time2
* @return
* @throws ParseException
*/
public boolean compareDateTime(Date time1, Date time2) throws ParseException {
//日期比较,time1 小于 time2
return time1.before(time2);
//日期比较,time1 大于 time2
// return time1.after(time2);
//日期比较,time1 等于 time2
// return time1.equals(time2);
}
/**
* 日期比较
* @param time1
* @param time2
* @return
* @throws ParseException
*/
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);
}
/**
* 获取指定时间后若干小时的时间
* @param date
* @return java.util.Date
*/
public static Date afterOneHourToNowDate(Date date, int hours) {
Calendar calendar = Calendar.getInstance();
/* HOUR_OF_DAY 指示一天中的小时 */
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hours);
return calendar.getTime();
}
/**
*获取指定时间n天前/n天后的日期
* @param num ﹣3 三天前 +3 三天后
* @return
*/
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());
}
/**
* @Description 获取年份
* @param pattern 格式
* @param num 负数为之前年,0为当前年,正数为之后年
* @Throws
* @Return java.lang.String
* @Date 2021-09-06 10:28:15
* @Author WangKun
**/
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);
}
/**
* 获取上个月对应的今天的日期,如:2022-10-11 的上个月的今天是 2022-09-11
* @param startDate
* @return
*/
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;
}
}
标签:java,String,MM,final,时间,date,static,工具,public
From: https://www.cnblogs.com/lfh-blog/p/16887614.html