Mutable and Immutable Objects

Mutable and Immutable words definition in English dictionary is “can change” and “cannot change” respectively. A mutable object can be changed after it’s created and an immutable object can’t.For example in java ,String is immutable object and StringBuilder is mutable. Let’s understand this through example


public class MutableVSImmutable {
    public static void main(String[] args) {

        System.out.println("------String  Output----------");
        String immuatble = "you can not change me, " +
            "if you change, new object will create ," +
            " check hashcode after change";

        System.out.print("hashcode before----> " + immuatble.hashCode());

        immuatble = immuatble + "This change will create new instance of immuatble ," +
            " old object you can not modify, " +
            "check new hashcode generated it means new object";

        System.out.println(" hashcode  after--->" + immuatble.hashCode());

        System.out.println("-----String Builder Output-----");

        StringBuilder muatble = new StringBuilder(
            "you can change me I will remain same object even after modification," +
            " check my hashcode");
        System.out.print("hashcode before---->" + muatble.hashCode());

        muatble = muatble
            .append("It will not create new object of mutable,check same hashcode,it means same object");

        System.out.println(" hashcode after---->" + muatble.hashCode());

Here is the output
------String  Output----------
 hashcode before----> -36412608 hashcode  after--->-163911166
 -----String Builder Output-----
 hashcode before---->1704856573 hashcode after---->1704856573

Parse and Format LocalDate,LocalDateTime,LocalTime

parse method used to convert String to LocalDate,LocalDateTime,LocalTime using the DateTimeFormatter  class.

format method used to convert LocalDate,LocalDateTime,LocalTime to String using the DateTimeFormatter class.

The DateTimeFormatter class provides various predefined formatters or you can define your own pattern using ofPattern method.

1.Parse and Format LocalDate


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateParseFormatExample {

	private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");

	public static LocalDate convertStringToLocalDate(String formattedCurrentDate) {
		LocalDate localDate = LocalDate.parse(formattedCurrentDate, dateTimeFormatter);
		return localDate;

	public static String convertLocalDateToString(LocalDate date) {
		String formattedStringDate = date.format(dateTimeFormatter);
		return formattedStringDate;

	public static void main(String[] args) {

		LocalDate currentDate =;
		System.out.println("LocalDate::" + currentDate);

		String formattedCurrentDate = convertLocalDateToString(currentDate);
		System.out.println("LocalDateToString::" + formattedCurrentDate);

		LocalDate localDate = convertStringToLocalDate(formattedCurrentDate);
		System.out.println("StringToLocalDate::" + localDate);



2.Parse and Format LocalDateTime


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeParseFormatExample{
	private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss:SSS");

	public static LocalDateTime convertStringToLocalDateTime(String formattedCurrentDate) {
		LocalDateTime localDateTime = LocalDateTime.parse(formattedCurrentDate, dateTimeFormatter);
		return localDateTime;

	public static String convertLocalDateTimeToString(LocalDateTime dateTime) {
		String formattedStringDateTime = dateTime.format(dateTimeFormatter);
		return formattedStringDateTime;

	public static void main(String[] args) {
		LocalDateTime currentDateTime =;
		System.out.println("LocalDateTime::" + currentDateTime);
		String formattedCurrentDateTime = convertLocalDateTimeToString(currentDateTime);
		System.out.println("LocalDateTimeToString::" + formattedCurrentDateTime);
		LocalDateTime localDateTime = convertStringToLocalDateTime(formattedCurrentDateTime);
		System.out.println("StringToLocalDateTime::" + localDateTime);

3.Parse and Format LocalTime


import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class TimeParseFormatExample {
	private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss:SSS");

	public static LocalTime convertStringToLocalTime(String formattedCurrentDate) {
		LocalTime localTime = LocalTime.parse(formattedCurrentDate, dateTimeFormatter);
		return localTime;

	public static String convertLocalTimeToString(LocalTime localtime) {
		String formattedStringTime= localtime.format(dateTimeFormatter);
		return formattedStringTime;

	public static void main(String[] args) {

		LocalTime currentDateTime =;
		System.out.println("LocalTime::" + currentDateTime);

		String formattedCurrentTime = convertLocalTimeToString(currentDateTime);
		System.out.println("LocalTimeToString::" + formattedCurrentTime);

		LocalTime localTime = convertStringToLocalTime(formattedCurrentTime);
		System.out.println("StringToLocalTime::" + localTime);




1.Camel-File Transfer Example

This is the post excerpt.

File-based information exchange is common between distributed system. For example, A java application getting data from the legacy system in txt files and application read those txt files from inbox directory and after receiving a file in inbox directory file moved to outbox directory after processing.

In the following example, we will learn how to route a file from one directory to another directory.

For example .. emplyoyee.txt file is present in inbox directory and application will move it to the outbox directory.
employee.txt file content is


import org.apache.camel.builder.RouteBuilder;

public class EmployeeFileRouter extends RouteBuilder {

 public void configure() throws Exception {

If you want to keep the original file as is in inbox directory we need to set the noop option to true.
Try this example with option noop =false to differentiate the behavior.

If you want to give different filename use fileName option to set different file name.
Here is the modified configure method with fileName option.
File name changed from employee.txt to empdetails.txt

 public void configure() throws Exception {

By default camel moves file to .camel directory.If you want to move file to different directory you can use move option.Moving file to different directory is good option after processing because you can avoid processing same file over and over and you can also keep the backup of processed files.

Modified configure method with move option.

 public void configure() throws Exception {


import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

public class EmployeeMain {

	public static void main(String[] args) {
		CamelContext camelContext = new DefaultCamelContext();
		try {
			camelContext.addRoutes(new EmployeeFileRouter());
		} catch (Exception e) {


In order to run this example we need camel-core dependency because file component is present in camel-core jar

<?xml version="1.0"?>
<project xsi:schemaLocation="" xmlns=""