-2

If I give the input as any date format.I want to convert it into YYYY-MM-DD HH:MM:SS

I wrote the code as:

The input format will be any of them below:

DateTimeFormatter inputformats=DateTimeFormatter.ofPattern("[dd-MMM-uuuu hh:mm]","[dd-MMM-uuuu hh:mm:ss.s]","[uuuu-MM-dd hh:mm:ss]","[dd-MM-uuuu hh:mm:ss]""[uuuu-MM-dd]").withResolverStyle(ResolverStyle.STRICT);

    
try {
    LocalDateTime localDate = LocalDateTime.parse(value, inputformats);
    value = localDateTime.format(outputformat);
    return value;

} catch (DateTimeParseException e) {
    log.error(e);
    return null;
}

And I give the input as:"26-08-1990 10:12:11". But I am getting the "could not be parsed: unable to obtain localDateTime from temporalaccessor" this error

User74
  • 11
  • 4
  • 1
    Why did you code your `ofPattern` like you did? and which pattern do you think it should match? – Scary Wombat Apr 27 '21 at 08:08
  • Please don't indent paragraphs. It messes up the formatting. – Federico klez Culloca Apr 27 '21 at 08:10
  • Btw, none of your patterns match the date you try to parse. You've given a `dd-MM-yyyy` (or `-uuuu`) but any similar pattern has a `MMM` which indicates the month should be given as text, i.e. `Aug` etc. – Thomas Apr 27 '21 at 08:13
  • 1
    Adding to Wombat's question: where does this `ofPattern()` method come from? [`java.time.format.DateTimeFormatter`](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/time/format/DateTimeFormatter.html) doesn't have a `ofPattern()` that takes multiple patterns. – Thomas Apr 27 '21 at 08:16
  • If I give the input that matches any of these given formats (inputformats).then it should convert into "YYYY-MM-DD HH:MM:SS".@ScaryWombat – User74 Apr 27 '21 at 08:17
  • What you've posted can also not be your actual or complete code, e.g. `localDateTime` and `outputformat` are nowhere defined. In addition to that, do you realize that `YYYY-MM-DD HH:MM:SS` is not the pattern you expect? `YYYY` is the week-based year, `DD` is the day of year, `MM` is the month (you don't want it in your time portion) and `SS` is the fraction of a second (normally milliseconds). – Thomas Apr 27 '21 at 08:22
  • @User74 *If I give the input that matches any of these given formats* - well it doesn't – Scary Wombat Apr 27 '21 at 08:27

1 Answers1

-1
    public static void main(String[] args) {
        DateTimeFormatter inputformats = DateTimeFormatter.ofPattern(""
                + "[uuuu/MM/dd HH:mm:ss.SSSSSS]"
                + "[uuuu-MM-dd HH:mm:ss[.SSS]]"
                + "[dd-MM-uuuu HH:mm:ss]"
        ).withResolverStyle(ResolverStyle.STRICT);

        String value = "26-08-1990 10:12:11";
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(value, inputformats);
            value = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            System.out.println(value);

        } catch (DateTimeParseException e) {
            System.out.println(e);
        }
    }

Where dd-MM-uuuu HH:mm:ss sample input pattern and yyyy-MM-dd HH:mm:ss is expected output pattern.

VaibS
  • 1,627
  • 1
  • 15
  • 21
  • 1
    I tried this one.But am getting "unable to obtain LocalDate from TemporalAccessor{YearofEra=1990,DayOfMonth=26, MonthofYear=28},Iso resolved to 10:12:11 of type java.time.format.parsed".I want to use YearofEra(uuuu) instead of (YYYY) and used DateTimeFormatter with withResolverStyle(ResolverStyle.Strict) – User74 Apr 27 '21 at 09:19
  • Updated the answer and it is working for me. – VaibS Apr 27 '21 at 10:02
  • @User74 Did it work for you? If yes, please remove the downvote on ans and accept it. – VaibS Apr 28 '21 at 11:42