5

我有这样的字符串。

Thu Oct 07 11:31:50 IST 2010

我想将其转换为确切的日期时间格式以将其存储在 SQL 中。

熟悉这么多字符串到日期的转换,如下所示。

String dateString = "2001/03/09";

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

但我需要将一个字符串转换Thu Oct 07 11:31:50 IST 2010 为带有时间戳的日期格式。

谁能解释将其转换为其 java.util.Date 格式的正确方法。?

4

3 回答 3

8

试试这个:

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

为了将来参考阅读 SimpleDateFormat 类: http ://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

于 2012-06-05T08:55:30.393 回答
3

使用这种格式-' 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);
于 2012-06-05T08:57:05.897 回答
0

你不能像下面那样做吗

   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)));
于 2012-06-05T08:59:11.627 回答