From eeca705ec06dc6b081844b1569c51e8f7cbd871d Mon Sep 17 00:00:00 2001 From: jdotcms Date: Fri, 21 Feb 2025 18:52:47 -0600 Subject: [PATCH 1/2] #31420 adding date tool to timestamp --- .../velocity/tools/generic/DateTool.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/dotCMS/src/main/java/org/apache/velocity/tools/generic/DateTool.java b/dotCMS/src/main/java/org/apache/velocity/tools/generic/DateTool.java index d6fbc17d1b5e..8364a07a28d0 100644 --- a/dotCMS/src/main/java/org/apache/velocity/tools/generic/DateTool.java +++ b/dotCMS/src/main/java/org/apache/velocity/tools/generic/DateTool.java @@ -20,6 +20,7 @@ import java.lang.reflect.Field; +import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -828,6 +829,81 @@ public Date toDate(String format, Object obj, } } + //////////// + + /** + * Converts an object to an instance of {@link Timestamp} using the + * format returned by {@link #getFormat()},the {@link Locale} returned + * by {@link #getLocale()}, and the {@link TimeZone} returned by + * {@link #getTimeZone()} if the object is not already an instance + * of Date, Calendar, or Long. + * + * @param obj the date to convert + * @return the object as a {@link Timestamp} or null if no + * conversion is possible + */ + public Timestamp toTimestamp(final Object obj) + { + return new Timestamp(toDate(obj).getTime()); + } + + /** + * Converts an object to an instance of {@link Timestamp} using the + * specified format,the {@link Locale} returned by + * {@link #getLocale()}, and the {@link TimeZone} returned by + * {@link #getTimeZone()} if the object is not already an instance + * of Date, Calendar, or Long. + * + * @param format - the format the date is in + * @param obj - the date to convert + * @return the object as a {@link Timestamp} or null if no + * conversion is possible + * @see #toDate(String format, Object obj, Locale locale) + */ + public Timestamp toTimestamp(final String format, final Object obj) + { + return new Timestamp(toDate(format, obj).getTime()); + } + + /** + * Converts an object to an instance of {@link Timestamp} using the + * specified format and {@link Locale} if the object is not already + * an instance of Date, Calendar, or Long. + * + * @param format - the format the date is in + * @param obj - the date to convert + * @param locale - the {@link Locale} + * @return the object as a {@link Timestamp} or null if no + * conversion is possible + * @see SimpleDateFormat#parse + */ + public Timestamp toTimestamp(final String format, final Object obj, final Locale locale) + { + return new Timestamp(toDate(format, obj, locale).getTime()); + } + + /** + * Converts an object to an instance of {@link Timestamp} using the + * specified format, {@link Locale}, and {@link TimeZone} if the + * object is not already an instance of Date, Calendar, or Long. + * + * @param format - the format the date is in + * @param obj - the date to convert + * @param locale - the {@link Locale} + * @param timezone - the {@link TimeZone} + * @return the object as a {@link Timestamp} or null if no + * conversion is possible + * @see #getDateFormat + * @see SimpleDateFormat#parse + */ + public Timestamp toTimestamp(final String format, final Object obj, + final Locale locale, final TimeZone timezone) + { + return new Timestamp(toDate(format, obj, locale, timezone).getTime()); + } + + /////////// + /** * Converts an object to an instance of {@link Calendar} using the * locale returned by {@link #getLocale()} if necessary. From 20111d12031069f26708fe08bcae3456635b8409 Mon Sep 17 00:00:00 2001 From: jdotcms Date: Fri, 21 Feb 2025 18:53:09 -0600 Subject: [PATCH 2/2] #31420 adding date tool to timestamp --- .../postman/DateTool.postman_collection.json | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 dotcms-postman/src/main/resources/postman/DateTool.postman_collection.json diff --git a/dotcms-postman/src/main/resources/postman/DateTool.postman_collection.json b/dotcms-postman/src/main/resources/postman/DateTool.postman_collection.json new file mode 100644 index 000000000000..b6d2e5881e75 --- /dev/null +++ b/dotcms-postman/src/main/resources/postman/DateTool.postman_collection.json @@ -0,0 +1,70 @@ +{ + "info": { + "_postman_id": "04e24c3e-7d51-4e2b-be4a-e63094ab158d", + "name": "DateTool", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "781456" + }, + "item": [ + { + "name": "TestToTimeStamp", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code should be 200\", function () {", + " pm.response.to.have.status(200);", + "});", + "", + "var text = pm.response.text()", + "", + "pm.test(\"Right date\", function () {", + " pm.expect(text).to.be.eql('2025-02-06 00:00:00.0');", + "});", + "" + ], + "type": "text/javascript", + "packages": {} + } + } + ], + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{jwt}}", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "$date.toTimestamp('yyyy-MM-dd','2025-02-06')", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{serverURL}}/api/vtl/dynamic/", + "host": [ + "{{serverURL}}" + ], + "path": [ + "api", + "vtl", + "dynamic", + "" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file