2

I have to convert java.util.Date to LocalDate. I stole the example from here: Convert java.util.Date to java.time.LocalDate

but noticed something strange.

When I just use new LocalDate() then I see in debuger: enter image description here

but when I do: date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() then in debuger I see: enter image description here

so insted of iLocalMillis there is year and so on. Which causes problems for me later. Why is that so? Can I have exactly the same details after conversion as in the new LocalDate()?

Michu93
  • 3,143
  • 2
  • 30
  • 55
  • 1
    `Which causes problems for me later` ... what sorts of problems? I mean, if both instances are from the same class, what would be different about them? Did you bother to scroll all the way to the bottom of the properties in the case of the first `LocalDate`? – Tim Biegeleisen Feb 28 '19 at 13:14
  • I send this date to third party and while there is no problem with `new LocalDate()` it occurs while I try to send converted LocalDate from java.util.Date. There are no more values to this dates - just these three which you can see in screenshot – Michu93 Feb 28 '19 at 13:16
  • 1
    What is the third party doing with the `LocalDate`, and what is the problem? – Tim Biegeleisen Feb 28 '19 at 13:19
  • There is no such thing as `new LocalDate()`, so your problem is probably related to how you think you're doing that. Anyway if you observe different fields in both cases, that means you're using different classes, with different imports. – kumesana Feb 28 '19 at 13:27
  • 2
    Could a different `LocalDate` class be involved? One from Joda-Time, for example? That would seem to explain. – Ole V.V. Feb 28 '19 at 13:28
  • Possible duplicate of [How to get UTC+0 date in Java 8?](https://stackoverflow.com/questions/26142864/how-to-get-utc0-date-in-java-8) – Dezso Gabos Feb 28 '19 at 14:17

1 Answers1

6

The fields in the first snapshot match the internal structure of an org.joda.time.LocalDate class.

The fields in the second snapshot match the internal structure of a java.time.LocalDate class.

So you are mixing two different LocalDate classes in your code.

assylias
  • 297,541
  • 71
  • 621
  • 741
  • @Michu93 And… don’ do that! Don’t mix *Joda-Time* with *java.time*. Both are built by the same man, [Stephen Colebourne](https://stackoverflow.com/users/38896/jodastephen). Joda-Time was a brilliant first effort, and *java.time* ([JSR 310](https://jcp.org/en/jsr/detail?id=310)) is the successor. The *java.time* classes are a redesign, leveraging lessons learned from Joda-Time, with similar concepts but different implementation. – Basil Bourque Feb 28 '19 at 19:44