Convert Date From One Format To Another Format In Java

Introduction

In this example I am going to show you how to convert date 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 prior to Java 8 and Java 8 onward ways to convert the date 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:

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:

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.

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

Now the following code will produce the expected results:

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"));
}

The output will be 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

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.

Source Code

Download

Leave a Reply

Your email address will not be published.