0

I'm trying to figure out how to convert a LocalDateTime to a Date object with a 0 offset.

The current code I'm using converts LocalDateTime to Date is: Date.from(localDateTime.toInstant(ZoneOffset.UTC))

LocalDateTime: 2016-12-07T16:29:12.218

After being converted to Date: 2016-12-07T10:29:12.218-0600

Yes, I see there is a zone offset being passed into toInstant but i'm not sure how to do what i'm asking for.

UPDATE: I just received more information on my problem saying to set the date with precision and a zone of UTC to make the offset 0.

Chriskt
  • 87
  • 1
  • 2
  • 9
  • 3
    An old-fashioned `java.util.Date` object does not know anything about timezones - it does not remember what timezone it's in, and if you print it (by implicitly or explicitly calling `toString()` on it) it will always use the default timezone of the machine it's running on. You cannot have a `Date` object that's in a certain timezone, because `Date` simply does not have to capacity to remember that. Set the timezone on a `SimpleDateFormat` and format the `Date` using that to get it to display in a timezone of choice. – Jesper Mar 03 '21 at 16:00
  • Does this answer your question? [SimpleDateFormat returns wrong time zone during parse](https://stackoverflow.com/questions/16107898/simpledateformat-returns-wrong-time-zone-during-parse) – Ole V.V. Mar 03 '21 at 16:40

1 Answers1

2

Looks like you are trying to convert LocalDateTime to Date at your system zone. So, try this instead

Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant())
reddyk
  • 36
  • 2