Add default timezone to date function

This commit is contained in:
Francis Dong
2021-08-31 10:45:55 +08:00
parent f3a9b00a44
commit 8d283d27d4

View File

@@ -21,6 +21,8 @@ import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -176,8 +178,8 @@ public class DateFunc implements IFunc {
* @param pattern * @param pattern
* @return * @return
*/ */
public String formatTs(long timestamp, String pattern) { public String formatTs(long timestamp, String pattern, String... timeZone) {
return formatDate(new Date(timestamp), pattern); return formatDate(new Date(timestamp), pattern, timeZone);
} }
/** /**
@@ -238,8 +240,13 @@ public class DateFunc implements IFunc {
* dafault yyyy-MM-dd HH:mm:ss * dafault yyyy-MM-dd HH:mm:ss
* @return * @return
*/ */
private String formatDate(Date date, String pattern) { private String formatDate(Date date, String pattern, String... timeZone) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern == null ? DATE_TIME_FORMAT : pattern); SimpleDateFormat sdf = new SimpleDateFormat(pattern == null ? DATE_TIME_FORMAT : pattern);
if (timeZone != null && timeZone.length > 0) {
sdf.setTimeZone(TimeZone.getTimeZone(timeZone[0]));
} else {
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
}
return sdf.format(date); return sdf.format(date);
} }