时间工具类
import org.apache.commons.lang3.time.FastDateFormat
object DateUtils {
def getTodayDate(): String = {
val nowDate = new Date()
FastDateFormat.getInstance("yyyy-MM-dd").format(nowDate)
}
def getYesterdayDate(): String = {
val calendar: Calendar = Calendar.getInstance()
calendar.add(Calendar.DATE, -1)
FastDateFormat.getInstance("yyyy-MM-dd").format(calendar)
}
def main(args: Array[String]): Unit = {
println(s"Today: ${getTodayDate()}")
println(s"Yesterday: ${getYesterdayDate()}")
}
}
import org.apache.commons.lang3.StringUtils;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TimeParseUtils {
public static Date parse(String str) {
if (StringUtils.isBlank(str)) {
return null;
}
Pattern pattern = Pattern.compile("\\d{10}");
if (pattern.matcher(str).matches()) {
return new Date(Integer.parseInt(str) * 1000L);
}
pattern = Pattern.compile("(\\d*)(\\w+)");
if ("now".equals(str) || str.length() == 0) {
return new Date();
}
if ("rtnow".equals(str) || "rt".equals(str)) {
return null;
}
if (str.startsWith("@")) {
return parseStr(new Date(), str.substring(1));
} else if (str.indexOf("@") != -1) {
String[] arr = str.split("@");
Date dt = parseStr(pattern, arr[0]);
return parseStr(dt, arr[1]);
} else if (str.startsWith("rt")) {
return parseStr(pattern, str.substring(2));
} else {
return parseStr(pattern, str);
}
}
public static Long parseLong(String str, Long defaultValue) {
Date dt = parse(str);
return dt != null ? dt.getTime() : defaultValue;
}
public static Long parseLong(String str) {
Date dt = parse(str);
return dt != null ? dt.getTime() : -1;
}
private static Date parseStr(Date date, String unit) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MILLISECOND, 0);
switch (unit) {
case "s":
break;
case "m":
calendar.set(Calendar.SECOND, 0);
break;
case "h":
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
break;
case "w1":
case "w2":
case "w3":
case "w4":
case "w5":
case "w6":
case "w7":
case "d":
if (unit.startsWith("w")) {
int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (day_of_week == 0) {
day_of_week = 7;
}
calendar.add(Calendar.DATE, -day_of_week + Integer.parseInt(unit.substring(1)));
}
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
break;
case "mon":
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DATE, 1);
break;
case "q":
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DATE, 1);
int month = calendar.get(calendar.MONTH) + 1;
int q = month / 3;
calendar.set(Calendar.MONTH, q * 3);
break;
case "y":
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DATE, 1);
calendar.set(Calendar.MONTH, 0);
break;
default:
throw new RuntimeException("time not support " + unit);
}
return calendar.getTime();
}
private static Date parseStr(Pattern pattern, String str) {
Matcher matcher = pattern.matcher(str);
if (!matcher.find()) {
throw new RuntimeException("time not support " + str);
}
int num = Integer.parseInt(matcher.group(1));
String unit = matcher.group(2);
num = str.startsWith("-") ? 0 - num : num;
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.MILLISECOND, 0);
switch (unit) {
case "s":
calendar.add(Calendar.SECOND, num);
break;
case "m":
calendar.add(Calendar.MINUTE, num);
break;
case "h":
calendar.add(Calendar.HOUR_OF_DAY, num);
break;
case "d":
calendar.add(Calendar.DATE, num);
break;
case "w":
calendar.add(Calendar.DATE, num * 7);
break;
case "mon":
calendar.add(Calendar.MONTH, num);
break;
case "q":
calendar.add(Calendar.MONTH, num * 3);
break;
case "y":
calendar.add(Calendar.YEAR, num);
break;
case "w1":
case "w2":
case "w3":
case "w4":
case "w5":
case "w6":
case "w7":
calendar.add(Calendar.DAY_OF_WEEK, num);
break;
default:
throw new RuntimeException("time not support " + str);
}
return calendar.getTime();
}
}
import org.junit.Test;
import java.text.SimpleDateFormat;
public class TimeParseTest {
static SimpleDateFormat standerTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Test
public void parseStr() {
parse("rt-10mon", null);
parse("1587872004", null);
parse("rtnow", null);
parse("rt-15m", null);
parse("@s", null);
parse("@m", null);
parse("@h", null);
parse("@d", null);
parse("@mon", null);
parse("@q", null);
parse("@y", null);
parse("@w1", null);
parse("@w2", null);
parse("@w3", null);
parse("@w4", null);
parse("@w5", null);
parse("@w6", null);
parse("@w7", null);
System.out.println("----------------");
parse("@s", null);
parse("-1s", null);
parse("-1m", null);
parse("-1h", null);
parse("-1d", null);
parse("-1w", null);
parse("-1mon", null);
parse("-2mon", null);
parse("-1q", null);
parse("-2q", null);
parse("-1y", null);
System.out.println("----------------");
parse("@s", null);
parse("1s", null);
parse("1m", null);
parse("1h", null);
parse("1d", null);
parse("1w", null);
parse("1mon", null);
parse("2mon", null);
parse("1q", null);
parse("2q", null);
parse("1y", null);
System.out.println("----------------");
parse("@s", null);
parse("-10m", null);
parse("-4h@m", null);
parse("@d", null);
parse("-1d@d", null);
parse("-2d@d", null);
parse("@w1", null);
parse("@w7", null);
parse("-7d@w1", null);
parse("@mon", null);
parse("-1mon@mon", null);
parse("@q", null);
parse("-1q@q", null);
parse("1q@q", null);
parse("2q@q", null);
parse("-1q@q", null);
parse("-2q@q", null);
parse("@y", null);
parse("-1y@y", null);
}
private static void parse(String str, String expect) {
Object obj = TimeParseUtils.parse(str);
if (obj != null) {
String output = standerTimeFormat.format(obj);
if (expect != null) {
System.out.println(str + " " + output + " " + expect + " => " + output.equals(expect));
} else {
System.out.println(str + "->" + output);
}
}
}
}