Guilherme Alves
Guilherme Alves

Reputation: 33

ZonedDateTime.ofInstant not Showing seconds

I'm trying to obtain the time using ZonedDateTime.ofInstant giving a specific date-time, however i've noticed that in some ocasions the result given doesn't come with seconds in it. Code Example representing the situation i'm working on

import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
  
        
        // create a long variable for milliseconds
        //this is the same as "2022-01-31T00:00:00.00Z"
        long milliseconds1  = 1643587200000L;
        long milliseconds2  = 1643587201000L;
            

        // get Instant using ofEpochMilli() method
        Instant instant1
            = Instant.ofEpochMilli(milliseconds1); 
        // create Instant object
        Instant instant2
            = Instant.ofEpochMilli(milliseconds2);
  
        // create a ZonID
        ZoneId zone = ZoneId.of("Europe/Lisbon");
  
        // apply ofInstant method
        // of ZonedDateTime class
        ZonedDateTime zt1 = ZonedDateTime
                               .ofInstant(instant1, zone);
        ZonedDateTime zt2 = ZonedDateTime
                                .ofInstant(instant2, zone);
  
        // print the result
        System.out.println("ZonedDateTime is " + zt1);
        System.out.println("ZonedDateTime is " + zt2);
    }
}

The result of this code execution is as follows:

ZonedDateTime is 2022-01-31T00:00Z[Europe/Lisbon]
ZonedDateTime is 2022-01-31T00:00:01Z[Europe/Lisbon]

I need to get the seconds that are not showing in the first result but i can't understand why it's not showing Is there a reason for ZonedDateTime.ofInstant to not give the full time, or is there a different way to get the time to a specific time zone in YYYY-MM-DD:HH:MM:SS?

Upvotes: 2

Views: 983

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500893

You're just using the default toString() implementation, which in turn uses the default LocalDateTime.toString() implementation, which is documented as:

The output will be one of the following ISO-8601 formats:

  • uuuu-MM-dd'T'HH:mm
  • uuuu-MM-dd'T'HH:mm:ss
  • uuuu-MM-dd'T'HH:mm:ss.SSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.

If you want a non-default format, which it sounds like you do, you need to use a DateTimeFormatter. I suspect that DateTimeFormatter.ISO_ZONED_DATE_TIME does what you want - but if it doesn't, just build one that has the exact format you want.

Sample code:

import java.time.*;
import java.time.format.*;

public class Test {
    public static void main(String[] args) {
        Instant instant = Instant.ofEpochMilli(1643587200000L);
        ZoneId zone = ZoneId.of("Europe/Lisbon");
        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zone);
        DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
        System.out.println(formatter.format(zdt));
    }
}

Output:

2022-01-31T00:00:00Z[Europe/Lisbon]

Upvotes: 5

Related Questions