Skip to main content

Fixed: "Java 8 date/time type java.time.Instant not supported by default" in JJWT

· 10 min read
Jagdish Kumawat
Founder @ Dewiride

A Java program that put a java.time.Instant into a custom JWT claim with JJWT stopped with Java 8 date/time type java.time.Instant not supported by default before it made the token. Jackson, the JSON library JJWT uses, can't write java.time values as JJWT sets it up, and JWTs carry times as seconds anyway. Sending getEpochSecond() fixes it.

What I Was Doing​

I was checking which Java values JJWT writes into a custom claim, for posts about custom claims in Java. Strings, numbers, lists, maps and records all worked. Then I passed a time, a java.time.Instant, and the program stopped with the error below.

The example below shows the same thing with nothing else in the way: a token with one custom claim, joined, the time Ada joined a site.

What I Expected​

A signed token whose joined claim holds that time.

What Happened Instead​

Create a folder named demo, with this pom.xml in it:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jjwt.version>0.13.0</jjwt.version>
</properties>

<dependencyManagement>
<dependencies>
<!-- jjwt-jackson asks for Jackson 2.12.7.1. This makes Maven use the newest Jackson 2 instead. -->
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.23.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

It lists JJWT's three parts: jjwt-api, which the code calls, and jjwt-impl and jjwt-jackson, which do the work while the program runs. jjwt-jackson writes the token's JSON with Jackson. The jackson-bom entry makes Maven use Jackson 2.23.0 instead of the older version jjwt-jackson asks for, and the exec-maven-plugin entry names com.example.Main as the class to run.

Then the program, in src/main/java/com/example/Main.java:

src/main/java/com/example/Main.java
package com.example;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;

import java.time.Instant;
import javax.crypto.SecretKey;

public class Main {

public static void main(String[] args) {
// The signing key: 32 random bytes, written as base64.
// It's in the code only for this example; a real app loads it from its configuration.
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode("C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64="));

Instant joined = Instant.parse("2026-01-02T03:04:05Z"); // when Ada joined
String token = Jwts.builder()
.subject("ada")
.claim("joined", joined)
.signWith(key)
.compact();
System.out.println(token);
}
}

It turns a base64 secret into a signing key, sets joined to 2 January 2026 at 03:04:05 UTC, and signs a token with that claim.

In the demo folder, build and run it:

Terminal
mvn -q compile exec:java
Output
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.7.0:java (default-cli) on project demo: An exception occurred while executing the Java class. Cannot serialize JWS Payload to JSON. Cause: Unable to serialize object of type io.jsonwebtoken.impl.DefaultClaims: Java 8 date/time type `java.time.Instant` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (or disable `MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_TIMES`) (through reference chain: io.jsonwebtoken.impl.DefaultClaims["joined"]) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

A zsh terminal where mvn -q compile exec fails with Cannot serialize JWS Payload to JSON, because the Java 8 date/time type java.time.Instant is not supported by default, in the claim named joined.

-q keeps Maven quiet, compile builds the program and exec:java runs it. The command is the same in zsh, bash and PowerShell. The program compiled, then failed while it turned the claims into JSON, before it printed anything.

What I Checked​

  • The message. It comes in two layers. JJWT's own says what it was doing: "Cannot serialize JWS Payload to JSON." Jackson's says why: "Java 8 date/time type java.time.Instant not supported by default". The reference chain at the end, DefaultClaims["joined"], names the claim.
  • Other java.time types. I ran the same program with a LocalDate, a LocalDateTime, an OffsetDateTime, a ZonedDateTime and a Duration. Each one failed the same way, with its own type in the message.
  • The JJWT README. Its Jackson JSON Processor section says that with jjwt-jackson on the classpath, "JJWT will automatically create a new Jackson ObjectMapper for its needs as required." That ObjectMapper wrote this payload, and the error shows that it can't write java.time values.
  • The JJWT issues. jwtk/jjwt#235, "Java8 Date/Time formats", asks for java.time support in claims. It has been open since July 2017, and JJWT 0.13.0 still doesn't have it.
  • RFC 7519. JWT's own time claims, exp, nbf and iat, hold a NumericDate: "A JSON numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds." A custom claim doesn't have to follow it, but a time in the same form is read the same way by every library.
  • A java.util.Date instead. JJWT accepts one in a custom claim, but it came out in milliseconds while iat in the same token was in seconds: {"sub":"ada","iat":1767323045,"joined":1767323045000}. JJWT read both back as the same time. Read as seconds, the way iat is read, 1767323045000 is +57974-03-28T20:03:20Z.

So JJWT 0.13.0 can't write a java.time value into a claim, and the form JWTs use for a time is a number of seconds anyway.

