0

Having date as string: "2021-09-11T12:02:50-06:00Z". Want to convert to java.util.Date using apache DateUtils:

    public static Date toDate (String dateString) throws ParseException {
        String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'TZD''Z'";
        return DateUtils.parseDate(dateString, new String[]{DATETIME_FORMAT});
    }

giving below exception:

java.text.ParseException: Unable to parse the date: 2021-09-11T12:02:50-06:00Z
    at org.apache.commons.lang3.time.DateUtils.parseDateWithLeniency(DateUtils.java:388)
    at org.apache.commons.lang3.time.DateUtils.parseDate(DateUtils.java:302)
    at org.apache.commons.lang3.time.DateUtils.parseDate(DateUtils.java:279)

tried DATETIME_FORMAT as "yyyy-MM-dd'T'HH:mm:ss'TZD''Z'" , "yyyy-MM-dd'T'HH:mm:ss'TZD''Z'", "YYYY-MM-DD'T'hh:mm:ss'TZD'"

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    why add `'TZD'` and `'Z'` to format string? Is that something from `DateUtils`? (I only know/use standard Java for date/time manipulation and `''` means a literal text there) – user16320675 Nov 24 '21 at 07:29
  • 1
    Maybe you shouldn't use 'Z' in both example and format? – CoolMind Nov 24 '21 at 07:30
  • 2
    `ZonedDateTime dt = ZonedDateTime.parse("2021-09-11T12:02:50-06:00Z", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz'Z'"));` will work, but I'm guessing there is a reason why you're still using an out-dated, effectively deprecated, API – MadProgrammer Nov 24 '21 at 07:35
  • I recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). It will also parse your string without any format pattern: `Instant.parse("2021-09-11T12:02:50-06:00Z")`. – Ole V.V. Nov 24 '21 at 08:41
  • @user16320675 I don’t think it“s `'TZD'` followed by `'Z'`. Since a double single quote denotes a literal single quote, it’s the literal string `TZD'Z`. – Ole V.V. Nov 24 '21 at 08:45
  • You are right. @user16320675, my comment was not important since, as you say, it doesn’t change the problem that you pointed out. – Ole V.V. Nov 24 '21 at 09:09
  • What is the expected result of parsing? There is indeed a contradiction in your string. `-06:00` means 6 hours behind UTC. `Z` means UTC, or 0 hours behind or ahead of UTC. Which if them do you want? – Ole V.V. Nov 24 '21 at 09:42
  • Or in an attempt to say it more clearly:: You should not try to parse that string. You should go back to the folks who gave it to you and ask them to give you a string with only one UTC offset in it so the self contradiction is eliminated and so the string adheres to [The ISO 8601 standard](https://en.wikipedia.org/wiki/ISO_8601), which is what it is obviously trying to do. – Ole V.V. Nov 24 '21 at 10:03
  • I have voted to close because you have not told us your expected result. There is no reasonable way to parse your self-contradictory string and hence no reasonable answer to give. – Ole V.V. Nov 25 '21 at 20:33

1 Answers1

0

Please make this correction in format and also in date string.

public static Date toDate (String dateString) throws ParseException {
        String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
        return DateUtils.parseDate(dateString, new String[]{DATETIME_FORMAT});
    }

And in input format do not use colon. Example

 System.out.println(toDate  ("2021-09-11T12:02:50-0600"));
Prabir Ghosh
  • 430
  • 3
  • 7
  • 1
    We cannot ask to get the date in the working format in the solution but we can try to alter the given date in the working format. – RAHUL VISHWAKARMA Nov 30 '21 at 18:13
  • If input format does not match with the specified date format then you can parse the input before processing with DateUtils. – Prabir Ghosh Dec 01 '21 at 05:50