mirror of https://github.com/halo-dev/halo
Add add method in DateUtils
parent
0b35392b1a
commit
6ed9a927ac
|
@ -38,6 +38,7 @@ public class HaloProperties {
|
|||
private String workDir = HaloConst.USER_HOME + "/.halo/";
|
||||
|
||||
public HaloProperties() throws IOException {
|
||||
// Create work directory if not exist
|
||||
Files.createDirectories(Paths.get(workDir));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import org.springframework.util.Assert;
|
|||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Date utilities.
|
||||
|
@ -41,4 +42,50 @@ public class DateUtils {
|
|||
calendar.setTime(date);
|
||||
return calendar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds date.
|
||||
*
|
||||
* @param date current date must not be null
|
||||
* @param time time must not be less than 1
|
||||
* @param timeUnit time unit must not be null
|
||||
* @return added date
|
||||
*/
|
||||
public static Date add(@NonNull Date date, long time, @NonNull TimeUnit timeUnit) {
|
||||
Assert.notNull(date, "Date must not be null");
|
||||
Assert.isTrue(time >= 0, "Addition time must not be less than 1");
|
||||
Assert.notNull(timeUnit, "Time unit must not be null");
|
||||
|
||||
Date result;
|
||||
|
||||
int timeIntValue;
|
||||
|
||||
if (time > Integer.MAX_VALUE) {
|
||||
timeIntValue = Integer.MAX_VALUE;
|
||||
} else {
|
||||
timeIntValue = Long.valueOf(time).intValue();
|
||||
}
|
||||
|
||||
// Calc the expiry time
|
||||
switch (timeUnit) {
|
||||
case DAYS:
|
||||
result = org.apache.commons.lang3.time.DateUtils.addDays(date, timeIntValue);
|
||||
break;
|
||||
case HOURS:
|
||||
result = org.apache.commons.lang3.time.DateUtils.addHours(date, timeIntValue);
|
||||
break;
|
||||
case MINUTES:
|
||||
result = org.apache.commons.lang3.time.DateUtils.addMinutes(date, timeIntValue);
|
||||
break;
|
||||
case SECONDS:
|
||||
result = org.apache.commons.lang3.time.DateUtils.addSeconds(date, timeIntValue);
|
||||
break;
|
||||
case MILLISECONDS:
|
||||
result = org.apache.commons.lang3.time.DateUtils.addMilliseconds(date, timeIntValue);
|
||||
break;
|
||||
default:
|
||||
result = date;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue