am having a string like this.
Thu Oct 07 11:31:50 IST 2010
I want to convert this into its exact date time format to store it in SQL.
Am familiar with so many string to date conversions like the following.
String dateString = "2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
But i need to convert a string like Thu Oct 07 11:31:50 IST 2010
into its Date format with timestamp.
Can anyone explain the proper way of converting this into its java.util.Date format.?
Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.
Parsing String to Date //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Parse/convert the required String to Date object using the parse() method by passing it as a parameter.
A SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. String pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.
Try this:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
For future reference read up on the SimpleDateFormat class: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use this format -'EEE MMM dd HH:mm:ss z yyyy
'
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
.parse("Thu Oct 07 11:31:50 IST 2010");
System.out.println(date);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With