Home > Software engineering >  Issue in fetching Local time with Java 8 Time API with particular pattern of date
Issue in fetching Local time with Java 8 Time API with particular pattern of date

Time:06-07

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 ?

CodePudding user response:

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

CodePudding user response:

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);
  }
}

CodePudding user response:

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.

CodePudding user response:

Below code should print time in local time.

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

  • Related