How to convert java.sql.Date to java.util.Date in Java - JDBC Example

You often need to convert java.sql.Date to java.util.Date while interacting with databases from Java application. Since JDBC the Java API for database connectivity uses java.sql.Date to represent and most of the java code accepts java.util.Date, you have no choice but to convert SQL date to util date in Java. Though, there is some fundamental difference between them e.g. java.sql.Date only contains date part e.g. day, month, and year, it doesn't contain any time attributes, but java.util.Date contains both date and time attributes e.g. hour, minutes, seconds, and milliseconds. So, when you convert a java.sql.Date to java.util.Date, the resultant java.util.Date instance has the time part as zero i.e 00:00:00 to reflect midnight.

How to make Eclipse use spaces instead of tabs in Java editor

I use Eclipse IDE extensively to write Java programs for testing and example purposes, but when I copy those programs in any text editors e.g. VIM, Notepad, TextPad, or Edit plus, the indentation goes weird. I see a lot of white spaces which makes the program wider than expected. This happens because when you copy Java program from Eclipse to a text editor, tabs are converted to spaces and different editor has the different settings of tabs. UNIX text editors prefer tab is 8 spaces, Windows text editors, and IDEs e.g. Eclipse treats tabs as 4 spaces.

10 Examples of Joining String in Java 8 - StringJoiner and String.join()

It's quite common in day to day programming to join Strings e.g. if you have an array or List of String let's say {Sony, Apple, Google} and you want to join them by a comma to produce another String "Sony, Apple, Google", there is not an easy way to do it in Java. You need to iterate through array or list and then use a StringBuilder to append a comma after each element and finally to remove the last comma because you don't want a comma after the last element. A JavaScript-like Array.join() method or join() method of Android's TextUtils class is what you need in this situation, but you won't find any such method on String, StringBuffer, StringBuilder, Arrays, or Collections class until Java 8.

Why use Log4j logging vs System.out.println in Java

Printing messages to the console is an integral part of development, testing, and debugging a Java program. If you are working on a Server-side application, where you can not see what's going on inside the server, your only visibility tool is a log file; without logs, you can not do any debugging or see what's going on inside your application. Though, Java has a pretty handy System.out.println() methods to print something on the console, which can also be routed to log file but not sufficient for a real-world Java application. If you are running a Java program in Linux or any UNIX based system, Log4j or SLF4j or any other logging framework offers a lot more features, flexibility, and improvement on message quality, which is not possible using the System.out.println() statement.

How to install Maven on Windows 7,8 or 10?

There is no difference in installing Maven on Windows7, Windows 8, Windows 8.1 or Windows 10, you can follow the same steps to install Maven in any version Windows operating system. Installing Maven is very easy, just download the Apache Maven ZIP file from the Apache Maven Project website ( You can download the for using Maven in your 64-bit Windows machine. Remember, Maven 3.3 requires JDK 1.7 or above to run, so make sure you already installed JDK 7 or JDK8. Once you download the binary distribution of Maven, half of the job is down.

Maven Eclipse Error - "No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK"

If you are like many Java developer who uses Maven and Eclipse to build Java project using M2Eclipse plugin, you might have seen this error before. I ran into it recently when I ran the Maven Install command for one of the Java projects, configured as Maen project,  only to realize that Maven builds failed after throwing this error. The main reason for this error is that Maven is not able to find javac (the Java compiler) which is required to compile Java source files. If you remember, the javac command comes in the bin directory of JDK, hence, you need a JDK installation in your local machine to compile Java project.

How to add Primary key into a new or existing table in SQL Server

Since a primary key is nothing but a constraint you can use the ALTER clause of SQL to add a primary key into the existing table. Though it's an SQL and database best practice to always have a primary key in a table, many times you will find tables that don't have a primary key. Sometimes, this is due to lack of a column that is both NOT NULL and UNIQUE (constraint require to be a primary key) but other times purely due to lack of knowledge or lack of energy. If you don't have a column that can serve as the primary key you can use identity columns for that purpose. Alternatively, you can also combine multiple columns to create composite primary keys e.g. you can combine firstname and lastname to create a primary key name, etc.

2 Books to Prepare Java EE 6 Web Component Developer Certified Expert 1Z0-899 Exam (OCEJWCD)

Oracle launched "Java EE 6 Web Component Developer Certified Expert 1Z0-899 Exam" in 2011 to replace "Java Platform, Enterprise Edition 6 Java Server Pages and Servlet Developer Certified Expert Exam" and the "Oracle Certified Expert, Java Platform, Enterprise Edition 6 Java Server Pages and Servlet Developer" certification. They are equivalent of plain old SCWCD exam of Sun Microsystems era. Frankly speaking, I always found sun naming less confusing and clearer than Oracle's e.g. SCJP was much better than OCAJP and OCPJP. Though this exam is based on your knowledge of Servlet and JSP as per Java EE 6 specification, any book which covers this two technology e.g. "Head First Servlet and JSP" is highly recommended because Servlets and JSP are not changed much except Servlet 3.0 specification, which you can prepare separately. Oracle also provides training and preparation material and you can also take help from commercial mock exam providers like Whizlabs and Enthuware.

How to Remove Duplicate Characters from String in Java

This week's coding exercise is to remove duplicate characters from String in Java. For example, if given String is "aaaaaa" then output should be "a", because the rest of the "a" are duplicates. Similarly, if the input is "abcd" then the output should also be "abcd" because there is no duplicate character in this String.  By the way, you can not use any third-party library or Java API method to solve this problem, you need to develop your own logic or algorithm and then write code to implement that algorithm. This is one of the interesting problems from Java coding interviews and you can use this program to weed out Java programmers who cannot write code.