import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public final class SimpleDateUtils {
private static final SimpleDateFormat DATE_FORMATTER;
static {
DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
public static String getDateString(final String... format) {
return format.length == 0 ? DATE_FORMATTER.format(new Date()).toString()
: new SimpleDateFormat(format[0]).format(new Date()).toString();
}
public static Date parseDate(final String time) {
try {
return DATE_FORMATTER.parse(time);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static String dateToString(final Date date, final String... args) {
return args.length == 0 ? DATE_FORMATTER.format(date).toString()
: new SimpleDateFormat(args[0]).format(date).toString();
}
public static boolean within(final Date startTime, final Date validateTime, final int time, int... timeunit) {
Calendar submitCal = Calendar.getInstance();
submitCal.setTime(startTime);
Calendar after = Calendar.getInstance();
after.setTime(startTime);
after.add(timeunit.length > 0 ? timeunit[0] : Calendar.SECOND, +time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(validateTime);
return calendar.after(submitCal) && calendar.before(after);
}
}
java判断date是否在指定的时间区间类
最新推荐文章于 2024-07-07 03:49:54 发布