-1

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Anuj kumar
  • 57
  • 5
  • 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? – Ole V.V. Apr 03 '23 at 17:18
  • Though not the same you may find this question and its answers helpful: [Start and End of Week w/ ThreeTenBackport](https://stackoverflow.com/questions/56742948/start-and-end-of-week-w-threetenbackport) – Ole V.V. Apr 03 '23 at 19:15

3 Answers3

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.

Spectric
  • 30,714
  • 6
  • 20
  • 43
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    Ditto Thanks for the feedback, @OleV.V. I thought the same but changed my answer before posting it. I should have kept both the inclusive and exclusive upper range. I have done it now. – Arvind Kumar Avinash Apr 03 '23 at 19:58
  • 1
    While there is good information and suggestions in all the other answers, this is the one that is to the point and shows exactly how to do the conversions. Thanks. – Ole V.V. Apr 04 '23 at 04:22
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")));
csalmhof
  • 1,820
  • 2
  • 15
  • 24
  • 1
    I suggest you skip the needless involvement of a `LocalDateTime`. Just pass the `ZoneId` to `atStartOfDay` to get a `ZonedDateTime`. – Basil Bourque Apr 03 '23 at 14:50
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. 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    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. – Ole V.V. Apr 03 '23 at 17:14