Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string into java.util.date format in java

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.?

like image 238
TKV Avatar asked Jun 05 '12 08:06

TKV


People also ask

How do I convert a string to a date?

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.

How convert string to date in Java in YYYY MM DD format in Java?

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.

What is YYYY MM DD format in Java?

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.


2 Answers

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

like image 170
maksimov Avatar answered Sep 30 '22 10:09

maksimov


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);
like image 34
Subhrajyoti Majumder Avatar answered Sep 30 '22 11:09

Subhrajyoti Majumder