Convert String to Date Using java.util.Calendar

The java.util.Calendar class also encapsulates a date. The Calendar class has some more features than the java.util.Date class. It provides the ability to extract the day, month, year, and other fields corresponding to the date that the Calendar represents. So, sometimes, you may need to convert a String date to a Calendar instance.

For this, you first need to convert the String date to a java.util.Date instance via the SimpleDateFormat class. You can then use this Date to create a Calendar instance. The following code demonstrates this:

As before, this code uses a SimpleDateFormat and creates a java.util.Date instance from the String date. The Calendar.getInstance() method is invoked which creates a Calendar object corresponding to the current time. The calendar.setTime() method is then invoked with the Date object. This changes the Calendar’s time to the time in the Date object. So, this code prints the following output:

Fri Jun 12 00:00:00 IST 2015
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 Customize GitBash For Windows

By default the GitBash prompt settings / configuration come from shell script called 'git-prompt.sh'. This is usually hosted inside 'profile.d' directory inside the GitBash...

Remove Duplicates from List Using HashSet

The Set is also an interface in the 'Java Collection' framework. Unlike a List, a Set does not allow duplicates. Hence you can...

Create Basic Hibernate Program

Hibernate is an ORM framework that helps in accessing database tables from Java code. It is an object-oriented way of accessing database tables and...

Â

MORE IN THIS CATEGORY

Java Tutorial #7 Part 1 – Classes & Objects

Classes are a very important concept in Java. Java code always needs to be written in a class. In this article, we will be...

Convert String to java.time.LocalDate with Formatter

The LocalDate class has an overloaded parse() method. In addition to the String Date, it accepts a DateTimeFormatter instance that specifies the format of...

How To Auto-launch VBscript with Elevated Privileges

For cases when you need to execute a VBS script (*.vbs file) as admin with elevated privileges you can try the following snippet. This...

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