Table of Contents
To change the date format in a Java string, you can use the SimpleDateFormat class, which is part of the java.text package. This class allows you to specify a pattern for parsing and formatting dates.
Here are two possible approaches to changing the date format in a Java string:
Approach 1: Using SimpleDateFormat
1. Create an instance of the SimpleDateFormat class, passing the desired date format pattern as a string to the constructor. The pattern should contain a combination of letters and symbols that represent the different elements of the date.
Example:
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
2. Use the parse() method of the SimpleDateFormat class to parse the input string into a Date object. This method throws a ParseException if the input string does not match the specified format.
Example:
String inputString = "2021-07-01"; Date date = inputFormat.parse(inputString);
3. Create another instance of the SimpleDateFormat class with the desired output format pattern.
Example:
SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy");
4. Use the format() method of the SimpleDateFormat class to format the Date object into a string with the desired output format.
Example:
String outputString = outputFormat.format(date);
Here's the complete code snippet:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatter { public static void main(String[] args) throws ParseException { SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd"); String inputString = "2021-07-01"; Date date = inputFormat.parse(inputString); SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy"); String outputString = outputFormat.format(date); System.out.println(outputString); } }
This will output: "01/07/2021".
Related Article: How To Fix Java Certification Path Error
Approach 2: Using DateTimeFormatter (Java 8 and later)
Related Article: How to Generate Random Integers in a Range in Java
Starting from Java 8, a new date and time API was introduced in the java.time package. This API provides the DateTimeFormatter class, which can be used to change the date format in a Java string.
1. Create an instance of the DateTimeFormatter class using the ofPattern() method, passing the desired date format pattern as a string.
Example:
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
2. Use the parse() method of the DateTimeFormatter class to parse the input string into a LocalDate object. This method throws a DateTimeParseException if the input string does not match the specified format.
Example:
String inputString = "2021-07-01"; LocalDate date = LocalDate.parse(inputString, inputFormatter);
3. Create another instance of the DateTimeFormatter class with the desired output format pattern.
Example:
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
4. Use the format() method of the DateTimeFormatter class to format the LocalDate object into a string with the desired output format.
Example:
String outputString = date.format(outputFormatter);
Here's the complete code snippet:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateFormatter { public static void main(String[] args) { DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String inputString = "2021-07-01"; LocalDate date = LocalDate.parse(inputString, inputFormatter); DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); String outputString = date.format(outputFormatter); System.out.println(outputString); } }
This will output: "01/07/2021".
These are two possible approaches to change the date format in a Java string. Both approaches are valid and can be used depending on the version of Java you are working with and your specific requirements.