- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to Date in Java
Let’s say the following are our strings −
String date1 ="11/10/2018"; String date2 = "15-Mar-2019 21:11:35";
Now, convert the above string to dates −
SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); Date formattedDate1 = dateFormat1.parse(date1); Date formattedDate2 = dateFormat2.parse(date2);
Example
Following is the program to convert String to Date in Java −
import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String[] args)throws Exception { String date1 ="11/10/2018"; String date2 = "15-Mar-2019 21:11:35"; SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); Date formattedDate1 = dateFormat1.parse(date1); Date formattedDate2 = dateFormat2.parse(date2); System.out.println(formattedDate1); System.out.println(formattedDate2); } }
Output
Thu Oct 11 00:00:00 UTC 2018 Fri Mar 15 21:11:35 UTC 2019
- Related Articles
- How to convert String to Date in java?
- Java Program to Convert String to Date
- How to convert Date to String in Java 8?
- Java Program to convert a String to Date
- Convert date string to timestamp in Python
- How to convert a string to date in MySQL?
- How to convert a Python date string to a date object?
- How to convert a Date value to string in JDBC?
- How to use the SimpleDateFormat class to convert a Java Date to a formatted String?
- Convert Java String to Short in Java
- How to convert a string in to date object in JavaScript?
- Program to convert Milliseconds to Date Format in Java
- Java Program to convert Date into milliseconds
- How to convert from string to date data type in MongoDB?
- How to convert unix timestamp string to readable date in Python?

Advertisements