The Fix​

Send the time as whole seconds since 1970, with Instant.getEpochSecond(). When you read the token, turn the seconds back into an Instant with Instant.ofEpochSecond:

Main.java that puts joined.getEpochSecond() into the joined claim, prints the token, then verifies the token and turns the claim back into an Instant with Instant.ofEpochSecond.

src/main/java/com/example/Main.java
package com.example;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;

import java.time.Instant;
import javax.crypto.SecretKey;

public class Main {

public static void main(String[] args) {
// The signing key: 32 random bytes, written as base64.
// It's in the code only for this example; a real app loads it from its configuration.
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode("C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64="));

Instant joined = Instant.parse("2026-01-02T03:04:05Z"); // when Ada joined
String token = Jwts.builder()
.subject("ada")
// JWTs carry times as whole seconds since 1970, so send the seconds.
.claim("joined", joined.getEpochSecond())
.signWith(key)
.compact();
System.out.println(token);

Claims claims = Jwts.parser().verifyWith(key).build().parseSignedClaims(token).getPayload();
System.out.println(Instant.ofEpochSecond(claims.get("joined", Long.class)));
}
}
  • joined.getEpochSecond() returns a long. Its Java documentation: "Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z." Jackson writes a long as a plain JSON number.
  • claims.get("joined", Long.class) reads the claim as a Long. Its JJWT documentation says it converts only "simple String, Date, Long, Integer, Short and Byte types automatically", so Long.class works, and Instant.class doesn't.
  • Instant.ofEpochSecond does the opposite of getEpochSecond: it "Obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z."

The Result​

Run it again:

Terminal
mvn -q compile exec:java
Output
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZGEiLCJqb2luZWQiOjE3NjczMjMwNDV9.dkaRDzsKy7adLbl4Qut0J350jILE8UhCurwgcDsaINI
2026-01-02T03:04:05Z

A zsh terminal where mvn -q compile exec prints a signed JWT, then the joined time read back from it, 2026-01-02T03:04:05Z.

The token's payload is {"sub":"ada","joined":1767323045}, and the time read back from it is the time the program put in.

Common Mistakes​

MistakeWhat happensDo this instead
An Instant, LocalDateTime or any other java.time value in claimJava 8 date/time type ... not supported by default, and no tokenSend getEpochSecond()
A java.util.Date in a custom claim, to make the error go awayJJWT writes it in milliseconds, unlike iat and expSend seconds
claims.get("joined", Instant.class)RequiredTypeException, because JJWT doesn't convert to InstantRead a Long, then use Instant.ofEpochSecond

Troubleshooting​

SymptomLikely causeFix
Java 8 date/time type java.time.Instant not supported by default, or the same with LocalDate, LocalDateTime, OffsetDateTime, ZonedDateTime or DurationA java.time value in a claimSend getEpochSecond()
io.jsonwebtoken.RequiredTypeException: Cannot convert existing claim value of type 'class java.lang.Integer' to desired type 'class java.time.Instant'.claims.get with Instant.classUse Long.class, then Instant.ofEpochSecond
java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because the return value of "io.jsonwebtoken.Claims.get(String, java.lang.Class)" is nullThe token has no joined claimCheck the value for null before Instant.ofEpochSecond
A time tens of thousands of years away, such as +57974-03-28T20:03:20ZThe claim holds milliseconds, from a java.util.Date, and was read as secondsSend seconds

FAQ​

Can I add the module the message names instead?​

The README's Jackson JSON Processor section shows how to give JJWT your own Jackson ObjectMapper: declare jjwt-jackson with compile scope, and pass new JacksonSerializer(objectMapper) to the builder's json method. That's where a module such as jackson-datatype-jsr310 would be registered. I didn't take that route. Reading the claim back would need work of its own, because Claims.get doesn't convert to java.time types, and seconds are what JWTs use for times.

Why do exp and iat take a Date without this problem?​

JJWT's expiration, notBefore and issuedAt methods take a java.util.Date and write a NumericDate in seconds, as RFC 7519 asks. A custom claim is only a name to JJWT, and a Date in one came out in milliseconds.

Will JJWT support Instant in claims?​

jwtk/jjwt#235 asks for it, and it's still open in JJWT 0.13.0. Until a release supports it, send seconds.

Conclusion​

JJWT 0.13.0 writes claims with Jackson, and the Jackson it sets up can't write java.time values, so an Instant, a LocalDateTime or any other java.time value in a claim stops the token from being made. Send getEpochSecond() instead: seconds since 1970 are what JWTs use for every time, and Instant.ofEpochSecond turns them back into an Instant.

Additional Resources​