Convert String to Date Using SimpleDateFormat Class

Sometimes, you may need to convert a String to a java.util.Date object. For this, you need to use the SimpleDateFormat class. You need to specify the format in which the String date is present and invoke the parse method. The following code demonstrates how this can be done:

This code creates a SimpleDateFormat instance. Since the input date is in the yyyy-MM-dd format, the SimpleDateFormat is created using this format. The parse() method is then invoked with the actual String date. This returns a java.util.Date object corresponding to the String. So, this code prints the following output:

Fri Jun 12 00:00:00 IST 2015

You can check out the API documentation for SimpleDateFormat in order to understand the patterns that need to be specified for various date formats.

Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

RELATED POSTS

How To Create Maven Project in Eclipse With Archetype

Maven Basics Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output...

Configuring Maven Compiler Plugin (Java)

1. What is Maven Compiler Plugin The ‘Maven Compiler Plugin’ or more commonly known as 'Maven Java Compiler' is used to compile the source code...

Concatenate Strings Using String.concat()

The String class has a method called concat. It accepts as parameter a String value and concatenates it with the current String. The following...

Â

MORE IN THIS CATEGORY

How To Change Your Default Linux Shell

The shell is a program that accepts and interprets commands. Besides bash, Linux supports many other shell programs, such as ksh, zsh, csh, and...

Interpreted Vs Compiled Languages

This is based on an excerpt from one of my favourite literature on VBScript and in fact, is quite relevant with respect to one...

Java Tutorial #1 – Variables and Data Types

Table of Contents Introduction 1. Variables 2. Data Types Primitive Data Types Integer Data Types Decimal Data Types Character Data Types Boolean Data Types Reference...

RECENT 'HOW-TO'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string with a function or variable. It does not...

SIMILAR ON CODEX

- Advertisement -spot_img