G G
G G

Reputation: 1069

How to get only till hour from datetime in scala

How to get the following output in scala using joda time

     input: 2016-04-26T21:00:00+00:0
     output:2016-04-26 21:00:00

thanks,

Upvotes: 0

Views: 1698

Answers (1)

Bharadwaj
Bharadwaj

Reputation: 1351

This seems to do it -

$ scala -cp joda-time-2.9.jar
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_92).
Type in expressions for evaluation. Or try :help.

scala> import org.joda.time.DateTime
import org.joda.time.DateTime

scala> import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormat

scala> val fmt2 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ")
fmt2: org.joda.time.format.DateTimeFormatter = org.joda.time.format.DateTimeFormatter@12f40c25

scala> import org.joda.time.DateTimeZone
import org.joda.time.DateTimeZone

scala> val x = fmt2.parseDateTime("2016-04-26T21:00:00+00:00").withZone(DateTimeZone.UTC)
warning: Class org.joda.convert.FromString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
warning: Class org.joda.convert.FromString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
x: org.joda.time.DateTime = 2016-04-26T21:00:00.000Z

scala> x.withMinuteOfHour(0).withSecondOfMinute(0)
res0: org.joda.time.DateTime = 2016-04-26T21:00:00.000Z

scala> val fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
fmt: org.joda.time.format.DateTimeFormatter = org.joda.time.format.DateTimeFormatter@42dafa95

scala> fmt.print(res0)
res1: String = 2016-04-26 21:00:00

Upvotes: 1

Related Questions