1

I have some examples:

  • case1. val date1 = new DateTime("2015-08-03T04:59:00.000")

    output: new DateTime("2015-08-03T04:00:00.000")

  • case2. val date2 = new DateTime("2015-08-03T04:15:00.000")

    output: new DateTime("2015-08-03T04:00:00.000")

means for any datetime if the time is more the 1 minute output should be start of hour. Example for day: datetime.withTimeAtStartOfDay.

Łukasz
  • 8,555
  • 2
  • 28
  • 51
sk1007
  • 571
  • 3
  • 11
  • 30
  • Possible duplicate of [JodaTime equivalent of DateUtils.truncate()](http://stackoverflow.com/questions/4133682/jodatime-equivalent-of-dateutils-truncate) – Aivean Oct 22 '15 at 21:08

2 Answers2

0

I'm assuming that you are using the joda time DateTime. If you are, then you can use the following method

def dateTimeAtStartOfHour(s: String) = {
  new DateTime(s)
    .withMinuteOfHour(0)
    .withSecondOfMinute(0)
    .withMillisOfSecond(0)
}

val date1 = dateTimeAtStartOfHour("2015-08-03T04:59:00.000")
val date2 = dateTimeAtStartOfHour("2015-08-03T04:15:00.000")
val date3 = dateTimeAtStartOfHour("2015-08-03T04:59:13.000")

Output is

date1: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00

date2: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00

date3: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00

Community
  • 1
  • 1
JoseM
  • 4,302
  • 2
  • 24
  • 35
0

You need to use "truncate" analogue, which is called roundFloorCopy in joda:

https://stackoverflow.com/a/8510936/1349366

Community
  • 1
  • 1
Aivean
  • 10,692
  • 25
  • 39