场景
方法链模式,就是类似 StringBuffer 拼字符串的方法。当某个功能方法参数较多时,可以使用方法链模式。比如,要获取某个源日期之后或之前的日期。这里需要的参数就比较多,比如源日期,源日期格式(如果标准日期是字符串),变换天数(负数是源日期之前,正数源日期之后),结果日期格式(如果需要字符串日期)
方法链模式-关键点
方法链演示类-DateChange
package cn.com.soulfox.date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateChange {
//常用日期格式
public static final String YYYYMMDD = "yyyyMMdd";
public static final String HHMMSS = "HHmmss";
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String TIMESTAMPT = "yyyy-MM-dd HH:mm:ss";
public static final String XML_TIMESTAMPT = "yyyy-MM-dd'T'HH:mm:ss";
//源日期
private String srcDateStr;
//源日期格式
private String srcDateformat;
//Date型源日期
private Date srcDate;
//改变天数
//负数是源日期之前,正数源日期之后
private int days;
//日历对象,
private Calendar calendar;
//结果日期
private Date newDate;
//结果日期格式
private String newDateStrformat;
// private boolean isLog = false;
//构造方法私有,防止直接创建DayChangeUtil对象
private DateChange() {
}
//创建日期变换独享
public static DateChange buildChangeDate() {
return new DateChange();
}
/**
* 设置源日期
*
* @param srcDateStr 字符串日期
* @param srcDateformat 源日期格式
* @return
*/
public DateChange setSrcDate(String srcDateStr, String srcDateformat) {
this.srcDateStr = srcDateStr;
this.srcDateformat = srcDateformat;
this.srcDate = getDate(this.srcDateStr, this.srcDateformat);
this.calendar = Calendar.getInstance();
this.calendar.setTime(this.srcDate);
return this;
}
/**
* 设置源日期
*
* @param srcDate java.util.Date类型日期
* @return
*/
public DateChange setSrcDate(Date srcDate) {
this.srcDate = srcDate;
this.calendar = Calendar.getInstance();
this.calendar.setTime(this.srcDate);
//方法连可以连续调用,关键点就是在这里返回对象本身
return this;
}
/**
* 设置改变天数
* 正数: 当前日期以后日期
* 负数: 当前日期以前日期
* 0: 不变
*
* @param days 改变天数
* @return
*/
public DateChange setChangeDays(int days) {
this.days = days;
if (days == 0) {
this.newDate = this.srcDate;
} else {
calendar.add(Calendar.DATE, this.days);
this.newDate = calendar.getTime();
}
return this;
}
/**
* 源日期之前的日期
*
* @param days
* @return
*/
public DateChange setBeforeDays(int days) {
int tmpDays = days <= 0 ? days : -days;
return setChangeDays(tmpDays);
}
/**
* 源日期之后的日期
*
* @param days
* @return
*/
public DateChange setAfterDays(int days) {
int tmpDays = days >= 0 ? days : -days;
return setChangeDays(tmpDays);
}
/**
* 设置新日期格式
* 不设置默认使用源日期格式
*
* @param newDateFormat
* @return
*/
public DateChange setNewDateFormat(String newDateFormat) {
this.newDateStrformat = newDateFormat;
return this;
}
/**
* 获取字符串型新日期
*
* @return
*/
public String getTargetDateString() {
if (this.newDateStrformat == null) {
if (this.srcDateformat != null) {
this.newDateStrformat = this.srcDateformat;
} else {
this.newDateStrformat = DateChange.TIMESTAMPT;
}
}
String dateString = getDate(this.newDate, this.newDateStrformat);
return dateString;
}
/**
* 获取java.util.Date型新日期
*
* @return
*/
public Date getTargetDate() {
return this.newDate;
}
private Date getDate(String srcDateStr, String srcDateformat) {
if (srcDateStr == null) {
throw new RuntimeException("源日期不能为空");
}
if (srcDateformat == null) {
throw new RuntimeException("源日期格式不能为空");
}
SimpleDateFormat sdf = new SimpleDateFormat(srcDateformat);
try {
return sdf.parse(srcDateStr);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private String getDate(Date srcDate, String srcDateFormat) {
if (srcDate == null) {
throw new RuntimeException("源日期不能为空");
}
if (srcDateFormat == null) {
throw new RuntimeException("源日期格式不能为空");
}
SimpleDateFormat sdf = new SimpleDateFormat(srcDateFormat);
try {
return sdf.format(srcDate);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
测试代码
package cn.com.soulfox.date;
import org.junit.Test;
import java.util.Date;
/**
* @create 2023/11/9 15:17
*/
public class DateChangeUtilTest {
@Test
public void test(){
String targetDateString = DateChange.buildChangeDate()//创建日期转换对象
.setSrcDate(new Date()) //设置Date型源日期
.setAfterDays(3) //获取源日期之后3天的日期
.setNewDateFormat(DateChange.YYYY_MM_DD)//设置结果日期的格式
.getTargetDateString(); //获取结果日期
System.out.println(targetDateString);
}
}