Monday, July 27, 2015

How to Calculate Difference between two Dates in Java (In Days)

If you are not running on Java 8, then there are two ways to calculate difference between two dates in Java in days, either by using standard JDK classes e.g. java.util.Date and java.util.Calendar or by using joda-time library. Unfortunately, Java's old Date and Calendar API is buggy and not intuitive, so many of us by default use Joda for all date and time arithmetic. In this example, you will learn how to find  number of days between today and any date entered by user using Joda, as well as without using any third party library. When I first time face this problem, I thought what's a big deal about finding difference between dates? If you can convert Date to milliseconds then finding number of days, months or years is just a matter of simple arithmetic, but I was WRONG. I was not thinking about real world date and time nuisance like leap seconds, leap years, and day light saving time. It's very difficult to accurately calculate difference between two dates in Java without using third party library, unless you have time to develop your own library like Joda, which diligently takes these things into consideration. Thankfully, you don't need to worry because Java 8 got lucky third time. There is a new Date and Time API which has corrected previous mistakes and turns out to be a really gem. If you are keen to learn Java 8, not just this API, I suggest you to grab a copy of Java 8 in Action, one of the better books to learn new features of Java 8 in quick time.

Saturday, July 25, 2015

java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver - Cause and Solution

java.sql.SQLException: No suitable driver found for 
There are two ways to connect Microsoft SQL Server from Java program, either by using Microsoft's official JDBC driver (sqljdbc4.jar), or by using jTDS driver (jtds.jar). This error comes when your supplied database URL didn't match with the JDBC driver present in the CLASSPATH. Many programmers who usually use jtds.jar, makes a mistake while using sqljdbc4.jar by adding "microsoft" in JDBC URL. That makes URL invalid and JDBC API throws "java.sql.SQLException: No suitable driver : sqljdbc4.jar" error.

Thursday, July 23, 2015

3 Ways to Read File line by line in Java 8? Examples

Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read first 100 lines then rest of the lines will not be loaded into memory, which results in better performance. This is slightly different than Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream e.g. forEach(), count() etc. By using count() method you can actually count number of lines in files or number of empty lines by filtering empty lines. In fact, you can do a lot more than just reading content from file, you can filter them on some criterion e.g. filter lines which are not starting with a specific word, filter lines whose length is greater than 100, trim all lines to remove leading and trailing space, convert each lines into uppercase or lowercase etc. In short, you can use different methods from java.util.stream.Streams class to process lines read from file before printing them or returning them to caller. It's not just lambda expression, which is introduced in Java 8, there are many more goodies like this which are hidden behind aura of big features like lambdas and streams. You can read Java SE 8 for really impatient or Java 8 in Action to learn more about such hidden gems.