如何将java字符串转换为Date对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6510724/
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
how to convert java string to Date object
提问by user755043
I have a string
我有一个字符串
String startDate = "06/27/2007";
now i have to get Date object. My DateObject should be the same value as of startDate.
现在我必须得到 Date 对象。我的 DateObject 应该与 startDate 的值相同。
I am doing like this
我这样做
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);
But the output is in format
但输出是格式
Jan 27 00:06:00 PST 2007.
太平洋标准时间 2007 年 1 月 27 日 00:06:00。
采纳答案by citizen conn
You basically effectively converted your date in a string format to a date object. If you print it out at that point, you will get the standard date formatting output. In order to format it after that, you then need to convert it back to a date object with a specified format (already specified previously)
您基本上有效地将字符串格式的日期转换为日期对象。如果您在那时将其打印出来,您将获得标准日期格式输出。为了在此之后对其进行格式化,您需要将其转换回具有指定格式(之前已指定)的日期对象
String startDateString = "06/27/2007";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
回答by sother
"mm" means the "minutes" fragment of a date. For the "months" part, use "MM".
“mm”表示日期的“分钟”片段。对于“月”部分,使用“MM”。
So, try to change the code to:
因此,尝试将代码更改为:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = df.parse(startDateString);
Edit: A DateFormat object contains a date formatting definition, not a Date object, which contains only the date without concerning about formatting. When talking about formatting, we are talking about create a String representation of a Date in a specific format. See this example:
编辑: DateFormat 对象包含日期格式定义,而不是 Date 对象,它只包含日期而不考虑格式。在谈论格式化时,我们谈论的是以特定格式创建日期的字符串表示形式。看这个例子:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws Exception {
String startDateString = "06/27/2007";
// This object can interpret strings representing dates in the format MM/dd/yyyy
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// Convert from String to Date
Date startDate = df.parse(startDateString);
// Print the date, with the default formatting.
// Here, the important thing to note is that the parts of the date
// were correctly interpreted, such as day, month, year etc.
System.out.println("Date, with the default formatting: " + startDate);
// Once converted to a Date object, you can convert
// back to a String using any desired format.
String startDateString1 = df.format(startDate);
System.out.println("Date in format MM/dd/yyyy: " + startDateString1);
// Converting to String again, using an alternative format
DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy");
String startDateString2 = df2.format(startDate);
System.out.println("Date in format dd/MM/yyyy: " + startDateString2);
}
}
Output:
输出:
Date, with the default formatting: Wed Jun 27 00:00:00 BRT 2007
Date in format MM/dd/yyyy: 06/27/2007
Date in format dd/MM/yyyy: 27/06/2007
回答by mihsathe
try
{
String datestr="06/27/2007";
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.parse(datestr);
}
catch (Exception e)
{}
month is MM, minutes is mm..
月是MM,分钟是mm..
回答by beingbrad
var startDate = "06/27/2007";
startDate = new Date(startDate);
console.log(startDate);
回答by readikus
The concise version:
精简版:
String dateStr = "06/27/2007";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date)formatter.parse(dateStr);
Add a try/catch block for a ParseException to ensure the format is a valid date.
为 ParseException 添加 try/catch 块以确保格式是有效日期。