-2

I have the date 2023-04-03 and wanted to convert it into UTC time with range (start and end of the day)

2023-04-03T00:00:00 -> 2023-04-02T18:30:00z
2023-04-03T24:00:00 -> 2023-04-03T18:30:00z

Input: 2023-04-03
Need this output: 2023-04-02T06:30:00z, 2023-04-03T18:30:00z

I have tried LocalDate and SimpleDateFormatter, but they are not converting to this way.

LocalDateTime ldt = LocalDate.parse("2023-04-03").atStartOfDay();
ZonedDateTime zdt = ldt.atZone(ZoneId.of("UTC"));
Instant instant = zdt.toInstant();
System.out.println(":inst: " + instant.toString());

output of the above code:

Hello: 2016-06-12T00:00

:inst: 2016-06-12T00:00:00Z

2
  • 1
    I suppose that 2023-04-02T06:30:00z is a typo on your part and you want 6:30 PM AKA 18:30. Can you confirm or deny, please? Commented Apr 3, 2023 at 17:18
  • Though not the same you may find this question and its answers helpful: Start and End of Week w/ ThreeTenBackport Commented Apr 3, 2023 at 19:15

3 Answers 3

2

I have tried LocalDate and SimpleDateFormatter, but they are not converting to this way.

Stop using the error-prone legacy date-time API

The java.util date-time API and their corresponding parsing/formatting type, SimpleDateFormat are outdated and error-prone. In March 2014, the modern Date-Time API was released as part of the Java 8 standard library which supplanted the legacy date-time API and since then it is strongly recommended to switch to java.time, the modern date-time API.

You need a time zone to get the desired output

Your desired output (start and end of the day) is in UTC and therefore you need to convert the date-time from your time zone to UTC. Once you have start and end of the day in your time zone, you can convert them to UTC using ZonedDateTime#withZoneSameInstant.

Demo:

class Main {
    public static void main(String[] args) {
        ZoneId zoneIndia = ZoneId.of("Asia/Kolkata");
        LocalDate date = LocalDate.parse("2023-04-03");

        System.out.println(date.atStartOfDay(zoneIndia)
                .withZoneSameInstant(ZoneOffset.UTC));

        System.out.println(date.atStartOfDay(zoneIndia).with(LocalTime.MAX)
                .withZoneSameInstant(ZoneOffset.UTC));

        // Or get the exlusive upper range (half-open interval for the day)
        System.out.println(date.plusDays(1).atStartOfDay(zoneIndia)
                .withZoneSameInstant(ZoneOffset.UTC));
    }
}

Output:

2023-04-02T18:30Z
2023-04-03T18:29:59.999999999Z
2023-04-03T18:30Z

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Sign up to request clarification or add additional context in comments.

Comments

1

TLDR:

LocalDate localDate = LocalDate.parse("2023-04-03");
ZonedDateTime startOfDay = localDate.atStartOfDay().atZone(ZoneOffset.systemDefault());
ZonedDateTime endOfDay = localDate.plusDays(1).atStartOfDay().atZone(ZoneOffset.systemDefault());

System.out.println(startOfDay.toInstant() + ", " + endOfDay.toInstant());

Explanation:

With this Line

ZonedDateTime zdt = ldt.atZone(ZoneId.of("UTC"));

You configure the time you created in the LocalDateTime at the Zone UTC.

I assume you (or your system) are not at UTC-Time so you need to define the ZonedDateTime at your ZoneId like this:

ZonedDateTime startOfDay = localDate.atStartOfDay().atZone(ZoneOffset.systemDefault());

If you want to define your ZoneID not from your system you can do something like this (IST for example):

ZonedDateTime startOfDay = localDate.atStartOfDay().atZone(ZoneId.of(ZoneId.SHORT_IDS.get("IST")));

1 Comment

I suggest you skip the needless involvement of a LocalDateTime. Just pass the ZoneId to atStartOfDay to get a ZonedDateTime.
1

Skip LocaDateTime

No need to involve a LocalDateTime. That class cannot represent a point on the timeline. Its usage here only confuses things.

LocalDate               // Represent a date-only value. No time of day. No time zone or offset. 
.parse( "2023-04-03" )  // Parse text in standard ISO 8601 format. Returns a `LocalDate` object. 
.atStartOfDay(          // Let java.time determine the first moment of the day. While the day always starts at 00:00 in UTC, that is not always the case in some time zones on some dates. 
    ZoneOffset.UTC      // A constant, representing an offset of zero hours-minutes-seconds. 
)                       // Returns a `ZonedDateTime`. 
.toInstant()            // Adjusts to an offset from UTC of zero hours-minutes by converting to `Instant`. 
.toString()             // Generate text in standard ISO 8601 format. 

1 Comment

The question asked for 2023-04-02T18:30:00z, and the OP is located in Pune, India. When I run your code with Asa/Kolkata as default time zone, I get 2023-04-03T00:00:00Z, so not exactly what was asked for. Still upvoted for the good information and suggestions in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.