Prashanth Babu

Ramblings on Hadoop Ecosytem, Java, etc.

Java8 Date and Time: Parsing and formatting

With Java v8, we have new API for Date and Time, which looks pretty elegant and also is immutable and hence thread-safe.

Please check java.time package summary added in Java v8 for detailed info of all the classes present in this package. Also checking official Java Tutorial Trail for Data Time really helps understand the API better.

This API is based on / inspired by the wonderful Joda-Time, which was the de facto standard date and time library for Java prior to Java SE 8.

In this blogpost, we will discuss how to parse and format dates with this new API.

Generating Random numbers in a Range in Java

Generating random numbers is usually a pain.
And more so when you have generate random numbers in a given range in Java. You have to consider a lot of parameters.

Java (< v8)

I would write something quick and dirty like the following.
For brevity, programming language syntax chosen is Scala, but you get the point.

val random = new java.util.Random
val (size, min, max) = (10, 20, 50)
for(i <- 0 until size) yield (min + random.nextDouble() * (max-min)).toInt

// Output of the above code in Scala REPL
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(46, 38, 22, 21, 43, 35, 49, 47, 23, 41)

Migrating from Blogger to Octopress

So like many others in the blogging world, I too shifted from Blogger to statically generated Octopress hosted on GitHub last week.And today I completed migrating all my Blogger posts to Octopress and got them hosted here. Henceforth will leave Blogger as-is and will try to make this as my active blog.

Initially I was kinda apprehensive that I might have to put in significant effort to migrate my posts from Blogger to Octopress. But it turned out to be very simple actually. In fact, it was as simple as running just a single Ruby script.

Octopress blogging setup on a new machine

This post gives a brief intro to setting up Octopress blog on a new Windows machine. This assumes you already have your blogs on your GitHub account and are changing the machine or setting up on a new machine.

Step 1: Install Ruby v1.9.3

Download and install Ruby v1.9.3 from here. ruby --version should give something on the lines of ruby 1.9.3p551 (2014-11-13) [i386-mingw32].