-
Notifications
You must be signed in to change notification settings - Fork 0
Computing Functions Datetime
AddDays | Adds or subtracts the specified number of days |
AddHours | Adds or subtracts a specified number of hours |
AddMinutes | Adds or subtracts the specified number of minutes |
AddMonths | Adds or subtracts a specified number of months |
AddSeconds | Adds or subtracts a specified number of seconds |
AddWeeks | Adds or subtracts a specified number of weeks |
AddYears | Adds or subtracts a specified number of years |
DateToStr | Convert java.util.Date to string with format |
LastDayInMonth | Returns the last day of the month |
LastDayInYear | Returns the last day of the year |
SysDate | Returns the current date without time, that is, the time will always be 00:00:00 |
SysTime | Returns the current datetime |
ToDate | Converts a string with format to java.util.Date |
ToTimeZone | Converts a date to a different time zone |
Adds or subtracts values for date components.
addSeconds(date, seconds)
addMinutes(date, minutes)
addHours(date, hours)
addDays(date, days)
addWeeks(date, weeks)
addMonths(date, months)
addYears(date, years)
date
The date from which the component will be increased or decreased.
seconds
, minutes
, hours
, days
, weeks
, months
, years
Quantity by how much to change the date component
New date, type : Date
public class Test {
@org.junit.Test
public void testPlus() {
ObjCalcEngine engine = new ObjCalcEngine();
DateFormat df = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss");
//Seconds
Date date = engine.calc(" addSeconds(todate('2018.01.01', 'yyyy.MM.dd'), 5) ", Date.class);
assertEquals("2018.01.01-00:00:05", df.format(date));
date = engine.calc(" addSeconds(todate('2018.01.01', 'yyyy.MM.dd'), -5) ", Date.class);
assertEquals("2017.12.31-23:59:55", df.format(date));
//Minutes
date = engine.calc(" addMinutes(todate('2018.01.01', 'yyyy.MM.dd'), 5) ", Date.class);
assertEquals("2018.01.01-00:05:00", df.format(date));
date = engine.calc(" addMinutes(todate('2018.01.01', 'yyyy.MM.dd'), -5) ", Date.class);
assertEquals("2017.12.31-23:55:00", df.format(date));
//Hours
date = engine.calc(" addHours(todate('2018.01.01', 'yyyy.MM.dd'), 5) ", Date.class);
assertEquals("2018.01.01-05:00:00", df.format(date));
date = engine.calc(" addHours(todate('2018.01.01', 'yyyy.MM.dd'), -5) ", Date.class);
assertEquals("2017.12.31-19:00:00", df.format(date));
//Days
date = engine.calc(" addDays(todate('2018.01.01', 'yyyy.MM.dd'), 5) ", Date.class);
assertEquals("2018.01.06-00:00:00", df.format(date));
date = engine.calc(" adddays(todate('2018.01.01', 'yyyy.MM.dd'), -5) ", Date.class);
assertEquals("2017.12.27-00:00:00", df.format(date));
//Weeks
date = engine.calc(" addWeeks(todate('2018.01.01', 'yyyy.MM.dd'), 5) ", Date.class);
assertEquals("2018.02.05-00:00:00", df.format(date));
date = engine.calc(" addWeeks(todate('2018.01.01', 'yyyy.MM.dd'), -5) ", Date.class);
assertEquals("2017.11.27-00:00:00", df.format(date));
//Months
date = engine.calc(" addmonths(todate('2018.01.01', 'yyyy.MM.dd'), 5) ", Date.class);
assertEquals("2018.06.01-00:00:00", df.format(date));
date = engine.calc(" addmonths(todate('2018.01.01', 'yyyy.MM.dd'), -5) ", Date.class);
assertEquals("2017.08.01-00:00:00", df.format(date));
//Years
date = engine.calc(" addYears(todate('2018.01.01', 'yyyy.MM.dd'), 5) ", Date.class);
assertEquals("2023.01.01-00:00:00", df.format(date));
date = engine.calc(" addYears(todate('2018.01.01', 'yyyy.MM.dd'), -5) ", Date.class);
assertEquals("2013.01.01-00:00:00", df.format(date));
}
}
Convert date to string with format and back
dateToStr(date, format)
todate(string, format)
date
Date value. By default, the value can be of the type java.util.Date
, java.time.LocalDateTime
or java.time.LocalDate
.
string
String with date.
format
The Output format string with date.
Here is the full list of supported date and time format characters from the Javadocs.
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G | Era designator | Text | AD |
y | Year | Year |
1996 ; 96
|
Y | Week year | Year |
2009 ; 09
|
M | Month in year (context sensitive) | Month |
July ; Jul ; 07
|
L | Month in year (standalone form) | Month |
July ; Jul ; 07
|
w | Week in year | Number | 27 |
W | Week in month | Number | 2 |
D | Day in year | Number | 189 |
d | Day in month | Number | 10 |
F | Day of week in month | Number | 2 |
E | Day name in week | Text |
Tuesday ; Tue
|
u | Day number of week (1 = Monday, …, 7 = Sunday) | Number | 1 |
a | Am/pm marker | Text | PM |
H | Hour in day (0-23) | Number | 0 |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | 0 |
h | Hour in am/pm (1-12) | Number | 12 |
m | Minute in hour | Number | 30 |
s | Second in minute | Number | 55 |
S | Millisecond | Number | 978 |
z | Time zone | General time zone |
Pacific Standard time ; PST ; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |
X | Time zone | ISO 8601 time zone |
-08 ; -0800 ; -08:00
|
Value with type String
for dateToStr
Value with type java.util.Date
for toDate
ObjCalcEngine engine = new ObjCalcEngine();
String etalon = "2018.11.23-15:43:38";
DateFormat df = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss");
Date date = df.parse(etalon);
//toString Date
String result1 = engine.calc("dateToStr(., 'yyyy.MM.dd-HH:mm:ss')", date, String.class);
assertEquals(etalon, result1);
//toString LocalDateTime
LocalDateTime ldt = LocalDateTime.parse(etalon, DateTimeFormatter.ofPattern("yyyy.MM.dd-HH:mm:ss"));
String result2 = engine.calc("dateToStr(., 'yyyy.MM.dd-HH:mm:ss')", ldt, String.class);
assertEquals(etalon, result2);
//toString LocalDate
LocalDate ldate = LocalDate.parse(etalon, DateTimeFormatter.ofPattern("yyyy.MM.dd-HH:mm:ss"));
String result3 = engine.calc("dateToStr(., 'yyyy.MM.dd-HH:mm:ss')", ldate, String.class);
assertEquals("2018.11.23-00:00:00", result3);
//toDate
Date result = engine.calc("todate('2018.11.23-15:43:38', 'yyyy.MM.dd-HH:mm:ss')", Date.class);
Date sample = df.parse("2018.11.23-15:43:38");
assertEquals(sample, result);
Returns the last day of the month or year, respectively.
date
Current date to calculate last day
Last day of the month or year, type : java.util.Date
ObjCalcEngine engine = new ObjCalcEngine();
DateFormat df = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss");
//lastDayInMonth
Date date = engine.calc(" lastDayInMonth(todate('2018.02.01', 'yyyy.MM.dd')) ", Date.class);
assertEquals("2018.02.28-00:00:00", df.format(date));
date = engine.calc(" lastDayInMonth(todate('2018.02.01-10:35:17', 'yyyy.MM.dd-HH:mm:ss')) ", Date.class);
assertEquals("2018.02.28-10:35:17", df.format(date));
//lastDayInYear
date = engine.calc(" lastDayInYear(todate('2018.02.01', 'yyyy.MM.dd')) ", Date.class);
assertEquals("2018.12.31-00:00:00", df.format(date));
date = engine.calc(" lastDayInYear(todate('2018.02.01-10:35:17', 'yyyy.MM.dd-HH:mm:ss')) ", Date.class);
assertEquals("2018.12.31-10:35:17", df.format(date));
Returns the current date or the current datetime.
No parameters
The current date or the current datetime, type : java.util.Date
ObjCalcEngine engine = new ObjCalcEngine();
System.out.println(engine.calc(" sysDate "));
System.out.println(engine.calc(" sysTime "));
Output
2020-10-25
Sun Oct 25 17:23:19 MSK 2020
Converts a date to a different time zone
time
DateTime in any type java.util.Date or java.time.*
zone
Offset to the time zone in any format recognized by java.time.ZoneOffset
DateTime converted to new timezone. As default in java.time.OffsetDateTime
String result = engine.calc(
"dateToStr(toTimeZone(toDate('2020.03.23 12:54:16+03:00', 'yyyy.MM.dd HH:mm:ssXXX'), " +
" '+05'" +
" ), " +
" 'yyyy.MM.dd HH:mm:ssXXX'" +
" )", String.class);
assertEquals("2020.03.23 14:54:16+05:00", result);
- Home
- Extendable Query Language
- Computing
- Simple Usage
- Own Value Extraction
- Functions
- What is Type
- Known Types
- Add Own Type
- Data conversion
- Generate SQL Query
- Find files