Output Date in Java in ISO 8601 extended format

Introduction

When I was making a RESTful API with Spring Boot, I wanted to format the Date type of the response to ISO 8601 extended format and return it. It took a long time to find the format, so I will leave it as a memorandum.

format

It can be output with yyyy-MM-dd'T'HH: mm: ssXXX.

Test.java


import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
        public static void main(String[] args) {
                Date d = new Date();
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
                System.out.println(sf.format(d));
        }
}
2018-12-19T15:46:06+09:00

Used as Spring Boot response

If you are using Spring Boot, you should be using ** Jackson ** as the JSON conversion of the response. You can specify the default format by adding the following settings to ʻapplication.yml`.

application.yml


spring:
  jackson:
      date-format: "yyyy-MM-dd'T'HH:mm:ssXXX"
      time-zone: "Asia/Tokyo"

Jackson will be ** GMT if you don't specify a timezone **. Basically, I think it's better to specify the time zone.

If you want to convert the format explicitly instead of the default format, write as follows.

Response.java


import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;

public class Response {
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX", timezone = "Asia/Tokyo")
    private Date createdAt;

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }
}

At the end

The format is slightly different for each language, so it's surprisingly addictive. Isn't it all unified?

Recommended Posts

Output Date in Java in ISO 8601 extended format
Format XML in Java
Date manipulation in Java 8
View current date in Java
Date processing in Java (LocalDate: Initialization)
Log output to file in Java
Studying Java 8 (date API in java.time package)
Output Spring Boot log in json format
Output Notes document as XML document in Java
Try using JSON format API in Java
How to get the date in java
Java that outputs vmg format file in eml format
[Java] output, variables
google java format
Create a CSR with extended information in Java
Partization in Java
Try scraping about 30 lines in Java (CSV output)
Changes in Java 11
Rock-paper-scissors in Java
I tried to output multiplication table in Java
Pi in Java
[Java] Something is displayed as "-0.0" in the output
FizzBuzz in Java
Compare PDF output in Java for snapshot testing
Format of the log output by Tomcat itself in Tomcat 8
How to input / output IBM mainframe files in Java?
[Java] Things to note about type inference extended in Java 10
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
I tried using an extended for statement in Java
Gzip-compress byte array in Java and output to file
About Java log output
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
[Java] Date / time operations
[Java] for statement / extended for statement
Comments in Java source
Azure functions in java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Java Gold Countermeasures: Format
POST JSON in Java
Express failure in Java
Output triangle in Ruby