BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Convert String to date in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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

Top Related Articles:

  1. Java Access Modifiers – Public, Private, Protected & Default
  2. Date validation in java
  3. Compare two dates with each other in Java
  4. Java Date Validation Example
  5. Java 8 – Calculate days between two dates

Tags: Java-Conversion, Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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 Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap