Sunday, 26 March 2017

Playing with java date format


TimeZone examples

On a machine with default timezone as US/Pacific

scala> import java.text.SimpleDateFormat
scala> import java.util.TimeZone
scala> import java.util.Date

scala> TimeZone.getDefault()
res26: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="US/Pacific",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=US/Pacific,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 dateFormat = new SimpleDateFormat("yyyy-MM-dd")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@f67a0200

scala> dateFormat.setTimeZone(TimeZone.getTimeZone("US/Pacific"))

scala> val date = dateFormat.format(new Date(2017-1900, 10-1, 28)) //because new Date(2017-1900, 10-1, 28) takes default timezone
warning: there was one deprecation warning; re-run with -deprecation for details
date: String = 2017-10-28

scala> dateFormat.parse(date).getTime
res1: Long = 1509174000000

On a machine with default timezone as America/Los_Angeles,

scala> TimeZone.getDefault()
res11: 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 dateFormat = new SimpleDateFormat("yyyy-MM-dd")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@f67a0200

scala> dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))

scala> val date = dateFormat.format(new Date(2017-1900, 10-1, 28))
<console>:18: warning: constructor Date in class Date is deprecated: see corresponding Javadoc for more information.
       val date = dateFormat.format(new Date(2017-1900, 10-1, 28))
                                    ^
date: String = 2017-10-28

scala> dateFormat.parse(date).getTime
res13: Long = 1509174000000

On a machine with default timezone as Etc/UTC

scala> import java.text.SimpleDateFormat
scala> import java.util.TimeZone
scala> import java.util.Date

scala> TimeZone.getDefault()
res9: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

scala> val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
dateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@f67a0200

scala> dateFormat.setTimeZone(TimeZone.getTimeZone("US/Pacific"))

scala>  val date = dateFormat.format(new Date(2017-1900, 10-1, 28))
warning: there was one deprecation warning; re-run with -deprecation for details
date: String = 2017-10-27

scala>  dateFormat.parse(date).getTime
res2: Long = 1509087600000

date formats

scala> import java.util.Calendar
import java.util.Calendar

scala> val today = Calendar.getInstance().getTime()
today: java.util.Date = Sun Mar 26 00:58:12 PDT 2017

scala> import java.text.SimpleDateFormat
import java.text.SimpleDateFormat

scala> val hourMinuteSecs = new SimpleDateFormat("kkmmss").format(today)
hourMinuteSecs: String = 245812

scala> val hourMinuteSecs = new SimpleDateFormat("hhmmss").format(today)
hourMinuteSecs: String = 125812

scala> val hourMinuteSecs = new SimpleDateFormat("HHmmss").format(today)
hourMinuteSecs: String = 005812

scala> val hourMinuteSecs = new SimpleDateFormat("HHmmss a").format(today)
hourMinuteSecs: String = 005812 AM

Tuesday, 14 March 2017

scala value class vs ref class

Scala Value class vs Ref class

Int (including FloatDoubleChar etc) is a value class in scala, which is totally different thing than Null trait when you do val x: Int = null.
final abstract class Int() extends scala.AnyVal {}
What is a value class?
Value class is a class whose instances are not represented as
objects by the underlying host system. All value classes inherit from
class AnyVal.
You can try your creating your own value class,
scala> case class ValString(str: String) extends AnyVal
defined class ValString

scala> val string: ValString = null
<console>:13: error: type mismatch;
 found   : Null(null)
 required: ValString
       val string: ValString = null
                               ^
value class needs to have some value or none. Thats why its recommended to use Option[ValString]
On the other hand, String is AnyRef. Actually AnyRef corresponds to java.lang.Object.
example,
scala> class RefString(str: String) extends AnyRef
defined class RefString

scala> val refString : RefString = null
refString: RefString = null