当前位置 : 主页 > 编程语言 > java >

计算日期工具类

来源:互联网 收集:自由互联 发布时间:2022-07-04
package utils ; import org . apache . commons . lang . StringUtils ; import java . text . DateFormat ; import java . text . ParseException ; import java . text . SimpleDateFormat ; import java . util . Calendar ; import java . util . Date ;
package utils;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* 〈计算日期工具类〉
*
* @author Barrett
* @version 1.0.0
* @time 2019/12/16
*/
public class DateUtil {

private static final String DATE_FORMART = "yyyy-M-dd HH:mm:ss";

public static void main(String[] args) {
int second = 60;
int day = 5;
int addMonthLen = 2;
String dateFormart1 = "yyyy-M-dd";
String linkSymbol = "-";
}

/**
* @Author Barrett
* @Date 9:08 2020/1/11
* @Description 获取字符串类型的当前时间
*/
public static String getCurrentDateByYMDHMS() {
Date dd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String now = sdf.format(dd);
return now;
}

/**
* @Author Barrett
* @Date 9:08 2020/1/11
* @Description 获取当前时间的年月日
*/
public static String getCurrentDateByYMD() {
Date dd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(dd);
}

/**
* @Author Barrett
* @Date 9:16 2020/1/11
* @Description date转str
*/
public static String dateToString(Date date) {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMART);
return format.format(date);
}

/**
* @Author Barrett
* @Date 8:45 2020/1/11
* @Description str转date
*/
public static Date stringToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
* @Author Barrett
* @Date 19:03 2020/1/10
* @Description 按照指定格式将字符串时间转成date
*/
public static Date createDateByString(String dateString, String dateFormat) {
Date date = null;
DateFormat df = new SimpleDateFormat(dateFormat);
try {
date = df.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
* @Author Barrett
* @Date 18:26 2020/1/10
* @Description 获取当前时间前N秒的时间,格式:yyyy-MM-dd HH:mm:ss
*/
public static String getDateTimeBeforeSecond(int second) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = new GregorianCalendar();
Date date = new Date();
c.setTime(date); //设置参数时间
c.add(Calendar.SECOND, -second); //把日期往后增加SECOND 秒.整数往后推,负数往前移动
date = c.getTime(); //这个时间就是日期往后推一天的结果
return df.format(date);
}

/**
* @Author Barrett
* @Date 18:26 2020/1/10
* @Description 获取当前时间前N天的时间,格式:Fri Jan 10 18:31:17 CST 2020
*/
public static Date getDateBefore(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
}

/**
* Date add month
*/
/**
* @Author Barrett
* @Date 18:26 2020/1/10
* @Description 获取当前时间+N个月的时间,格式:2020-02-10 00:00:00
*/
public static String getDateAddMonth(Date dd, int addMonthLen) {
Calendar Cal = Calendar.getInstance();
Cal.setTime(dd);
Cal.add(Calendar.MONTH, addMonthLen);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return sdf.format(Cal.getTime());
}

/**
* @Author Barrett
* @Date 18:35 2020/1/10
* @Description 格式化当前时间为00时,格式:2020-01-10 00:00:00
*/
public static String getCurrentDateByHMSZeroZeroZero(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return sdf.format(date);
}

/**
* @Author Barrett
* @Date 16:20 2020/1/4
* @Description 按照指定格式格式化当前时间
* 例如 dateFormart = yyyy-MM-dd 00:59:99
* 返回:2020-01-04 00:59:99
*/
public static String getYMDHMbeforeMinute(Date date, int sMinute, String dateFormart) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) - sMinute);
Date dd = now.getTime();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormart);
return sdf.format(dd);
}

/**
* @Author Barrett
* @Date 16:23 2020/1/4
* @Description 计算当前时间为本周的第几天:从周日开始算到周六
*/
public static int getWeekNum(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK);
}

/**
* @Author Barrett
* @Date 18:44 2020/1/10
* @Description 按照指定格式计算两个时间相差几天
* smdate:2020-01-04
* bdate:2020-01-10
* dateFormat:yyyy-MM-dd
* 返回6
*/
public static int countDaysBetween(String smdate, String bdate, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Calendar cal = Calendar.getInstance();
long between_days = 0;
try {
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
between_days = (time2 - time1) / (1000 * 3600 * 24);
} catch (ParseException e) {
e.printStackTrace();
}
return Integer.parseInt(String.valueOf(between_days));
}

/**
* @Author Barrett
* @Date 18:46 2020/1/10
* @Description 按指定格式获取当前时间的月末时间
* dateFormat="yyyy-MM-dd HH:mm:ss"
* 2020-01-31 18:48:46
*/
public static String getSomeMonthLastDay(Date date, String dateFormat) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, value);
return new SimpleDateFormat(dateFormat).format(cal.getTime());
}

