Convert Date From One Format To Another Format In Java

Date Time Formatting

In this example I am going to show you how to convert date or date & time both from one format to another format in Java programming language. There are many situations where you need to convert date format in your Java based application from one format to another format.

You can use Java version prior to 8 and Java version 8 onwards to convert the date or date & time from one format to another format.

Prerequisites

Java 8+

Convert Date Format

The input date or datetime is string type and can be converted to another format. You may also want to convert string type date to Date object.

Here is the code snippets that convert input date to another format:

package com.roytuts.java.convert.date.format;

public static String convertDateFormat(String inputDate, String inputFormat, String outputFormat)
		throws ParseException {
	DateFormat inputDf = new SimpleDateFormat(inputFormat);
	DateFormat outputDf = new SimpleDateFormat(outputFormat);

	Date tempDate = inputDf.parse(inputDate);

	String dtStr = outputDf.format(tempDate);

	return dtStr;
}

In the above code I am using SimpleDateFormat to format the date in the desired format.

For Java 8 date type, you can use the following code snippets to change the input date format:

package com.roytuts.java.convert.date.format;

public static String convertDate8Format(String inputDate, String inputFormat, String outputFormat)
		throws ParseException {
	DateTimeFormatter inputDf = DateTimeFormatter.ofPattern(inputFormat);
	DateTimeFormatter outputDf = DateTimeFormatter.ofPattern(outputFormat);

	LocalDate tempDate = LocalDate.parse(inputDate, inputDf);

	String dtStr = tempDate.format(outputDf);

	return dtStr;
}

The above code snippets formats only date value, if you want to change datetime value then you need to use LocalDateTime object.

package com.roytuts.java.convert.date.format;

public static String convertDateTime8Format(String inputDateTime, String inputFormat, String outputFormat)
		throws ParseException {
	DateTimeFormatter inputDf = DateTimeFormatter.ofPattern(inputFormat);
	DateTimeFormatter outputDf = DateTimeFormatter.ofPattern(outputFormat);

	LocalDateTime tempDateTime = LocalDateTime.parse(inputDateTime, inputDf);

	String dtStr = outputDf.format(tempDateTime);

	return dtStr;
}

Remember the above code will throw exception if you try to pass small h or hh in the time value as an input. The output format can have small h or hh.

Testing Date Format Conversion

I have created a main method and now the following code will produce the expected results:

package com.roytuts.java.convert.date.format;

public static void main(String[] args) throws ParseException {
	System.out.println(convertDateFormat("20220601", "yyyyMMdd", "dd/MM/yyyy"));
	System.out.println(convertDateFormat("20220601123456", "yyyyMMddHHmmss", "dd/MM/yyyy HH:mm:ss"));
	System.out.println(convertDateFormat("20220601123456", "yyyyMMddhhmmss", "dd/MM/yyyy HH:mm:ss"));
	System.out.println(convertDateFormat("20220601123456", "yyyyMMddHHmmss", "dd/MM/yyyy hh:mm:ss"));
	System.out.println(convertDateFormat("20220601123456", "yyyyMMddhhmmss", "dd/MM/yyyy hh:mm:ss"));
	System.out.println(convertDate8Format("20220601", "yyyyMMdd", "dd/MM/yyyy"));
	System.out.println(convertDateTime8Format("20220601123456", "yyyyMMddHHmmss", "dd/MM/yyyy HH:mm:ss"));
	System.out.println(convertDateTime8Format("20220601123456", "yyyyMMddHHmmss", "dd/MM/yyyy hh:mm:ss"));
	System.out.println(convertDateFormat("04-04-2024", "dd-MM-yyyy", "dd/MM/yyyy"));
	System.out.println(convertDate8Format("04-04-2024", "dd-MM-yyyy", "dd/MM/yyyy"));

	// error as input format needs HH instead of hh
	System.out.println(convertDateTime8Format("20220601113456", "yyyyMMddhhmmss", "dd/MM/yyyy hh:mm:ss"));
}

The above code snippets will give the output as shown below:

01/06/2022
01/06/2022 12:34:56
01/06/2022 00:34:56
01/06/2022 12:34:56
01/06/2022 12:34:56
01/06/2022
01/06/2022 12:34:56
01/06/2022 12:34:56
04/04/2024
04/04/2024

Now if you try to execute the following line of code:

System.out.println(convertDateTime8Format("20220601113456", "yyyyMMddhhmmss", "dd/MM/yyyy hh:mm:ss"));

Then you will get the following exception:

Exception in thread "main" java.time.format.DateTimeParseException: Text '20220601113456' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=34, SecondOfMinute=56, NanoOfSecond=0, MilliOfSecond=0, HourOfAmPm=11, MicroOfSecond=0},ISO resolved to 2022-06-01 of type java.time.format.Parsed

The exception is occurred due to the small hh in the input date value, basically small hh expects to have time value in 12 hour format but 24 hour format has been provided in the input.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *