Wednesday, 30 Nov 2022 10:30:00 (UTC)
12/08 (Thu), 10:00 GMT
Each item was extracted using regex from the data uploaded in this way.
With the selected items, we want to construct a localDatetime in Java.
Below is the code to configure it.
@Override
public List<LocalDateTime> getTime(String str) {
Matcher matcher = regex("(\\d+)\\/(\\d+)\\(\\W\\), \\d+:\\d+-(\\d+):(\\d+)\\s+(\\w+)", str);
LocalDateTime date;
List<LocalDateTime> list = new ArrayList<>();
int year, month, day, hour, minute, second;
ZoneId timezone;
year = LocalDateTime.now().getYear();
while(matcher.find()){
month = getMonth(matcher.group(1));
day = Integer.parseInt(matcher.group(2));
hour = Integer.parseInt(matcher.group(3));
minute = Integer.parseInt(matcher.group(4));
second = 0;
if(matcher.group(5).equals("KST")) {
timezone = ZoneId.of("Asia/Seoul");
}
else{
timezone = ZoneId.of(matcher.group(5));
}
date = LocalDateTime.of(year, month, day, hour, minute, second);
ZonedDateTime time = ZonedDateTime.of(date, timezone);
LocalDateTime result = timeZoneConverter(time);
list.add(result);
}
return list;
}
public static Matcher regex(String regex, String input) {
return Pattern.compile(regex, Pattern.MULTILINE).matcher(input);
}
public static int getMonth(String s) {
int result = 0;
switch (s) {
case "1":
case "Jan":
result = 1;
case "Feb":
case "2":
result = 2;
case "Mar":
case "3":
result = 3;
case "Apr":
case "4":
result = 4;
case "May":
case "5":
result = 5;
case "Jun":
case "6":
result = 6;
case "Jul":
case "7":
result = 7;
case "Aug":
case "8":
result = 8;
case "Sep":
case "9":
result = 9;
case "Oct":
case "10":
result = 10;
case "Nov":
case "11":
result = 11;
case "Dec":
case "12":
result = 12;
}
return result;
}
public static LocalDateTime timeZoneConverter(ZonedDateTime dateTime) {
ZonedDateTime resultZonedDateTime = dateTime.withZoneSameInstant(ZoneId.of("Asia/Seoul"));
LocalDateTime localDate = LocalDateTime.parse(resultZonedDateTime.toLocalDateTime().toString());
System.out.println("localDate = " + localDate);
return localDate;
}
return is required java LocalDateTime...
If the content of the article is insufficient, I will edit it further.
I googled for hours, but couldn't get around.
The result that i want is below.
2022/11/30 10:30:00 UTC
2022/12/08 10:00:00 GMT
List<DateTimeFormatter>. Then try parsing your input with each one in turn, until one succeeds and gives you aZonedDateTime.EEEE, dd MMM yyyy, HH:mm:ss (z),MM/dd (EE), HH:mm z,yyyy/MM/dd HH:mm:ss z(last two).OffsetDateTimefor them (or evenZonedDateTimeif you want to distinguish between GMT and UTC). NotLocalDateTime.