In this post we will see how to convert String to date in java. For this we will use SimpleDateFormat API to convert string to date.
We need to pass in the dateformat in which we want to convert String to date as a parameter to the constructor of SimpleDateFormat
1 |
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); |
Then using the SimpleDateFormat.parse(String) method we convert the String into our desired date.
While using the SimpleDateFormat.parse(String) method, we need to wrap it in try-catch block as their is a possibility of getting a ParseException due to invalid format/String passed.
Important:
Please note that the date format that we give is case sensitive
for example :
M – Month in year
m – Minute in hour
D – Day in year
d – Day in month
You can get the full details about the format here – http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Code Sample – Convert String to Date
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.kscodes.sampleproject; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateExample { public static void main(String[] args) { // Convert Date in dd-MM-yyyy format String formatDDMMYYYY = "dd-MM-yyyy"; String dateStr1 = "24-03-2015"; convertDate(dateStr1, formatDDMMYYYY); // Convert Date in MMM/dd/yyyy format String formatMMMDDYYYY = "MMM/dd/yyyy"; String dateStr2 = "Jan/20/1990"; convertDate(dateStr2, formatMMMDDYYYY); // Convert Date in MMM/dd/yyyy format String formatMMDDYYYYHHMMSS = "MM-dd-yyyy HH:mm:ss"; String dateStr3 = "12-31-2000 10:10:59"; convertDate(dateStr3, formatMMDDYYYYHHMMSS); // Give a Invalid date format String invalidFormat = "MMM/dd/yyyy"; String dateStr4 = "24-03-2015"; convertDate(dateStr4, invalidFormat); } public static void convertDate(String dateStr, String format) { System.out.println("Original String - " + dateStr); System.out.println("Date Format - " + format); DateFormat dateFormatter = new SimpleDateFormat(format); try { Date convertedDate = dateFormatter.parse(dateStr); System.out.println("The Converted Date is ::" + convertedDate); // Using dateFormat.format() to again convert date to String in // specified format System.out .println("Now again Converting Date to String in given format ::" + dateFormatter.format(convertedDate)); } catch (ParseException e) { System.out.println("Invalid String or Format !!!"); } System.out.println("----------------------------------"); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Original String - 24-03-2015 Date Format - dd-MM-yyyy The Converted Date is ::Tue Mar 24 00:00:00 EDT 2015 Now again Converting Date to String in given format ::24-03-2015 ---------------------------------- Original String - Jan/20/1990 Date Format - MMM/dd/yyyy The Converted Date is ::Sat Jan 20 00:00:00 EST 1990 Now again Converting Date to String in given format ::Jan/20/1990 ---------------------------------- Original String - 12-31-2000 10:10:59 Date Format - MM-dd-yyyy HH:mm:ss The Converted Date is ::Sun Dec 31 10:10:59 EST 2000 Now again Converting Date to String in given format ::12-31-2000 10:10:59 ---------------------------------- Original String - 24-03-2015 Date Format - MMM/dd/yyyy Invalid String or Format !!! ---------------------------------- |