How To Convert String To Date In Java?

Last updated on Jul 05,2024 59K Views

How To Convert String To Date In Java?

edureka.co

This article will introduce you to ways in which you can convert String to Date in Java and also give you a thorough practical demonstration. Following pointers will be covered in this article,

So let us get started,

How to convert String to Date in Java?

We will learn here “How to convert String objects to Date objects” using simple code changes and techniques. The best way to convert is
String to Date

SimpleDateFormat.parse(String);

Date to String

SimpleDateFormat.format(Date);


Parsing works in various ways:

Moving on with this article on Convert String To Date in Java,

Taking the date as a Text

If you need the month as a text in three letters, we need to define 3 ‘M’ which is taken as the month value. Then the value of the month is interpreted as text like Oct, Dec, Jun etc.

To get the result: 12-Dec-1998

Here is the code to express the String value in the Date format.

Package com.test.test
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateExample1 {
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "12-Dec-1998";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}catch (ParseException e) {
e.printStackTrace();
}
}
}

Output:
Fri Dec 12 00:00:00 MYT 1998
12-Dec-1998

Java Full Course – 10 Hours | Java Full Course for Beginners | Java Tutorial for Beginners | Edureka

🔥𝐄𝐝𝐮𝐫𝐞𝐤𝐚 𝐉𝐚𝐯𝐚 𝐂𝐨𝐮𝐫𝐬𝐞 𝐓𝐫𝐚𝐢𝐧𝐢𝐧𝐠: https://www.edureka.co/java-j2ee-training-course (Use code “𝐘𝐎𝐔𝐓𝐔𝐁𝐄𝟐𝟎”)
This Edureka Java Full Course will help you understand the various fundamentals of Java programm…

Moving on with this article on Convert String To Date in Java,

To get the Date formatted in the form “12/12/1988”

package com.test.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateExample2
{
public static void main(String[] argv)
{
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "12/12/1988";
try
{
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}catch (ParseException e) {
e.printStackTrace();
}
}
}

Java 8 uses Date-Time API that provides parse() methods to convert the String value into the Date-Time value. For basic parsing rules, there have been standards defined to represent the String value for the date and time in either ISO_LOCAL_TIME or ISO_LOCAL_DATE  format. We put the formatter code in the ‘try’ and ‘catch’ block which throws an exception at runtime everytime the defined rule is not met.
Simple parsing example is:

LocalDateTime dateTime = LocalDateTime.parse("2018-05-05T11:50:55");

Moving on with this article on Convert String To Date in Java,

To change the Time Zone

To do so, we need to define the time zone parsing methods known as “ZonedDateTime” to directly change the string value into date-time format. All you need to do is define the time zone in which you want your date-time. For example, here we need our date and time in the European zone. So, we define the tiemzone as Europe/Paris by using the ‘ZonedDateTime’ method::

ZonedDateTime zonedDateTime = ZonedDateTime.parse("2015-05-05T10:15:30+01:00[Europe/Paris]");

Now, let’s take the simple Date Time API which converts the String value to Date value using SimpleDateFormat:

  1. Java introduced a new Date Time API call with its version 8 to represent the Date time parameters known as “java.time”. The old call in all the previous versions to represent the date was java.util.date.

Let’s see how to actually convert a String to a local date and time data type:

Parse the API call:

If the String value that we need to convert to the Date-time type is of ISO-801 format then we can simply call DateFormat and SimpleDateFormat classes using parse() methods.

An example for the same:

import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1
{
public static void main(String[] args)throws Exception
{
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println(sDate1+"t"+date1);
}
} 

Output:
31/12/1998 Thu Dec 31 00:00:00 IST 1998

import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample2 {
public static void main(String[] args)throws Exception {
String sDate1="12/10/1988";
String sDate2 = "12-Oct-1988";
String sDate3 = "12 10, 1988";
String sDate4 = "Wed, Oct 12 1988";
String sDate5 = "Wed, Oct 12 1988 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(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);
}
}

By using the above code you actually get the results in all the mentioned formats. So, we defined the various date formats in a String value and then we parsed them by defining the SimpleDateFormat class. Once done the output generates in all the mentioned date time formats.

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

31-Dec-1998 Thu Dec 31 00:00:00 IST 1998

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

Thu, Dec 31 1998 Thu Dec 31 00:00:00 IST 1998

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

31-Dec-1998 23:37:50 Thu Dec 31 23:37:50 IST 1998

To know more about the date format, read the document the javadoc. Some of the valid String to Date formats are mentioned here:
y   = year   (yy or yyyy)

M   = month  (MM)

d   = day in month (dd)

h   = hour (0-12)  (hh)

H   = hour (0-23)  (HH)

m   = minute in hour (mm)

s   = seconds (ss)

S   = milliseconds (SSS)

z = time zone  text (e.g. Pacific Standard Time…)

Z   = time zone, time offset (e.g. -0800)

Note: Defining the  ‘Java.util.date’ as Date date=new date(); has been deprecated. So, always use SimpleDateFormat with a matching input String that you need to convert.

Thus we have come to an end of this article on ‘How To Convert String To Date In Java?’. If you wish to learn more, check out the Java Certification Course by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.

Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework