-1

I am having difficulty in finding the current time in some pattern with JAVA 8 time api. I am trying below code for this, my goal is to find time in this pattern only -> yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


LocalDate localDate = LocalDate.now();
String localDateString = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
System.out.println(localDateString);

I am getting below issue :


Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
    at java.time.LocalDate.get0(LocalDate.java:680)
    at java.time.LocalDate.getLong(LocalDate.java:659)
    at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
    at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2551)
    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)
    at java.time.LocalDate.format(LocalDate.java:1691)

Can anyone please help me on this ?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Does this answer your question? [Format a date using the new date time API](https://stackoverflow.com/questions/23069370/format-a-date-using-the-new-date-time-api) – Ole V.V. Jun 06 '22 at 13:55
  • 2
    Don’t hardcode `Z` as a literal in your format pattern string. It’s an offset (of 0) from UTC and needs to be formatted as such. For example `OffsetDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm.ss.SSSX"))`, just gave `2022-06-06T13:58.58.028Z`. Or simpler if you can live with not predicting the number of decimals: `Instant.now()` -> `2022-06-06T13:58:58.044896Z`. – Ole V.V. Jun 06 '22 at 14:00
  • What exactly is your goal? What input, what output? Your Question is confused. – Basil Bourque Jun 06 '22 at 15:32
  • Do you want the UTC time or the time in your own time zone? The format you give is [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601), and according to it the fraction of second is optional and can have any number of decimals. Do you need exactly three decimals of fraction on the seconds, or is any ISO 8601 format OK? – Ole V.V. Jun 07 '22 at 19:14

3 Answers3

2

Instant.now().toString()

Your Question is confused. But I will guess that your goal is getting the current moment as seen in UTC. If so, you are working too hard.

Instant instant = Instant.now() ;

Create text in standard ISO 8601 format.

String output = instant.toString() ;

2022-01-06T12:34:56.123456Z

The Z on the end means +00:00, an offset from UTC of zero hours-minutes-seconds. Pronounced “Zulu”.

Parse text in standard ISO 8601 format.

Instant instant = Instant.parse( "2022-01-06T12:34:56.123456Z" ) ;

As you can see in the example code, there is no need to specify a formatting pattern. The java.time classes use ISO 8601 formats by default when parsing/generating text.

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

As @Chaosfire mentioned LocalDate instances do not contain any time part. Thus it can convert itself to a pattern that has time in it.
You either use LocalDateTime to get the exact time or if you are not bothered about time i.e it can be 00:00:00 for each instance then you can use LocalDate.atStartOfDay().

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDate localDate = LocalDate.now();
String localDateString = localDate.atStartOfDay(ZoneOffset.UTC).format(formatter);
//Output: 2022-06-06T00:00:00.000
Sayan Bhattacharya
  • 1,365
  • 1
  • 4
  • 14
  • 2
    If you want `Z` at the end of the output (which the question seems to indicate), append `X` to the format pattern string as in `DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")`. Output: `2022-06-06T00:00:00.000Z`. – Ole V.V. Jun 06 '22 at 14:17
0

You are trying to format a date like a date-time. A date obviously does not have hour of day, minute, etc. Are you perhaps trying to use LocalDateTime?

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

public class Temp {

  public static void main(String[] args) throws Exception {
    LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
    String localDateString = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
    System.out.println(localDateString);
  }
}
Chaosfire
  • 4,818
  • 4
  • 8
  • 23
  • 3
    Well on the right way. Hardcoding `Z` as a literal in the format pattern string — which you took over from the question — is wrong. `Z` (pronounced *Zulu*) means UTC or offset zero from UTC. So if your JVM’s default time zone is something else than UTC, you will get a wrong result. This in turn means that to obtain the UTC time you can’t use the no-arg `LocalDateTime.now()`. I recommend `OffsetDateTime.now(ZoneOffset.UTC)`. – Ole V.V. Jun 06 '22 at 14:04
  • @OleV.V. I am not sure why, but even if i use the pattern from question, i still get correct local time. And my time zone(system default as well) is not UTC. – Chaosfire Jun 06 '22 at 14:24
  • 2
    If the UTC time is 14:27 and your local time is 17:27, for example, then printing `17:27:00.000Z` is wrong because it is a claim that the time is 17:27 in UTC. – Ole V.V. Jun 06 '22 at 14:28