Easy Ways to Convert String data type to Date Format in Java

String Class is available under java.lang Package. There is numerous ways to covert String to Date Format. Let us see the ways one by one.

Way 1 – Using Date() Class :

If you want to have Current Date as Date() object, you can use java.util.Date, many of the functions from Date Class depreciated from Java 8, Supported functios are

= new Date()
= new Date(milliseconds)

Through Date() class we can get the Date Time, Day of Month, Day of Year and many more nessesary functions are avaialble. Sample program as follows,

//Basic String to Date Converion<br>
Date dateFormat = new Date(1475235770601L);
Date currentDate = new Date();
System.out.println("Milliseconds using DATE() : "+dateFormat);
System.out.println("Current Date using DATE() : "+currentDate);

Output :

Milliseconds using DATE() : Fri Sep 30 17:12:50 IST 2016
Current Date using DATE() : Thu Dec 09 16:03:40 IST 2021
= new Date(day,month,year);
= new Date() //Returns Current Date

Way 2 – Using SimpledateFormat Class :

SimpleDateFormat is one of the easiet and very understandable way to do the String to Date Manipulation in Java.

Things to keep in Mind to use SimpleDateFormat in java

Ensure you have Java 8 Version
Need to include the External Jar in your project Library.
Refer this to know how to form the Date pattern as input String(https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html).

Sample Program to manipulate String to Date using SimpleDateFormat.

//Using SimpleDateFormat
String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date pureDate = format.parse(string);
System.out.println(""+pureDate);

Things to Note :

SimpleDateFormat faster in String Manipulation and it is not a Thread safe.

Supported Java Version : All Java Versions

Way 3 – Using JodaTime Class:

JodaTime is an alternate option to use SimpleDateFormat, All you need to consider before using JodaTime

Need to import the JodaTime into Workspace Library.

//Using JODA Time
String input = "January 2, 2010";
Locale locale = Locale.ENGLISH;
DateTimeZone timeZone = DateTimeZone.forID( "Pacific/Honolulu" ); 
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MMMM d, yyyy" ).withZone( timeZone ).withLocale( locale );
DateTime dateTime = formatter.parseDateTime( input );
System.out.println("Joda Time Month Of the Year : "+dateTime.getMonthOfYear());
System.out.println("Joda Time Day Of the Year : "+dateTime.getDayOfYear());
System.out.println("Joda Time Hour Of the Day : "+dateTime.getHourOfDay());
System.out.println("Joda Time Week Of the Year : "+dateTime.getWeekOfWeekyear());

Output :

Joda Time Month Of the Year : 1
Joda Time Day Of the Year : 2
Joda Time Hour Of the Day : 0
Joda Time Week Of the Year : 53

Supported Java Version : All Java Versions

Way 4 – Using LocalDate Class :

In Java 8, there is a new Time API got introduced, java.time, Through this Time API we can do all kind of String types into Time.

//Using LocalDate Java 8
String str = "December 4, 2005";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(str, formatter);
int year = date.getYear(); // 2005
int day = date.getDayOfMonth(); // 4
Month month = date.getMonth(); // December
System.out.println("Localdate : "+day+"-"+month+"-"+year);

Output :

Localdate : 4-DECEMBER-2005

Supported Java Version : Java 8

Conclusion :

If you are using Java 8 or above it is always easy and better to use LocalDate and SimpleDateFormat both have significant feature, since the JodaTime is not part of Java Core libraries.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *