Merge remote-tracking branch 'origin/后台运营yy' into 后台运营yy

后台运营yy
yangyang 8 months ago
commit 0c160ab52e

@ -5,9 +5,21 @@ import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
// DateUtils类用于提供一系列与日期处理相关的实用方法包括日期格式化、日期解析、日期之间的差值计算以及在日期上进行增减操作等功能方便在不同的业务场景中对日期进行操作和处理。
public class DateUtils { public class DateUtils {
// 定义默认的日期格式字符串,格式为"yyyy-MM-dd",在一些方法中若未指定具体格式时,会使用该默认格式进行日期相关的操作。
public static final String DEFAULT_PATTERN = "yyyy-MM-dd"; public static final String DEFAULT_PATTERN = "yyyy-MM-dd";
/**
*
* Calendar
* 使使
*
* @param day 1-1
* @param pattern "yyyy-MM-dd HH:mm:ss"null使DEFAULT_PATTERN
* @return
*/
public static String getOneDayFromNow(int day, String pattern) { public static String getOneDayFromNow(int day, String pattern) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTime(new Date()); cal.setTime(new Date());
@ -16,23 +28,30 @@ public class DateUtils {
return sdf.format(cal.getTime()); return sdf.format(cal.getTime());
} }
/**
* 使"yyyy-MM-dd"
* getOneDayFromNow(int day, String pattern)使
*
* @param day 1-1
* @return "yyyy-MM-dd"
*/
public static String getOneDayFromNow(int day) { public static String getOneDayFromNow(int day) {
return DateUtils.getOneDayFromNow(day, DEFAULT_PATTERN); return DateUtils.getOneDayFromNow(day, DEFAULT_PATTERN);
} }
/** /**
* *
* * "yyyy-MM-dd"00:00:00
* @param smdate * 1000 * 3600 * 24
* *
* @param bdate * ParseException
* *
* @return * @param smdate Date
* @throws ParseException * @param bdate Date
* @throws java.text.ParseException * @return bdatesmdatebdatesmdate
* @throws ParseException
*/ */
public static int daysBetween(Date smdate, Date bdate) public static int daysBetween(Date smdate, Date bdate) throws ParseException {
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
smdate = sdf.parse(sdf.format(smdate)); smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate)); bdate = sdf.parse(sdf.format(bdate));
@ -46,83 +65,91 @@ public class DateUtils {
return Integer.parseInt(String.valueOf(between_days)); return Integer.parseInt(String.valueOf(between_days));
} }
// 以下是main方法用于简单测试daysBetween方法不过当前传入的参数为null实际运行可能会出现空指针异常等问题正常使用时应传入有效的日期对象进行测试。
public static void main(String[] args) throws ParseException { public static void main(String[] args) throws ParseException {
System.out.println(daysBetween(null, null)); System.out.println(daysBetween(null, null));
} }
// 定义一系列常用的日期格式字符串常量,方便在不同的日期格式化场景中使用,以下是各常量对应的格式说明:
/** /**
* 2010-12-01 * 2010-12-01
*/ */
public static String FORMAT_SHORT = "yyyy-MM-dd"; public static String FORMAT_SHORT = "yyyy-MM-dd";
/** /**
* 2010-12-01 23:15:06 * 2010-12-01 23:15:06
*/ */
public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss"; public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
/** /**
* yyyy-MM-dd HH:mm:ss.S * yyyy-MM-dd HH:mm:ss.S
*/ */
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S"; public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/** /**
* 20101201 * 20101201
*/ */
public static String FORMAT_SHORT_CN = "yyyy年MM月dd"; public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
/** /**
* 20101201 231506 * 20101201 231506
*/ */
public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒"; public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
/** /**
* * yyyyMMdd HHmmssSSS
*/ */
public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒"; public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
/** /**
* date pattern * "yyyy-MM-dd HH:mm:ss"
*
* @return String"yyyy-MM-dd HH:mm:ss"
*/ */
public static String getDatePattern() { public static String getDatePattern() {
return FORMAT_LONG; return FORMAT_LONG;
} }
/** /**
* * "yyyy-MM-dd HH:mm:ss"
* * format(Date date)Date
* @return *
* @return "2024-12-15 12:30:00"
*/ */
public static String getNow() { public static String getNow() {
return format(new Date()); return format(new Date());
} }
/** /**
* *
* * format(Date date, String pattern)Date
* @param format *
* @return * @param format "yyyy-MM-dd""yyyy年MM月dd日"
* @return "yyyy-MM-dd""2024-12-15"
*/ */
public static String getNow(String format) { public static String getNow(String format) {
return format(new Date(), format); return format(new Date(), format);
} }
/** /**
* 使 * 使"yyyy-MM-dd HH:mm:ss"
* * format(Date date, String pattern)
* @param date *
* @return * @param date new Date()
* @return "2024-12-15 12:30:00"
*/ */
public static String format(Date date) { public static String format(Date date) {
return format(date, getDatePattern()); return format(date, getDatePattern());
} }
/** /**
* 使 * 使
* * nullnullSimpleDateFormat使
* @param date * formatnull
* *
* @param pattern * @param date Datenew Date()
* * @param pattern "yyyy-MM-dd""yyyy年MM月dd日"
* @return * @return "yyyy-MM-dd""2024-12-15"datenull
*/ */
public static String format(Date date, String pattern) { public static String format(Date date, String pattern) {
String returnValue = ""; String returnValue = "";
if (date != null) { if (date!= null) {
SimpleDateFormat df = new SimpleDateFormat(pattern); SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date); returnValue = df.format(date);
} }
@ -130,13 +157,13 @@ public class DateUtils {
} }
/** /**
* 使 * 使197011000
* * DateLong.parseLongDate
* @param timestamp * 使SimpleDateFormatDate
* *
* @param pattern * @param timestamp "1672809600000"
* * @param pattern "yyyy-MM-dd""yyyy年MM月dd日"
* @return * @return "yyyy-MM-dd""2024-12-15"
*/ */
public static String format(String timestamp, String pattern) { public static String format(String timestamp, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern); SimpleDateFormat sdf = new SimpleDateFormat(pattern);
@ -144,24 +171,24 @@ public class DateUtils {
} }
/** /**
* 使 * 使"yyyy-MM-dd HH:mm:ss"Date
* * parse(String strDate, String pattern)null
* @param strDate *
* * @param strDate "2024-12-15""2024-12-15 12:30:00"
* @return * @return Datenull
*/ */
public static Date parse(String strDate) { public static Date parse(String strDate) {
return parse(strDate, getDatePattern()); return parse(strDate, getDatePattern());
} }
/** /**
* 使 * 使Date
* * SimpleDateFormat使parse
* @param strDate * null便
* *
* @param pattern * @param strDate "2024-12-15""2024年12月15日"
* * @param pattern "yyyy-MM-dd""yyyy年MM月dd日"
* @return * @return Datenull
*/ */
public static Date parse(String strDate, String pattern) { public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern); SimpleDateFormat df = new SimpleDateFormat(pattern);
@ -174,13 +201,13 @@ public class DateUtils {
} }
/** /**
* *
* * Calendar使addn
* @param date *
* *
* @param n * @param date Date
* * @param n 33-22
* @return * @return
*/ */
public static Date addMonth(Date date, int n) { public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
@ -190,13 +217,13 @@ public class DateUtils {
} }
/** /**
* *
* * addMonthCalendar使addn
* @param date *
* *
* @param n * @param date Date
* * @param n 55-11
* @return * @return
*/ */
public static Date addDay(Date date, int n) { public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
@ -206,7 +233,10 @@ public class DateUtils {
} }
/** /**
* * "yyyy-MM-dd HH:mm:ss.S"
* SimpleDateFormat使Calendar
*
* @return "2024-12-15 12:30:00.123"
*/ */
public static String getTimeString() { public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL); SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
@ -214,6 +244,10 @@ public class DateUtils {
return df.format(calendar.getTime()); return df.format(calendar.getTime());
} }
/**
*
* format(Date date)
/** /**
* *
* *
@ -221,16 +255,28 @@ public class DateUtils {
* *
* @return * @return
*/ */
/**
*
* `format(Date date)`4
* 44 "2024-12-15"
*
* @param date null`format`
* @return "2024"
*/
public static String getYear(Date date) { public static String getYear(Date date) {
return format(date).substring(0, 4); return format(date).substring(0, 4);
} }
/** /**
* * `getDatePattern` "yyyy-MM-dd HH:mm:ss"
* *
* @param date * 1. `Calendar.getInstance().getTime().getTime()` `t`
* * 2. `Date` `Date` `c.getTime().getTime()` `t1`
* @return * 3. `t - t1`1000360024
* `parse` `null`
*
* @param date "2024-12-10"
* @return
*/ */
public static int countDays(String date) { public static int countDays(String date) {
long t = Calendar.getInstance().getTime().getTime(); long t = Calendar.getInstance().getTime().getTime();
@ -241,13 +287,16 @@ public class DateUtils {
} }
/** /**
* *
* *
* @param date * 1. `Calendar.getInstance().getTime().getTime()` `t`
* * 2. `parse(date, format)` `Date` `Date` `c.getTime().getTime()` `t1`
* @param format * 3. `t - t1`1000360024
* * `parse` `null`
* @return *
* @param date "2024年12月10日" `format`
* @param format "yyyy年MM月dd日"
* @return
*/ */
public static int countDays(String date, String format) { public static int countDays(String date, String format) {
long t = Calendar.getInstance().getTime().getTime(); long t = Calendar.getInstance().getTime().getTime();
@ -256,19 +305,34 @@ public class DateUtils {
long t1 = c.getTime().getTime(); long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24; return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
} }
/**
*
*
* 1. `null` `Date` `new Date()`
* 2. `Calendar` `cal.setTime(date)`
* 3. `flag` `flag` `true`30 `cal.add(Calendar.DATE, -30)` `flag` `false` `cal.add(Calendar.DATE, 0)`
* 4. 使 `SimpleDateFormat`
*
*
* @param date `null` `null` 使
* @param format "yyyy-MM-dd"
* @param flag `true` 30`false`
* @param beforeDay 使
* @param nowDay 使
* @return `format` `flag`
*/
public static String timeFormat(Date date, String format, Boolean flag, int beforeDay, int nowDay) { public static String timeFormat(Date date, String format, Boolean flag, int beforeDay, int nowDay) {
if(date == null) { if (date == null) {
date = new Date(); date = new Date();
} }
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTime(date); cal.setTime(date);
if(flag) { if (flag) {
cal.add(Calendar.DATE,-30); cal.add(Calendar.DATE, -30);
} else { } else {
cal.add(Calendar.DATE,0); cal.add(Calendar.DATE, 0);
} }
SimpleDateFormat sdf = new SimpleDateFormat(format); SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(cal.getTime()); return sdf.format(cal.getTime());
} }
}
Loading…
Cancel
Save