Convert String to LocalDate
This script converts String to LocalDate.
This is useful e.g. as a transformation script for fields "validTill" or "validFrom" of contracts when you synchronize contracts from some CSV or DB system.
Another uses may be to convert String to LocalDateTime for EAVs or synchronization tokens.
IdM 10
Java.time libraries are less tolerant when it comes to the time format. You should specify that the original String can have a different number millisecond positions (from 0 to 6 in the given example).
Script
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.time.format.DateTimeFormatterBuilder; if (attributeValue == null) { return null; } DateTimeFormatter dateFormat = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd HH:mm:ss") .appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true) .toFormatter(); LocalDate localDate = LocalDate.parse(attributeValue, dateFormat); return localDate;
Script authorities
Service
none
Class
java.time.temporal.ChronoField java.time.format.DateTimeFormatterBuilder
Note
If you are 100 % sure that the source system returns zero or fixed number of milliseconds to IdM, you can use the following line (the example is valid for 3 numbers of milliseconds) instead of using DateTimeFormatterBuilder in the script above.
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
E.g. When reading data from a PostgreSQL database and a column "timestamp without timezone", the value is returned as String with trimmed trailing 0, so you wouldn't get fixed number of milliseconds. In such case, always use the DateTimeFormatterBuilder.
LocalDateTime may be obtained in a similar way:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.time.format.DateTimeFormatterBuilder; if (attributeValue == null) { return null; } DateTimeFormatter dateFormat = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd HH:mm:ss") .appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true) .toFormatter(); LocalDateTime localDateTime = LocalDateTime.parse(attributeValue, dateFormat); return localDateTime;
IdM 9
Script
import org.joda.time.format.DateTimeFormatter; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; if (!attributeValue) { return null; } DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy"); DateTime dt = formatter.parseDateTime(attributeValue as String); return dt;
Script authorities
Service
none
Class
org.joda.time.format.DateTimeFormatter org.joda.time.format.DateTimeFormat org.joda.time.DateTime