0
    public static String formatter(String dateInPattern, String dateOutPattern) {
    OffsetDateTime dateInPatternFormat = OffsetDateTime.parse(dateInPattern, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"));
    Date dateInValue = DateTimeUtils.toDate(Instant.parse(dateInPatternFormat.toString()));
    OffsetDateTime dateOutPatternFormat = OffsetDateTime.parse(dateOutPattern);

    return dateOutPatternFormat.format(DateTimeFormatter.ofPattern(dateInValue.toString()));

}

I need to enter a date in this pattern yyyy-MM-dd'T'HH:mm:ss'Z' this is equals (2018-07-22T14:00:00-03:00). And I need an output in this pattern dd/MM/yyyy

Helpe me please.

I have had many problems with date on android :(

  • While the accepted answer helped understanding the big picture, let's also explain the error in title. `Instant` is always in UTC, a parsed string must follow the pattern `yyyy-MM-dd'T'HH:mm:ss.sssZ`. You supplied a string with a time offset so parsing failed. The same input string could be correctly parsed as `OffsetDateTime`. – Eugen Pechanec Sep 27 '18 at 22:58
  • I’m sorry, I don’t get exactly what you are trying to do. Please be specific about which two arguments you are passing to your `formatter` method and what return value you require, using examples. – Ole V.V. Sep 28 '18 at 10:21
  • When you are using ThreeTenABP (or ThreeTen Backport), avoid the outdated and poorly designed `Date` class completely. ThreeTenABP has all the functionality you need, so conversion to `Date` just means extra and needless complication. – Ole V.V. Sep 28 '18 at 10:22
  • What I needed was to read a date that is a String yyyy-MM-dd'T'HH: mm: ss'Z 'and be able to output it to be a dd/MM/yyyy – Alan Bastos Sep 28 '18 at 19:56

1 Answers1

4

Your code is very confusing, with weird names and you seem to be mixing up pattern strings, e.g. yyyy-MM-dd, with value strings, e.g. 2018-07-22.

The value string 2018-07-22T14:00:00-03:00 can be parsed into an OffsetDateTime without specifying a DateTimeFormatter, since that is the default format of an OffsetDateTime.

If you then need to format that as dd/MM/yyyy, then use a DateTimeFormatter.

Don't know why your method takes 2 parameters.

Example:

String input = "2018-07-22T14:00:00-03:00";
OffsetDateTime offsetDateTime = OffsetDateTime.parse(input);
String output = offsetDateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(output); // prints: 22/07/2018
Andreas
  • 154,647
  • 11
  • 152
  • 247