How can I format this value in java localdatetime?

816 views Asked by At
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
1

There are 1 answers

0
Arvind Kumar Avinash On

Creating the parser for the first date-time string is straightforward. In order to parse the second date-time string, you can create DateTimeFormatter with the default year as the current year.

Note that your expected output has timezone information and therefore they are ZonedDateTime, not LocalDateTime.

Demo:

import java.time.Year;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("EEEE, dd MMM uuuu HH:mm:ss '('VV')'", Locale.ENGLISH);
        DateTimeFormatter dtf2 = new DateTimeFormatterBuilder()
                .parseDefaulting(ChronoField.YEAR, Year.now().getValue())
                .appendPattern("MM/dd '('EEE')', HH:mm VV")
                .toFormatter(Locale.ENGLISH);

        ZonedDateTime zdt1 = ZonedDateTime.parse("Wednesday, 30 Nov 2022 10:30:00 (UTC)", dtf1);
        ZonedDateTime zdt2 = ZonedDateTime.parse("12/08 (Thu), 10:00 GMT", dtf2);

        System.out.println(zdt1);
        System.out.println(zdt2);

        // Formatted output
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss z", Locale.ENGLISH);
        System.out.println(zdt1.format(formatter));
        System.out.println(zdt2.format(formatter));
    }
}

Output:

2022-11-30T10:30Z[UTC]
2022-12-08T10:00Z[GMT]
2022/11/30 10:30:00 UTC
2022/12/08 10:00:00 GMT

ONLINE DEMO

Combined parser

You can combine both patterns to create a single DateTimeFormatter as follows:

DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                .optionalStart()
                .appendPattern("EEEE, dd MMM uuuu HH:mm:ss '('VV')'")
                .optionalEnd()
                .optionalStart()
                .parseDefaulting(ChronoField.YEAR, Year.now().getValue())
                .appendPattern("MM/dd '('EEE')', HH:mm VV")
                .optionalEnd()
                .toFormatter(Locale.ENGLISH);

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.