Java Program To Convert Date To String

Chapter: Miscellaneous Last Updated: 11-08-2023 17:12:16 UTC

Program:

            /* ............... START ............... */
                
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateToStringConverter {
    public static void main(String[] args) {
        // Create a Date object
        Date currentDate = new Date();

        // Create a SimpleDateFormat object with the desired format
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // Convert the Date to a String
        String dateString = dateFormat.format(currentDate);

        // Print the converted String
        System.out.println("Current Date as String: " + dateString);
    }
}

                /* ............... END ............... */
        

Output

Current Date as String: 2023-08-11 15:30:45

Notes:

  • Java program demonstrates how to convert a Date object, which represents a point in time, into a human-readable String using a specific format.
  • The SimpleDateFormat class is used to define the format in which the date should be displayed as a string. We create a SimpleDateFormat object called dateFormat and provide the desired format pattern as a string: "yyyy-MM-dd HH:mm:ss". This pattern includes placeholders for the year (yyyy), month (MM), day (dd), hours (HH), minutes (mm), and seconds (ss).
  • We use the dateFormat object's format method to convert the currentDate into a formatted string according to the pattern. The result is stored in the dateString variable.
  • Finally, we print out the converted date string using the System.out.println statement. The output will show the current date and time in the specified format.

Tags

Java program to convert date to string #How to convert a date to a string in Java? #Date to String conversion Java

Similar Programs Chapter Last Updated
Find Unique Elements In List Java Miscellaneous 07-10-2023
Java Program To Implement A Custom equals() and hashcode() In Java Miscellaneous 07-10-2023
Java Program To Find The Intersection Of Two HashSets Miscellaneous 07-10-2023
Java Program To Remove Duplicate Elements From List Miscellaneous 07-10-2023
Java program to parse a date and time string from a log file and store it in a database Miscellaneous 19-09-2023
Java Program To Print All The Dates In A Month That Fall On A Weekend Miscellaneous 19-09-2023
Java Program To Find Number Of Working Days In A Month Miscellaneous 19-09-2023
Java Program To Calculate Age From Year Of Birth Miscellaneous 16-09-2023
How To Check If Two Strings Are Anagrams In Java Miscellaneous 22-08-2023
Java Program To Make A Snake Game Miscellaneous 15-08-2023
Java Program To Find Repeated Characters Of String Miscellaneous 15-08-2023
String To Array In Java Miscellaneous 11-08-2023
Java Program To Convert String To Date Object Miscellaneous 11-08-2023
Java Program To Find Number Of Days In A Month Miscellaneous 11-08-2023
Java Program To Print First And Last Day Of Month Miscellaneous 11-08-2023
Java Program To Find Leap Year Between Two Dates Miscellaneous 11-08-2023
Java Code To Find Difference Between Two Dates In Years Months And Days Miscellaneous 11-08-2023
Java program to calculate age from year of birth Miscellaneous 29-06-2023
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023

1 2