0

I am trying to convert a localdatetime to Date (which I use in Google Calendar api method)

Zone Id = "America/New_York"

I always, get this result :

2021-08-10T00:00:00.000+02:00     

+02:00 is my local time zone

I want to get -04:00 America/New_York in same format as above

Here is the method

public static Date toDate(LocalDateTime startTime, String zoneId) {
    ZoneId zonedId = ZoneId.of(zoneId);
    return Date.from(Instant.from(startTime.atZone(zonedId)));
}

Please anyone can help?

devReddit
  • 2,696
  • 1
  • 5
  • 20
ali
  • 49
  • 6
  • Your question does not make sense. An old-fashioned `Date` hasn’t got, as in cannot have a format. Nor a time zone nor a UTC offset. So no, no one can help you give you one that has. – Ole V.V. Aug 10 '21 at 18:32
  • A [mre], please? Also asking because from `toDate(LocalDateTime.of(2021, Month.AUGUST, 10, 0, 0), "America/New_York")` I get `Tue Aug 10 06:00:00 CEST 2021`. It looks nothing like the `2021-08-10T00:00:00.000+02:00 ` that you reported getting. – Ole V.V. Aug 10 '21 at 18:40

5 Answers5

1

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

A sample solution using java.time, the modern Date-Time API: You can use ZonedDateTime#withZoneSameInstant for this purpose.

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZoneId sourceZone = ZoneId.of("Europe/Berlin");
        ZonedDateTime zdtSource = ZonedDateTime.now(sourceZone);
        System.out.println(zdtSource);

        ZoneId targetZone = ZoneId.of("America/New_York");
        ZonedDateTime zdtTarget = zdtSource.withZoneSameInstant(targetZone);
        System.out.println(zdtTarget);
    }
}

Output from a sample run:

2021-08-10T20:06:24.023038+02:00[Europe/Berlin]
2021-08-10T14:06:24.023038-04:00[America/New_York]

ONLINE DEMO

What if I need OffsetDateTime?

You can use ZonedDateTime#toOffsetDateTime to get OffsetDateTime out of a ZonedDateTime object e.g.

OffsetDateTime odtTarget = zdtTarget.toOffsetDateTime();

Note: For any reason, if you need to convert this object of ZonedDateTime to an object of java.util.Date, you can do so as follows:

Date date = Date.from(zdtTarget.toInstant());

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


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Thank you for everyone , I read the comments and got the idea how to solve it from the comments then I ended up with the following code

import com.google.api.client.util.DateTime;

   public static DateTime toGoogleDateTime(LocalDateTime startTime, String zoneId) {
    ZonedDateTime zdtSource = startTime.atZone(ZoneId.of(zoneId));
    Date date = Date.from(zdtSource.toInstant());
    return new DateTime(date, TimeZone.getTimeZone(ZoneId.of(zoneId)));
}

the zoneId gets from

https://gist.github.com/adamgen/3f2c30361296bbb45ada43d83c1ac4e5

ali
  • 49
  • 6
0

It looks like you need to use a DateTimeFormatter.
If that is not sufficient, try DateTimeFormatterBuilder.

Take a look at DateTimeFormatterBuilder usages in Java 8, specifically optionals for examples of using a DateTimeFormatterBuilder.

DwB
  • 37,124
  • 11
  • 56
  • 82
0

To get java.util.Date from with proper Zone conversion, first use ZonedDateTime and DateTimeFormatter from java.time and finally use SimpleDateFormat to get java.util.Date.

public static Date toDate(LocalDateTime startTime, String zoneId) throws ParseException {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
    ZonedDateTime source = startTime.atZone(ZoneId.of("Asia/Dhaka"));
    ZonedDateTime result = source.withZoneSameInstant(ZoneId.of(zoneId));
    String date = result.format(formatter);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
    return format.parse(date);
}
devReddit
  • 2,696
  • 1
  • 5
  • 20
0

I was looking for this and found an easy solution, so I thought I should share it.

I just used a toString method:

date.toString() and then modify the String.

 private static String getFormattedDate(DateTime date) {
        String string = date.toString();
        String result = StringUtils.substring(string, 0, string.length() - 5);
        return result;
    }
Tomislav Stankovic
  • 3,080
  • 17
  • 35
  • 42
Marisabel
  • 11
  • 4