Convert string to java.util.date format in java

am has a line like this.

Thu Oct 07 11:31:50 IST 2010

I want to convert this to my exact date time format in order to save it in SQL.

I am familiar with so many string conversions to date, as shown below.

String dateString = "2001/03/09";

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString); 

But I need to convert the string as Thu Oct 07 11:31:50 IST 2010a date format with a timestamp.

Can someone explain the correct way to convert this to its java.util.Date. Format?

+5
source share
3 answers

Try the following:

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

For further reference, read in the SimpleDateFormat class: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

+8
source

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);
+3

,

   String str = "Thu Oct 07 11:31:50 IST 2010";
   SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd hh:mm:ss   'IST' yyyy");
   SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
   System.out.println(sdf2.format(sdf.parse(str)));
0

All Articles