prayagupd prayagupd - 1 month ago 7
Java Question

java.text.SimpleDateFormat#parse diference in scala vs clojure

I'm trying to assign

PDT
string to
java.util.Date
using
SimpleDateFormat
.

scala, gives me the right format which is
PDT
(
UTC-07:00
)
, with or without setting timeZone as my os timeZone is
"America/Los_Angeles"
.

scala> TimeZone.getDefault
res13: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

scala> val df = new SimpleDateFormat("yyyy:MM:dd'T'HH:mm:ssXXX")
df: java.text.SimpleDateFormat = java.text.SimpleDateFormat@da6487be

scala> df.parse("2017-10-28T23:59:59-07:00")
res12: java.util.Date = Sat Oct 28 23:59:59 PDT 2017


But clojure parses date in UTC format (
:UTC-00:00
),


without
.setTimeZone
,

aws-creds.core=> (import 'java.util.TimeZone)
java.util.TimeZone

aws-creds.core=> (TimeZone/getDefault)
#object[sun.util.calendar.ZoneInfo 0x28ce2a1 "sun.util.calendar.ZoneInfo[id=\"America/Los_Angeles\",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]"]

aws-creds.core=> (import 'java.util.Date)
java.util.Date

aws-creds.core=> (import 'java.text.SimpleDateFormat)
java.text.SimpleDateFormat

aws-creds.core=> (let [df (new SimpleDateFormat "yyyy-MM-dd'T'HH:mm:ssXXX" )]
#_=> (print (.parse df "2017-10-28T23:59:59-07:00")))
#inst "2017-10-29T06:59:59.000-00:00"nil


Same behaviour with
.setTimeZone
to
America/Los_Angeles
,

aws-creds.core=> (let [df (new SimpleDateFormat "yyyy-MM-dd'T'HH:mm:ssXXX" )]
#_=> (.setTimeZone df (TimeZone/getTimeZone "America/Los_Angeles"))
#_=> (print (.parse df "2017-10-28T23:59:59-07:00")))
#inst "2017-10-29T06:59:59.000-00:00"nil


which is strange, since in both cases I'm using
java.text.SimpleDateFormat
. Curious why
SimpleDateFormat#parse
scala stores date in
PDT
format while clojure in
UTC
format?

Answer Source

Looks like the Clojure REPL simply prints dates in UTC.

If you call toString() on the date, it prints the date exactly as the Scala REPL (which I believe implicitly calls toString when printing objects) does, and in the system time zone:

user=> (let [df (new SimpleDateFormat "yyyy-MM-dd'T'HH:mm:ssXXX" )]
  #_=>   (print (.toString (.parse df "2017-10-28T23:59:59-07:00"))))
Sat Oct 28 23:59:59 PDT 2017nil