How to convert string to date in java program

import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateExample1 
{
    public static void main(String[] args) throws Exception 
    {
        String sDate1 = "31/12/1998";
        String sDate2 = "31-Dec-1998";
        String sDate3 = "12 31, 1998";
        String sDate4 = "Thu, Dec 31 1998";
        String sDate5 = "Thu, Dec 31 1998 23:37:50";
        String sDate6 = "31-Dec-1998 23:37:50";

        SimpleDateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat formatter2 = new SimpleDateFormat("dd-MMM-yyyy");
        SimpleDateFormat formatter3 = new SimpleDateFormat("MM dd, yyyy");
        SimpleDateFormat formatter4 = new SimpleDateFormat("E, MMM dd yyyy");
        SimpleDateFormat formatter5 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
        SimpleDateFormat formatter6 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");

        Date date1 = formatter1.parse(sDate1);
        Date date2 = formatter2.parse(sDate2);
        Date date3 = formatter3.parse(sDate3);
        Date date4 = formatter4.parse(sDate4);
        Date date5 = formatter5.parse(sDate5);
        Date date6 = formatter6.parse(sDate6);

        System.out.println("String to Date converter by TechSarvam");
        System.out.println(sDate1 + "\t" + date1);
        System.out.println(sDate2 + "\t" + date2);
        System.out.println(sDate3 + "\t" + date3);
        System.out.println(sDate4 + "\t" + date4);
        System.out.println(sDate5 + "\t" + date5);
        System.out.println(sDate6 + "\t" + date6);
    }
}

Step-by-Step Explanation

1. Class Declaration:

public class StringToDateExample1 {

The class StringToDateExample1 is declared. This class will handle converting various date strings into Date objects.

2. Main Method:

public static void main(String[] args) throws Exception {

The main() method is the entry point for the program. The throws Exception is included because SimpleDateFormat.parse() can throw a ParseException, so it is handled by declaring it in the method signature.

3. String Date Declarations:

String sDate1 = “31/12/1998”;

String sDate2 = “31-Dec-1998”;

String sDate3 = “12 31, 1998”;

String sDate4 = “Thu, Dec 31 1998”;

String sDate5 = “Thu, Dec 31 1998 23:37:50”;

String sDate6 = “31-Dec-1998 23:37:50”;

Six date strings are defined: sDate1, sDate2,., sDate6. They all have a different format: dd/MM/yyyy, dd-MMM-yyyy, MM dd, yyyy, and so on.

4. Defining SimpleDateFormat Objects:

SimpleDateFormat formatter1 = new SimpleDateFormat(“dd/MM/yyyy”);

SimpleDateFormat formatter2 = new SimpleDateFormat(“dd-MMM-yyyy”);

SimpleDateFormat formatter3 = new SimpleDateFormat(“MM dd, yyyy”);

SimpleDateFormat formatter4 = new SimpleDateFormat(“E, MMM dd yyyy”);

SimpleDateFormat formatter5 = new SimpleDateFormat(“E, MMM dd yyyy HH:mm:ss”);

SimpleDateFormat formatter6 = new SimpleDateFormat(“dd-MMM-yyyy HH:mm:ss”);

Six SimpleDateFormat objects are created corresponding to six different formats. These objects would then be used to parse the string to Date object based on the pattern provided.

formatter1 expects dd/MM/yyyy

formatter2 expects dd-MMM-yyyy

formatter3 expects MM dd, yyyy

formatter4 expects E, MMM dd yyyy

formatter5 expects E, MMM dd yyyy HH:mm:ss

formatter6 expects dd-MMM-yyyy HH:mm:ss

5. Parsing the Date Strings:

Date date1 = formatter1.parse(sDate1);

Date date2 = formatter2.parse(sDate2);

Date date3 = formatter3.parse(sDate3);

Date date4 = formatter4.parse(sDate4);

Date date5 = formatter5.parse(sDate5);

Date date6 = formatter6.parse(sDate6);

  • Each date string (sDate1 to sDate6) is parsed into a Date object using the corresponding SimpleDateFormat formatter.
  • For instance, formatter1 parses the string \”31/12/1998\” into a Date object.
  • Each parse() call converts the date string into a Date object based on the corresponding format.

6. Printing the Results:

System.out.println(“String to Date converter by TechSarvam”);

System.out.println(sDate1 + “\t” + date1);

System.out.println(sDate2 + “\t” + date2);

System.out.println(sDate3 + “\t” + date3);

System.out.println(sDate4 + “\t” + date4);

System.out.println(sDate5 + “\t” + date5);

System.out.println(sDate6 + “\t” + date6);

  • The program prints a header: “String to Date converter by TechSarvam”.
  • Then, it prints every date string after the converted Date object. The Date object would be printed in its default toString() format; that includes day, month, year, time and timezone, depending on your system.

Output:

Depending on your system’s default timezone and format for Date, the output is generated something like this:

String to Date converter by TechSarvam

31/12/1998 Thu Dec 31 00:00:00 GMT 1998

31-Dec-1998 Fri Dec 31 00:00:00 GMT 1998

12 31, 1998 Thu Dec 31 00:00:00 GMT 1998

Thu, Dec 31 1998 Thu Dec 31 00:00:00 GMT 1998

Thu, Dec 31 1998 23:37:50 Thu Dec 31 23:37:50 GMT 1998

31-Dec-1998 23:37:50 Fri Dec 31 23:37:50 GMT 1998

Explanation of the Output:

  • Each line prints the original date string and its corresponding Date object.
  • For instance, \”31/12/1998\” becomes a Date object that will print out as Thu Dec 31 00:00:00 GMT 1998.
  • The output format of the Date object will be whatever the system locale and timezone settings are.
  • Each format is read correctly by its SimpleDateFormat so that the different date string formats are read in as valid Date objects.

Key Takeaways:

1. SimpleDateFormat: SimpleDateFormat is a class used for the parsing and formatting of dates. You declare a pattern that is the date in a format as the argument; for example:  “dd/MM/yyyy “.

2. Parse Method: The parse() method of SimpleDateFormat parses a string to a Date object when the string can be converted into a valid Date.

3. Date Object Representation: If a Date object is written, it is represented by default in the full format including date and time.

Example

String sDate1 = “31/12/1998”;

SimpleDateFormat formatter1 = new SimpleDateFormat(“dd/MM/yyyy”);

Date date1 = formatter1.parse(sDate1);

System.out.println(sDate1 + “\t” + date1); // Output: 31/12/1998    Thu Dec 31 00:00:00 GMT 1998

Leave a Comment

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