2

I have a dataset for my User case class with the date of birth field. I want to put this data into my database, however, the date is represented in a form of the date stamp, so I do not know how to parse it using LocalDateTime. Here are the examples of the datestamps I have:

-631065600
885254400
48124800
-1007596800

And I tried to do something like this:

val formatter = DateTimeFormatter.ofPattern("n")

  val users = List(
    models.User(1, "sosodachenheonne@list.me", "Роман", "Терленчан", "m", LocalDateTime.parse("885254400", formatter)),
    models.User(2, "vyhihhimrusedac@list.me", "Артём", "Пенленный", "m", LocalDateTime.parse("48124800", formatter)),
    models.User(3, "istoordadanaihdut@gmail.com", "Борислав", "Фетленвич", "m", LocalDateTime.parse("-1007596800", formatter))
  )

But I've got such an exception:

java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {NanoOfSecond=885254400},ISO of type java.time.format.Parsed
Cassie
  • 2,941
  • 8
  • 44
  • 92
  • why did you use `DateTimeFormatter.ofPattern("n")`? – Ramesh Maharjan Dec 14 '17 at 15:42
  • @RameshMaharjan I may understand it wrong, but I used it to set the formatting of date in nanoseconds – Cassie Dec 14 '17 at 15:43
  • 2
    You do not want `LocalDateTime` here as that class purposely lacks any time zone or offset-from-UTC information. Instead, convert your count-from-epoch number to an `Instant` and from there perhaps to a `ZonedDateTime`. Search Stack Overflow for many examples. – Basil Bourque Dec 14 '17 at 17:06
  • If only the date of birth is of interest (not the hour of day), I suggest you convert to `LocalDate` in the end. – Ole V.V. Dec 16 '17 at 12:01
  • Possible duplicate of [Convert Epoch seconds to date and time format in Java](https://stackoverflow.com/questions/8262333/convert-epoch-seconds-to-date-and-time-format-in-java) – Ole V.V. Dec 16 '17 at 12:10

2 Answers2

6

You can convert your variables from a timestamp to a LocalDateTime like this: LocalDateTime.ofInstant(Instant.ofEpochMilli(885254400), ZoneOffset.UTC)

Simon
  • 6,025
  • 7
  • 46
  • 98
  • 1
    Good answer. I think the numbers in the questions are seconds since the epoch, not milliseconds, so you want `.ofEpochSecond(885254400)`. – Ole V.V. Dec 16 '17 at 12:08
1

You can use the following code:

  val fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy")
  val date = new Date(-631065600)
  println(LocalDateTime.parse(date.toString, fmt))
igorpcholkin
  • 947
  • 7
  • 15
  • I get such exception for conversion of this number: java.time.format.DateTimeParseException: Text '1969-12-24' could not be parsed at index 0 – Cassie Dec 14 '17 at 16:07
  • Probably you use java.sql.Date which produces string representation in another format. Use java.util.Date instead. – igorpcholkin Dec 14 '17 at 16:23
  • Or have a look at my answer ;) – Simon Dec 14 '17 at 16:39
  • When you can use the modern Java date and time API (`java.time`, for example `LocalDateTime`), you certainly do not want to use neither `java.util.Date` nor `java.sql.Date`. Those classes are long outdated. – Ole V.V. Dec 16 '17 at 12:05