There is a very common case in which you need to convert a String to a Date and Date to String. In many cases, you simply want to parse the Date in one Format and convert it to another. For example, the source data set contains a field LAST_UPDATED
with a value 21Aug2019
which you need to convert to 08/21/2019
.
Current Timestamp as String
com.toolsverse.util.Utils.date2Str(new java.util.Date(), 'yyyy-MM-dd HH:mm:ss')
;
Convert Date/Time to String
To convert java.util.Date object to String, use function com.toolsverse.util.Utils.date2Str(date, format)
.
Convert String to Date/Time
To convert String to java.util.Date object, use the function com.toolsverse.util.Utils.str2Date(string, defaultDate, format)
.
Convert Date/Time in one String Format to another
To convert Date/Time on one string Format to another, use str2Date(dateToStr(...))
.
Example:
// 21Aug2019 -> 08/21/2019
com.toolsverse.util.Utils.date2Str(Utils.str2Date('21Aug2019', null, 'ddMMMMMyyyy'), 'MM/dd/yyyy')
Return current date in the specific time zone
To return the current date in the specific time zone, use the Calendar class:
var calendar = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("EST"));
var currentDateInSpecificTimezone = calendar.get(Calendar.YEAR) + '/'
+ calendar.get(Calendar.MONTH) + '/' +
calendar.get(Calendar.DATE);
Available Date/Time Formats
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 | 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 |
Comments
0 comments
Please sign in to leave a comment.