Working with dates in TDI can be tricky, since you'll normally be dealing with not only ISIM's internal format, but also various formats from different databases and other sources. The easiest way I've found is to use the java.time.* classes that were introduced in Java 8. Specifically, the LocalDateTime class is great because it has methods like minusHours(), minusDays(), etc. to make it easy to get a time from "X time ago".
Here are some examples I've come up with to use with the different formats I've run into on my latest contract:
var ldtObj = java.time.LocalDateTime.now();
var dtfObj = java.time.format. DateTimeFormatter.ofPattern(" yyyyMMdd");
var today = ldtObj.format(dtfObj);
// today is "20240830" for August 30, 2024
var dtfObjHHmm = java.time.format. DateTimeFormatter.ofPattern(" yyyyMMddHHmm");
var todayHHmm = ldtObj.format(dtfObjHHmm);
// todayHHmm is "202408301531" for 15:31 on August 30, 2024
var dtfObjH = java.time.format. DateTimeFormatter.ofPattern(" yyyy-MM-dd:HH");
var todayH = ldtObj.format(dtfObjH);
// todayH is "2024-08-30:15" for 15:00 on August 30, 2024
var dtfObjChars = java.time.format. DateTimeFormatter.ofPattern(" yyyy-MM-dd'T'HH'hrs'mm'mins'");
var todayChars = ldtObj.format(dtfObjChars);
// todayChars is "2024-08-30T15hrs31mins" for 15:31 on August 30, 2024
// just put single quotes in the Pattern around any literal characters you want in the resulting string.
var utcZone = java.time.ZoneId.of("UTC");
var ldtObjUtc = java.time.LocalDateTime.now( utcZone);
var dtfObjZ = java.time.format. DateTimeFormatter.ofPattern(" yyyyMMddHHmm'Z'");
var todayZ = ldtObjUtc.format(dtfObjZ);
// todayZ is "202408301531Z" for 15:31 on August 30, 2024 Zulu (UTC) time.
// get the time "3 hours ago"
var ldtObjThreeHoursAgo = ldtObj.minusHours(3);
var dtfObj = java.time.format. DateTimeFormatter.ofPattern(" yyyyMMddHHmm");
var today = ldtObj.format(dtfObj);
// today is "202408301231" for three hours before 15:31 August 30, 2024 (so 12:31)
No comments:
Post a Comment