-3

1) The date format I'm getting from API is yyyy-MM-dd'T'HH:mm:ss.SSSXXX

2) I want to give back a Json list with dates to frontend in different format like: dd MM HH:mm

Is there any annotations or properties to remap dates in 2) example as in 1) it's easily achievable with @JsonFormat.

Boken
  • 4,825
  • 10
  • 32
  • 42

2 Answers2

2

Offset-from-UTC

The XXX in your formatting pattern refers to an offset-from-UTC. This means the date and time-of-day represent a moment that is a number of hours-minutes-seconds ahead of, or behind, the baseline of UTC.

OffsetDateTime

So parse that input with OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.parse( "2019-01-23T12:34:56.123+05:30" ) ;

Do not be mislead into using the LocalDateTime class for such inputs. This class purposely lacks any concept of time zone or offset-from-UTC. So this class cannot be used to represent a moment. Parsing your inputs with this class is recklessly discarding valuable information. See: What's the difference between Instant and LocalDateTime?

Table of types of date-time class in modern java.time versus legacy

DateTimeFormatter

To generate strings representing this value in standard ISO 8601 format, merely call toString.

String output = odt.toString() ;  // Generate string in standard ISO 8601 format.

To generate strings in other formats, use the DateTimeFormatter class. You can specify a custom format, or better, let the class automatically localize for you.

To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine:
    • The human language for translation of name of day, name of month, and such.
    • The cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Example:

Locale l = Locale.CANADA_FRENCH ;   // Or Locale.US, Locale.JAPAN, etc.
DateTimeFormatter f = 
    DateTimeFormatter
    .ofLocalizedDateTime( FormatStyle.FULL )
    .withLocale( l )
;
String output = zdt.format( f );

Search Stack Overflow for more info, as DateTimeFormatter has already been covered many many times.

ISO 8601

Your input strings happen to be in standard ISO 8601 format.

The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

You can add some logic in getter (e.g. getDate()) like there:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String inputDate = "2014-07-04T12:08:56.235-07:00";
        String inputFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

        String outputFormat = "dd MM HH:mm";

        // Input formatter
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputFormat);

        // Time after formatting
        LocalDateTime dateTime = LocalDateTime.parse(inputDate, inputFormatter);

        // Output formatter
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormat);

        // Output date as String
        String outputDate = dateTime.format(outputFormatter);

        System.out.println(outputDate);
    }
}
Boken
  • 4,825
  • 10
  • 32
  • 42
  • 1
    **Incorrect.** `LocalDateTime` is the wrong class to be using here. That class purposely lacks any concept of time zone or offset-from-UTC. Yet the input carries an offset-from-UTC. Your code here is needlessly discarding valuable information. – Basil Bourque Mar 15 '19 at 19:18