Add getTime function #294

This commit is contained in:
Francis Dong
2021-09-01 13:17:14 +08:00
committed by dxfeng10
parent 46743b8cb8
commit 7e9eeef834
3 changed files with 57 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -60,6 +61,7 @@ public class DateFunc implements IFunc {
public void init() {
FuncExecutor.register(NAME_SPACE_PREFIX + "date.timestamp", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "date.getTime", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "date.now", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "date.add", this);
FuncExecutor.register(NAME_SPACE_PREFIX + "date.formatTs", this);
@@ -98,6 +100,23 @@ public class DateFunc implements IFunc {
public long timestamp() {
return System.currentTimeMillis();
}
/**
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
* represented by this Date object.
*
* @param date date string
* @param pattern Date time pattern
* @param timeZone [optional] timeZone
* @return
*/
public Long getTime(String date, String pattern, String... timeZone) {
if (StringUtils.isBlank(date)) {
return null;
}
Date d = parse(date, pattern, timeZone);
return d.getTime();
}
/**
* Returns current time with the given pattern<br>
@@ -139,6 +158,9 @@ public class DateFunc implements IFunc {
* @return
*/
public String add(String date, String pattern, int field, int amount, String... timeZone) {
if (StringUtils.isBlank(date)) {
return null;
}
Date d = parse(date, pattern, timeZone);
if (d != null) {
// convert to calendar field
@@ -183,7 +205,10 @@ public class DateFunc implements IFunc {
* @param timeZone [optional] timeZone
* @return
*/
public String formatTs(long timestamp, String pattern, String... timeZone) {
public String formatTs(Long timestamp, String pattern, String... timeZone) {
if (timestamp == null) {
return null;
}
return formatDate(new Date(timestamp), pattern, timeZone);
}
@@ -197,6 +222,9 @@ public class DateFunc implements IFunc {
* @return
*/
public String changePattern(String dateStr, String sourcePattern, String targetPattern, String... timeZone) {
if (StringUtils.isBlank(dateStr)) {
return null;
}
return formatDate(parse(dateStr, sourcePattern, timeZone), targetPattern, timeZone);
}
@@ -225,6 +253,9 @@ public class DateFunc implements IFunc {
* @return
*/
private Date parse(String dateStr, String pattern, String... timeZone) {
if (StringUtils.isBlank(dateStr)) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern == null ? DATE_TIME_FORMAT : pattern);
if (timeZone != null && timeZone.length > 0) {
sdf.setTimeZone(TimeZone.getTimeZone(timeZone[0]));
@@ -254,6 +285,9 @@ public class DateFunc implements IFunc {
* @return
*/
private String formatDate(Date date, String pattern, String... timeZone) {
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern == null ? DATE_TIME_FORMAT : pattern);
if (timeZone != null && timeZone.length > 0) {
sdf.setTimeZone(TimeZone.getTimeZone(timeZone[0]));

View File

@@ -119,6 +119,9 @@ public class StringFunc implements IFunc {
* {@code -1} if there is no such occurrence.
*/
public int indexOf(String str, String substr) {
if (StringUtils.isBlank(str)) {
return -1;
}
return str.indexOf(substr);
}
@@ -133,6 +136,9 @@ public class StringFunc implements IFunc {
* object as determined by the {@link #equals(Object)} method.
*/
public boolean startsWith(String str, String prefix) {
if (StringUtils.isBlank(str)) {
return false;
}
return str.startsWith(prefix);
}
@@ -147,14 +153,23 @@ public class StringFunc implements IFunc {
* object as determined by the {@link #equals(Object)} method.
*/
public boolean endsWith(String str, String suffix) {
if (StringUtils.isBlank(str)) {
return false;
}
return str.endsWith(suffix);
}
public String toUpperCase(String str) {
if (StringUtils.isBlank(str)) {
return str;
}
return str.toUpperCase();
}
public String toLowerCase(String str) {
if (StringUtils.isBlank(str)) {
return str;
}
return str.toLowerCase();
}

View File

@@ -51,6 +51,13 @@ class DateFuncTests {
// System.out.println(result);
// }
@Test
void testGetTime() {
String funcExpression = "fn.date.getTime(\"2021-07-09 22:44:55\", \"yyyy-MM-dd HH:mm:ss\")";
Long result = (Long)FuncExecutor.getInstance().exec(null, funcExpression);
assertEquals(1625841895000l, result.longValue());
}
@Test
void testAdd() {
String funcExpression = "fn.date.add(\"2021-07-09 22:44:55\", \"yyyy-MM-dd HH:mm:ss\", 1, 1000)";