java 如何在java中将java.lang.string转换为java.util.date

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6979120/
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-30 18:07:20  来源:igfitidea点击:

How to convert java.lang.string To java.util.date in java

javadate-format

提问by n92

Possible Duplicate:
how to parse date in java?

可能的重复:
如何在 Java 中解析日期?

I am getting "Mon Aug 01 00:00:00 IST 2011" (java.lang.String) as string and i need to convert it to "01-08-2011" Date(java.Util.Date) ,how to do this

我得到 " Mon Aug 01 00:00:00 IST 2011" (java.lang.String) 作为字符串,我需要将它转换为 " 01-08-2011" Date(java.Util.Date) ,如何做到这一点

回答by Pratik

use the SimpleDateFormat class

使用 SimpleDateFormat 类

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

to print the date. This will print the current date in predefined formatted using SimpleDateFormat

打印日期。这将使用 SimpleDateFormat 以预定义格式打印当前日期

System.out.println("Current Date " + sdf.format(new Date());

String mydate = "01-08-2011";

to convert string into date

将字符串转换为日期

Date parseDate = sdf.parse(mydate);

回答by sgokhales

Look at the SimpleDateFormatclass in the javadocs.

查看SimpleDateFormatjavadoc中的类。

Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default locale

使用给定的模式和默认语言环境的默认日期格式符号构造一个 SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Date " + sdf.format(new Date());

回答by Mot

Please take a look at (Simple)DateFormat's parsemethod.

请看一下 ( Simple)DateFormatparse方法。