/**
* @Author Barrett
* @Date 18:46 2020/1/10
* @Description 设置当前时间的月和日
* linkSymbol="-"
* monthAmount=7
* newDay=8
* 返回:2020-8-8
*/
public static String getDynamicMonthYMD(Date date, String linkSymbol, int monthAmount, int newDay) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, newDay);
calendar.add(Calendar.MONTH, monthAmount);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
return String.valueOf(year + linkSymbol + month + linkSymbol + day);
}

/**
* @Author Barrett
* @Date 18:46 2020/1/10
* @Description 设置当前时间的月和日
* linkSymbol="-"
* newDay=8
* 返回:2020-1-8
*/
public static String getYMD(Date date, String linkSymbol, int newDay) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, newDay);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
return String.valueOf(year + linkSymbol + month + linkSymbol + day);
}

/**
* @Author Barrett
* @Date 18:46 2020/1/10
* @Description 将当前时间按照linkSymbol进行连接
* linkSymbol="-"
* 2020-01-10
*/
public static String getYMD(Date date, String linkSymbol) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
return String.valueOf(year + linkSymbol + (month < 10 ? "0" + month : month) + linkSymbol + (day < 10 ? "0" + day : day));
}

/**
* @Author Barrett
* @Date 8:45 2020/1/11
* @Description 当前时间的前num天
*/
public static Date subDay(Date date, int num) {
Calendar time = Calendar.getInstance();
num = -num;
time.setTime(date);
time.add(Calendar.DAY_OF_MONTH, num);
return time.getTime();
}

/**
* @Author Barrett
* @Date 8:45 2020/1/11
* @Description 获取当前时间+minutes分钟后的时间
*/
public static Date getDateAfterMinute(Date date, int minutes) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minutes);
return now.getTime();
}

/**
* @Author Barrett
* @Date 8:53 2020/1/11
* @Description 判断当前字符串在当前格式以内
*/
public static boolean isDateType(String dateFormat, String dateString) {
boolean isDateTypeFlag = true;
DateFormat df = new SimpleDateFormat(dateFormat);
try {
df.parse(dateString);
} catch (ParseException e) {
isDateTypeFlag = false;
}
return isDateTypeFlag;
}

/**
* @Author Barrett
* @Date 8:53 2020/1/11
* @Description 比较oldDate和newDate
* oldDate < newDate == -1
*/
public static int compareDates(Date oldDate, Date newDate) {
Calendar oldCal = Calendar.getInstance();
Calendar newCal = Calendar.getInstance();
oldCal.setTime(oldDate);
newCal.setTime(newDate);
if (oldCal.equals(newCal)) {
return 0;
} else if (oldCal.before(newCal)) {
return -1;
} else {
return 1;
}
}

/**
* @Author Barrett
* @Date 8:53 2020/1/11
* @Description 按照指定时间返回date类型时间
*/
public static Date getDateByYMDHMS(int year, int month, int day, int hour, int minute, int second) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
return calendar.getTime();
}

/**
* @Author Barrett
* @Date 9:08 2020/1/11
* @Description num天后的时间
*/
public static Date addDay(Date date, int num) {
Calendar time = Calendar.getInstance();
time.setTime(date);
time.add(Calendar.DAY_OF_MONTH, num);
return time.getTime();
}

/**
* @Author Barrett
* @Date 9:08 2020/1/11
* @Description 按照指定格式返回当前时间
*/
public static String getDateString(String format) {
String dateStr;
if (StringUtils.isBlank(format)) {
format = "yyyy-MM-dd-HH:mm:ss";
}
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat(format);
dateStr = formatter.format(calendar.getTime());
return dateStr;
}

/**
* @Author Barrett
* @Date 9:08 2020/1/11
* @Description 传入时分秒返回当前时间
*/
public static Date getDateByHMS(int hour, int minute, int secord) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, secord);
return calendar.getTime();
}

/**
* @Author Barrett
* @Date 9:08 2020/1/11
* @Description 返回当天的凌晨时间
*/
public static String getCurrentDateByHMSZeroZeroZero() {
Date dd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return sdf.format(dd);
}



/**
* @Author Barrett
* @Date 10:53 2020/1/10
* @Description 将字符串格式日期转成指定格式字符串返回
*/
public static String parseToFormat(String payTime) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
payTime = format.format(format.parse(payTime));
} catch (ParseException e) {
e.printStackTrace();
}
return payTime;
}
}


上一篇:Java后台专业术语
下一篇:没有了
网友评论