Java Date format compatible
If we make java.util.Date
object, basically log format is below.
Sun May 08 18:03:23 KST 2022
Our team has an old system that is more than 10 years old so we are developing for renewal. The legacy program log format likes above. But We have to make same log format in new system.
You may curious why we have to maintain the log format. There is a separate department that monitors the log. However They told me that It is difficult to modify the tool that parses the log and analyzes it.
As a result we use LocalDateTime
and DateTimeFormatter
instead of java.util.Date
. But we are keeping compatible like below code.
LocalDateTime
.of(2022,1,4,18,3,23)
.atZone(ZoneId.of("Asia/Seoul"))
.format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH));
java test code
String localDateTime = LocalDateTime
.of(2022,1,4,18,3,23)
.atZone(ZoneId.of("Asia/Seoul"))
.format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH));
Calendar cal = Calendar.getInstance();
cal.set(2022, Calendar.JANUARY, 4, 18, 3, 23);
Date date = cal.getTime();
assertThat(localDateTime).isEqualTo(date.toString());
We have to make Calendar object for fixed date test.
kotlin test code
val localDateTime = LocalDateTime
.of(2022, 1, 4, 18, 3, 23)
.atZone(Times.LOCAL_ZONE_ID)
.format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH))
val calendar = Calendar.getInstance()
calendar.set(2022, Calendar.JANUARY, 4, 18, 3, 23)
val date = calendar.time
assertThat(localDateTime).isEqualTo(date.toString())
cf) With the help of intellij, I convert the java code to kotlin and found something weired.
Intellij convert calendar set method with replace set call with indexing operator kotlin
message. But last argument is expressed =
symbol.
calendar.set(2022, Calendar.JANUARY, 4, 18, 3, 23)
// same. kotlin indexed access operator
cal[2022, Calendar.JANUARY, 4, 18, 3] = 23