如何将java字符串转换为Date对象

use*_*043 134 java string date

我有一个字符串

String startDate = "06/27/2007";
Run Code Online (Sandbox Code Playgroud)

现在我必须得到Date对象.我的DateObject应该与startDate的值相同.

我这样做

DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);
Run Code Online (Sandbox Code Playgroud)

但输出是格式化的

1月27日00:06:00太平洋标准时间2007

cit*_*onn 184

您基本上有效地将字符串格式的日期转换为日期对象.如果在此时将其打印出来,您将获得标准日期格式输出.为了在此之后格式化它,您需要将其转换回具有指定格式的日期对象(先前已指定)

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 您使用"格式"方法以所需格式显示它.但这又是String.是否可以在代码中使用与startDateString格式相同的Date对象 (2认同)
  • 我不明白这个问题.日期对象的默认"格式"类似于2007年1月27日00:06:00 PST.如果要更改它,则需要使用DateFormat类...日期只是一个对象,从技术上讲它甚至没有"format"它只存储有关日期的数据...覆盖它上面的toString方法可能会给你一个不同的输出,但这就是DateFormat的用途,以保持所有日期对象的完整性和一致性. (2认同)

sot*_*her 20

"mm"表示日期的"分钟"片段.对于"月"部分,请使用"MM".

因此,尝试将代码更改为:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Date startDate = df.parse(startDateString);
Run Code Online (Sandbox Code Playgroud)

编辑:一个DateFormat对象包含一个日期格式定义,而不是一个Date对象,它只包含日期而不涉及格式.在谈论格式化时,我们讨论的是以特定格式创建Date的String表示.看这个例子:

    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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)


mih*_*the 9

    try 
    {  
      String datestr="06/27/2007";
      DateFormat formatter; 
      Date date; 
      formatter = new SimpleDateFormat("MM/dd/yyyy");
      date = (Date)formatter.parse(datestr);  
    } 
    catch (Exception e)
    {}
Run Code Online (Sandbox Code Playgroud)

月是MM,分钟是mm ..


rea*_*kus 5

简洁版:

String dateStr = "06/27/2007";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date)formatter.parse(dateStr);  
Run Code Online (Sandbox Code Playgroud)

为ParseException添加try/catch块以确保格式是有效日期.