Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

Java 8 fix for Exception when using LocalDateTime.now() format with zone Z - java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds

0 contributions
13 points

1. Problem description

I would like to format date time now from LocalDateTime.now() with formatter pattern 

yyyy-MM-dd HH:mm Z

Full code which throws UnsupportedTemporalTypeException: 

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

public class DateTimeFormatter_UnsupportedTemporalTypeException {

    public static void main(String[] args) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm Z");
        String format = fmt.format(LocalDateTime.now());
        System.out.println(format);
    }
}

When I run this code I get this stacktrace:

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
	at java.time.LocalDate.get0(LocalDate.java:680)
	at java.time.LocalDate.getLong(LocalDate.java:659)
	at java.time.LocalDateTime.getLong(LocalDateTime.java:720)
	at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
	at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3346)
	at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2190)
	at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
	at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)

2. Solution

// to solve this exception use:
ZonedDateTime.now()

// eg:
String format = fmt.format(ZonedDateTime.now());

// instead of using:
LocalDateTime.now()

Full working code example:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatter_UnsupportedTemporalTypeException_Solution {

    public static void main(String[] args) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm Z");
        String format = fmt.format(ZonedDateTime.now());
        System.out.println(format); // 2019-10-04 22:55 +0200
    }
}

Output:

2019-10-04 22:55 +0200

References

  1. LocalDateTime.now() - Java docs
  2. UnsupportedTemporalTypeException - Java docs
  3. ZonedDateTime.now() - Java docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more