Convert Date to String in Java
In this demo, we will be learning how to convert Date to String using the Java programming language.
The conversion of Date to String is called formatting.
We make use of the class java.text.SimpleDateFormat to format the dates the way users want them.
We make use of the class java.util.Date to fetch the current date and format it.
Java program to convert Date to String:
Steps:
- Ask the users to choose the type of format they want the date to be in. The formats available in this program are: ‘dd/MM/yyyy’, ‘MM/dd/yyyy’, ‘dd MMMM yyyy’, ‘dd MMM yyyy’, and ‘dd-MM-yyyy’.
- Using the java.util.Date class, get today’s date.
- Using the format() method in the java.text.SimpleDateFormat class, format the date in the format specified by the user and store it in a string variable.
- Print the string variable.
Java Code:
import java.util.Scanner; import java.text.SimpleDateFormat; import java.util.Date; public class DatetoString { public static void DatetoStringconversion(String FormatofDate) { //Creating a Date object to get todays's date. Date date = new Date(); //Setting the format of the date as specified by the user. SimpleDateFormat dateformatter = new SimpleDateFormat(FormatofDate); //Formatting the date to string String DatetoString = dateformatter.format(date); System.out.println(DatetoString); } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Select a number from 1 to 5 to choose the corresponding display format of the date: "); System.out.println("1. 'dd/MM/yyyy'\n2. 'MM/dd/yyyy'\n3. 'dd MMMM yyyy'\n4. 'dd MMM yyyy'\n5. 'dd-MM-yyyy'\nEnter your choice: "); int choice = s.nextInt(); while(true){ //Entering the switch case only if the input of choice is in the range 1-5. if(choice <= 5 && choice >=1) { /*We call the DatetoStringconversion function and send the format style chosen by the user as parameter*/ switch(choice) { case 1: DatetoStringconversion("dd/MM/yyyy"); break; case 2: DatetoStringconversion("MM/dd/yyyy"); break; case 3: DatetoStringconversion("dd MMMM yyyy"); break; case 4: DatetoStringconversion("dd MMM yyyy"); break; case 5: DatetoStringconversion("dd-MM-yyyy"); break; } //After displaying the string in the required format, we break out of the loop and stop the execution. return; } else { //Asking the user to re-enter their choice between 1 and 5. System.out.println("Enter the correct choice: "); choice = s.nextInt(); } } } }
Output:
- Displaying Date as String in the format ‘dd/MM/yyyy’:
Select a number from 1 to 5 to choose the corresponding display format of the date: 1. 'dd/MM/yyyy' 2. 'MM/dd/yyyy' 3. 'dd MMMM yyyy' 4. 'dd MMM yyyy' 5. 'dd-MM-yyyy' Enter your choice: 1 17/09/2021
- Displaying Date as String in the format ‘MM/dd/yyyy’:
Select a number from 1 to 5 to choose the corresponding display format of the date: 1. 'dd/MM/yyyy' 2. 'MM/dd/yyyy' 3. 'dd MMMM yyyy' 4. 'dd MMM yyyy' 5. 'dd-MM-yyyy' Enter your choice: 2 09/17/2021
- Displaying Date as String in the format ‘dd MMMM yyyy’:
Select a number from 1 to 5 to choose the corresponding display format of the date: 1. 'dd/MM/yyyy' 2. 'MM/dd/yyyy' 3. 'dd MMMM yyyy 4. 'dd MMM yyyy' 5. 'dd-MM-yyyy' Enter your choice: 3 17 September 2021
- Displaying Date as String in the format ‘dd MMM yyyy’:
Select a number from 1 to 5 to choose the corresponding display format of the date: 1. 'dd/MM/yyyy' 2. 'MM/dd/yyyy' 3. 'dd MMMM yyyy 4. 'dd MMM yyyy' 5. 'dd-MM-yyyy' Enter your choice: 4 17 Sep 2021
- Displaying Date as String in the format ‘dd-MM-yyyy’:
Select a number from 1 to 5 to choose the corresponding display format of the date: 1. 'dd/MM/yyyy' 2. 'MM/dd/yyyy' 3. 'dd MMMM yyyy 4. 'dd MMM yyyy' 5. 'dd-MM-yyyy' Enter your choice: 9 Enter the correct choice: 5 17-09-2021
Also read, How to change the format of date from ‘dd/mm/yyyy’ to ‘mm/dd/yyyy’ in Java
Leave a Reply