79092025

Date: 2024-10-15 23:31:23
Score: 0.5
Natty:
Report link

Your first solution is not working because the date time formatted is case sensitive so FEB is not accepted but Feb would work the second solution is not working because you have added a lot of unnecessary configuration in the case the expected string should be something like 01-FEB-25 12.00.00.000000 AM UTC.000000 AM UTC here is an example of working code:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        String input = "01-FEB-25 12.00.00.000000 AM UTC";
        System.out.println(convertDateTime(input));
    }

    private static String convertDateTime(String dateTimeInput) {

            try {
                DateTimeFormatter inputFormatter = new DateTimeFormatterBuilder()
                        .parseCaseInsensitive()
                        .appendPattern("dd-MMM-yy hh.mm.ss.SSSSSS a z")
                        .toFormatter(Locale.ENGLISH);
                DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

                // Parse the input string into ZonedDateTime
                ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeInput, inputFormatter);

                // Convert to LocalDateTime in UTC
                LocalDateTime dateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();

                return dateTime.format(outputFormatter);
            } catch (DateTimeParseException e) {
                e.printStackTrace();
                return null;
            }
    }
}
Reasons:
  • Blacklisted phrase (1): solution is not working
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: khalifa R.