beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Convert String to date in Java

By Chaitanya Singh | Filed Under: Java Date

In this tutorial we will see how to convert a String to Date in Java.

Convert String to Date: Function

After this section I have shared a complete example to demonstrate String to Date conversion in various date formats. For those who just want a function for this conversion, here is the function code:

public Date convertStringToDate(String dateString)
{
    Date date = null;
    Date formatteddate = null;
    DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    try{
        date = df.parse(dateString);
        formatteddate = df.format(date);
    }
    catch ( Exception ex ){
        System.out.println(ex);
    }
    return formatteddate;
}

Example program for string to date conversion

package beginnersbook.com;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateDemo{
   public static void main(String args[])
   {
       String testDateString = "02/04/2014";
       String testDateString2 = "02-04-2014 23:37:50";
       String testDateString3 = "02-Apr-2014";
       String testDateString4 = "04 02, 2014";
       String testDateString5 = "Thu, Apr 02 2014";
       String testDateString6 = "Thu, Apr 02 2014 23:37:50";
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
       DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
       DateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy");
       DateFormat df4 = new SimpleDateFormat("MM dd, yyyy");
       DateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy");
       DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
       try
       {
           //format() method Formats a Date into a date/time string. 
           Date d1 = df.parse(testDateString);
           System.out.println("Date: " + d1);
           System.out.println("Date in dd/MM/yyyy format is: "+df.format(d1));

           Date d2 = df2.parse(testDateString2);
           System.out.println("Date: " + d2);
           System.out.println("Date in dd-MM-yyyy HH:mm:ss format is: "+df2.format(d2));

           Date d3 = df3.parse(testDateString3);
           System.out.println("Date: " + d3);
           System.out.println("Date in dd-MMM-yyyy format is: "+df3.format(d3));

           Date d4 = df4.parse(testDateString4);
           System.out.println("Date: " + d4);
           System.out.println("Date in MM dd, yyyy format is: "+df4.format(d4));

           Date d5 = df5.parse(testDateString5);
           System.out.println("Date: " + d5);
           System.out.println("Date in E, MMM dd yyyy format is: "+df5.format(d5));

           Date d6 = df6.parse(testDateString6);
           System.out.println("Date: " + d6);
           System.out.println("Date in E, E, MMM dd yyyy HH:mm:ss format is: "+df6.format(d6));

       }
       catch (Exception ex ){
          System.out.println(ex);
       }
   }
}

Output:

Date: Wed Apr 02 00:00:00 IST 2014
Date in dd/MM/yyyy format is: 02/04/2014
Date: Wed Apr 02 23:37:50 IST 2014
Date in dd-MM-yyyy HH:mm:ss format is: 02-04-2014 23:37:50
Date: Wed Apr 02 00:00:00 IST 2014
Date in dd-MMM-yyyy format is: 02-Apr-2014
Date: Wed Apr 02 00:00:00 IST 2014
Date in MM dd, yyyy format is: 04 02, 2014
Date: Wed Apr 02 00:00:00 IST 2014
Date in E, MMM dd yyyy format is: Wed, Apr 02 2014
Date: Wed Apr 02 23:37:50 IST 2014
Date in E, E, MMM dd yyyy HH:mm:ss format is: Wed, Apr 02 2014 23:37:50

References:

  • DateFormat parse() method
  • Date – javadoc

Enjoyed this post? Try these related posts

  1. Date validation in java
  2. Java LocalDate – equals() method example
  3. Java – Display current time in Milliseconds Format
  4. Java – Date Format to display the Day of the week
  5. Java LocalDate
  6. Java Date Validation Example

Comments

  1. Sagarika Rayudu says

    February 11, 2015 at 12:17 AM

    What if the date is of HTTP/1.1 format?i.e, in addition to df6 there is a timezone too? How do we then convert a string to date?
    EG: “Mon, Feb 9 2015 00:02:12 GMT”

    Reply
  2. prabhat says

    September 16, 2015 at 3:03 PM

    All given matreials are mind blowing.
    but request you,please post spring contents

    Reply
  3. Vikas Chauhan says

    February 18, 2016 at 7:52 AM

    There is a correction in following statement in your code:
    DateFormat df3 = new SimpleDateFormat(“dd-MMM-yyyy”);
    Use “dd-mm-yyyy” instead of dd-mmm-yyyy”. correction in month format please correct it.

    Reply
    • SYAMKUMAR CM says

      February 18, 2019 at 3:30 AM

      “Date d2 = df2.parse(testDateString2);
      System.out.println(“Date: ” + d2);
      System.out.println(“Date in dd-MM-yyyy HH:mm:ss format is: “+df2.format(d2));”

      The above code can print the Date in the given format but cannot save the date in a variable.

      Reply

Leave a Reply Cancel reply

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

Java Date and Time

  • Java Date Time
  • Java LocalDate
  • Java LocalTime
  • Java LocalDateTime
  • Java ZonedDateTime
  • Java DateTimeFormatter
  • current date time

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap