Author Archive


How to Pad a String in Java

Padding strings with spaces or a specific character is a common text output requirement. This is required when you want to generate a fixed format output even when the size of the data displayed  are different. However there are no built-in Java functions for String padding. Using a third party library just for string padding […]

How to Load Properties File in Java

Properties file is text file commonly used to store configuration data in Java programs. Each line in the properties file represents a property with name and value separated by an equal sign. Usually properties files are saved with the extension .properties.  We will creating and loading the sample properties file given below. It contains sample […]

How to Convert Miles to Kilometers in Java

Miles and Kilometers are different units used for distance measurement. Miles is part of the imperial system and Kilometers is part of the metric system. The following formula can be used to convert miles into kilometers, Distance in km = 1.60934 * distance in miles. The following formula can be used to convert kilometers to […]

How to Calculate Age from Date of Birth in Java

The following Java program example calculates age of a person from his date of birth. This implementation is portable across Java versions. This implementation shows why date API in Java is bad before Java 8. import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Scanner; // Calculate age from date of birth in Java // Works in all Java […]

How to Send Email in Java Using Apache Commons

Apache commons has an email library built on top of Java Mail API. This simplifies the process of sending emails from Java programs. Apache commons email API has classes for sending HTML emails, emails with attachments or even emails with embedded images. For this article, Apache commons email version 1.4 and Java Mail version 1.5.6 […]