Convert String to Date and Date to String in Java

In this article, we will see programs to Convert String to Date and Date to String in Java using SimpleDateFormat class.

 

Convert String to Date in Java

We can convert or parse a String to java.util.Date using SimpleDateFormat.

For example, if we want to convert a String in MM/dd/yyyy format to Date, we can do the same using :


new SimpleDateFormat("MM/dd/yyyy").parse(dateString)

Here is a complete example, that converts a String in MM/dd/yyyy to a java.util.Date and then prints it using yyyy-MM-dd format.

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

public class SomeProgram {

  public static void main(String[] args) {
    String dateStr = "07/05/2018";
    try {
      convertStringToDate(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  public static void convertStringToDate(String dateString) throws ParseException {
    System.out.println("Given date is " + dateString);

    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Date date = sdf.parse(dateString);
    System.out.println("Parsed Date is : " + date);
    System.out.println("Parsed Date in yyyy-MM-dd format is : " + new SimpleDateFormat("yyyy-MM-dd").format(date));
  }

}

Output :

Given date is 07/05/2018
Parsed Date is : Thu Jul 05 00:00:00 EDT 2018
Parsed Date in yyyy-MM-dd format is : 2018-07-05

 

Convert Date to String in Java

We can convert a java.util.Date to a String using SimpleDateFormat.

For example, if we want to convert current Date to a String in MM/dd/yyyy format, we can do the same using :


new SimpleDateFormat("MM/dd/yyyy").format(new Date())

Here is a complete code, that converts current Date into Strings in MM/dd/yyyy and yyyy-MM-dd formats.

public static void convertDateToString(Date date) throws ParseException {
  System.out.println("Given date is " + date);
  System.out.println("Parsed Date in MM/dd/yyyy format is : " + new SimpleDateFormat("MM/dd/yyyy").format(date));
  System.out.println("Parsed Date in yyyy-MM-dd format is : " + new SimpleDateFormat("yyyy-MM-dd").format(date));
}

Output :

Given date is Fri Jul 06 00:16:29 EDT 2018
Parsed Date in MM/dd/yyyy format is : 07/06/2018
Parsed Date in yyyy-MM-dd format is : 2018-07-06

 

Here is the complete program:

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

public class SomeProgram {

  public static void main(String[] args) {
    String dateStr = "07/05/2018";
    try {
      System.out.println("Convert String to Date :\n");
      convertStringToDate(dateStr);
      
      System.out.println("\nConvert Date to String :\n");
      convertDateToString(new Date());
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  public static void convertStringToDate(String dateString) throws ParseException {
    System.out.println("Given date is " + dateString);

    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Date date = sdf.parse(dateString);
    System.out.println("Parsed Date is : " + date);
    System.out.println("Parsed Date in yyyy-MM-dd format is : " + new SimpleDateFormat("yyyy-MM-dd").format(date));
  }
  
  public static void convertDateToString(Date date) throws ParseException {
    System.out.println("Given date is " + date);
    System.out.println("Parsed Date in MM/dd/yyyy format is : " + new SimpleDateFormat("MM/dd/yyyy").format(date));
    System.out.println("Parsed Date in yyyy-MM-dd format is : " + new SimpleDateFormat("yyyy-MM-dd").format(date));
  }

}

Output:

Convert String to Date :

Given date is 07/05/2018
Parsed Date is : Thu Jul 05 00:00:00 EDT 2018
Parsed Date in yyyy-MM-dd format is : 2018-07-05

Convert Date to String :

Given date is Fri Jul 06 00:16:29 EDT 2018
Parsed Date in MM/dd/yyyy format is : 07/06/2018
Parsed Date in yyyy-MM-dd format is : 2018-07-06

 

© 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags