public class UtilDate {
public static final String dtLong = "yyyyMMddHHmmss";
public static final String simple = "yyyy-MM-dd HH:mm:ss";
public static final String simpleX = "yyyy/MM/dd HH:mm:ss";
public static final String dtShort = "yyyyMMdd";
public static final String yearMonth = "yyyyMM";
public static final String simplebr = "yyyy-MM-dd";
public static final String dtShortSlash = "yyyy/MM/dd";
public static String getOrderNum() {
Date date = new Date();
DateFormat df = new SimpleDateFormat(dtLong);
return df.format(date);
}
public static String getOrderNum(Date date) {
if (StringUtils.isBlank(String.valueOf(date))) {
return null;
}
DateFormat df = new SimpleDateFormat(dtLong);
return df.format(date);
}
public static String getDateFormatter() {
Date date = new Date();
DateFormat df = new SimpleDateFormat(simple);
return df.format(date);
}
public static String getDateFormatter(Date date) {
if (StringUtils.isBlank(String.valueOf(date))) {
return null;
}
DateFormat df = new SimpleDateFormat(simple);
return df.format(date);
}
public static String getDate() {
Date date = new Date();
DateFormat df = new SimpleDateFormat(dtShort);
return df.format(date);
}
public static String getDate(Date date) {
if (StringUtils.isBlank(String.valueOf(date))) {
return null;
}
DateFormat df = new SimpleDateFormat(dtShort);
return df.format(date);
}
public static String getDatebr() {
Date date = new Date();
DateFormat df = new SimpleDateFormat(simplebr);
return df.format(date);
}
public static String getDatebr(Date date) {
if (StringUtils.isBlank(String.valueOf(date))) {
return null;
}
DateFormat df = new SimpleDateFormat(simplebr);
return df.format(date);
}
public static String dtShortSlash() {
Date date = new Date();
DateFormat df = new SimpleDateFormat(dtShort);
return df.format(date);
}
public static String dtShortSlash(Date date) {
if (StringUtils.isBlank(String.valueOf(date))) {
return null;
}
DateFormat df = new SimpleDateFormat(dtShort);
return df.format(date);
}
public static String getFormatDate(String formatname, Date date) {
if (StringUtils.isBlank(String.valueOf(date))) {
return null;
}
DateFormat df = new SimpleDateFormat(formatname);
return df.format(date);
}
public static Date getDateForString(String formatname, String date) throws Exception {
if (StringUtils.isBlank(date)) {
return null;
}
DateFormat df = new SimpleDateFormat(formatname);
return df.parse(date);
}
public static Date getDateForSimple(String date) throws Exception {
if (StringUtils.isBlank(date)) {
return null;
}
DateFormat df = new SimpleDateFormat(simple);
return df.parse(date);
}
public static Date getDateForSimpleX(String date) throws Exception {
if (StringUtils.isBlank(date)) {
return null;
}
DateFormat df = new SimpleDateFormat(simpleX);
return df.parse(date);
}
public static Date getDateForDtLong(String date) throws Exception {
if (StringUtils.isBlank(date)) {
return null;
}
DateFormat df = new SimpleDateFormat(dtLong);
return df.parse(date);
}
public static Date getDateForDtShort(String date) throws Exception {
if (StringUtils.isBlank(date)) {
return null;
}
DateFormat df = new SimpleDateFormat(dtShort);
return df.parse(date);
}
public static Date getDateForSimplebr(String date) throws Exception {
if (StringUtils.isBlank(date)) {
return null;
}
DateFormat df = new SimpleDateFormat(simplebr);
return df.parse(date);
}
public static Date getDateForDtShortSlash(String date) throws Exception {
if (StringUtils.isBlank(date)) {
return null;
}
DateFormat df = new SimpleDateFormat(dtShortSlash);
return df.parse(date);
}
public static String getYearMonth(String time, String originalFormat) {
SimpleDateFormat original = new SimpleDateFormat(originalFormat);
SimpleDateFormat dateFormat = new SimpleDateFormat(yearMonth);
Date date = new Date();
if (StringUtils.isNotBlank(time)) {
try {
date = original.parse(time);
} catch (ParseException e) {
}
}
return dateFormat.format(date);
}
public Date getFormatDate(String formatname) throws Exception {
if (StringUtils.isBlank(formatname)) {
return null;
}
DateFormat df = new SimpleDateFormat(formatname);
return df.parse(df.format(new Date()));
}
public static String getThree() {
Random rad = new Random();
return rad.nextInt(1000) + "";
}
public static String getDatePoor(Date startDate, Date endDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long ns = 1000;
long diff = endDate.getTime() - startDate.getTime();
long day = diff / nd;
long hour = diff % nd / nh;
long min = diff % nd % nh / nm;
long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
}
public static Long getMonths(Date startDate, Date endDate) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(startDate);
c2.setTime(endDate);
if (c1.getTimeInMillis() < c2.getTimeInMillis()) {
return 0L;
}
int year1 = c1.get(Calendar.YEAR);
int year2 = c2.get(Calendar.YEAR);
int month1 = c1.get(Calendar.MONTH);
int month2 = c2.get(Calendar.MONTH);
int day1 = c1.get(Calendar.DAY_OF_MONTH);
int day2 = c2.get(Calendar.DAY_OF_MONTH);
int yearInterval = year1 - year2;
if (month1 < month2 || month1 == month2 && day1 < day2) {
yearInterval--;
}
int monthInterval = (month1 + 12) - month2;
if (day1 < day2) {
monthInterval--;
}
monthInterval %= 12;
return (long) (yearInterval * 12 + monthInterval);
}
public static Long getDay(Date startDate, Date endDate) {
DateFormat dateFormat = new SimpleDateFormat(simple);
Long days = null;
long diff = endDate.getTime() - startDate.getTime();
days = diff / (1000 * 60 * 60 * 24);
return days;
}
public static Long geHour(Date startDate, Date endDate) {
DateFormat dateFormat = new SimpleDateFormat(simple);
Long hour = null;
long diff = endDate.getTime() - startDate.getTime();
hour = diff / (1000 * 60 * 60);
return hour;
}
public static Long geMin(Date startDate, Date endDate) {
DateFormat dateFormat = new SimpleDateFormat(simple);
Long min = null;
long diff = endDate.getTime() - startDate.getTime();
min = diff / (1000 * 60);
return min;
}
public static Long geSec(Date startDate, Date endDate) {
DateFormat dateFormat = new SimpleDateFormat(simple);
Long seconds = null;
long diff = endDate.getTime() - startDate.getTime();
seconds = diff / (1000);
return seconds;
}
public static Date yearAddNum(Date time, Integer num) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.YEAR, num);
Date newTime = calendar.getTime();
return newTime;
}
public static Date monthAddNum(Date time, Integer num) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.MONTH, num);
Date newTime = calendar.getTime();
return newTime;
}
public static Date weekAddNum(Date time, Integer num) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.WEEK_OF_MONTH, num);
Date newTime = calendar.getTime();
return newTime;
}
public static Date dayAddNum(Date time, Integer num) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.DAY_OF_MONTH, num);
Date newTime = calendar.getTime();
return newTime;
}
public static Date getMonthStartDate() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
public static Date getMonthEndDate() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
public static Date getBeginWeekDate() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return cal.getTime();
}
public static Date getEndWeekDate() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 8 - dayofweek);
return cal.getTime();
}
public static Date getStartDate() {
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTime();
}
public static Date getStartDate1() {
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR_OF_DAY,07);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTime();
}
public static Date getEndDate() {
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTime();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2021-11-04 throw和throws有什么不同?