java 在java中将字符串转换为java.util.date格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10894212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:00:39  来源:igfitidea点击:

convert string into java.util.date format in java

javastringdatetimedatedate-conversion

提问by Tijo K Varghese

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.

我想将其转换为其确切的日期时间格式以将其存储在 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 2010into its Date format with timestamp.

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

Can anyone explain the proper way of converting this into its java.util.Date format.?

任何人都可以解释将其转换为 java.util.Date 格式的正确方法吗?

回答by maksimov

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

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

回答by Subhrajyoti Majumder

Use this format-'EEE MMM dd HH:mm:ss z yyyy'

使用这种格式-' 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);

回答by ejb_guy

Can't you do like below

你不能像下面这样吗

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