【问题标题】:how to convert java string to Date object [duplicate]如何将java字符串转换为日期对象[重复]
【发布时间】:2011-09-24 12:58:20
【问题描述】:

我有一个字符串

String startDate = "06/27/2007";

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

我就是这样的

DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);

但是输出是格式的

2007 年 1 月 27 日 00:06:00 PST。

【问题讨论】:

  • DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");日期 startDate = null;尝试 { startDate = dateFormat.parse("03/04/1975");字符串 foramtedDate =dateFormat.format(startDate); System.out.println("格式化后的日期为="+foramtedDate); // } catch (ParseException e) { // TODO 自动/生成的 catch 块 e.printStackTrace(); }

标签: java string date


【解决方案1】:

简洁版:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 您使用“格式”方法以所需格式显示它。但这又是字符串。是否可以在您的代码中有一个与 startDateString 格式相同的 Date 对象
    • 我不明白这个问题。日期对象的默认“格式”是这样的 Jan 27 00:06:00 PST 2007。如果要更改它,您需要使用 DateFormat 类...日期只是一个对象,从技术上讲它甚至没有“格式”它只存储有关日期的数据...覆盖其上的 toString 方法可能会给您不同的输出,但这就是 DateFormat 的用途,以保持所有日期对象的完整性和一致性。
    • 另一方面,如果您要在应用程序中始终使用这种类型的格式,您可以子类化日期对象并拥有一个 CustomDate ,它上面有帮助方法来返回格式类型你想要的,可能还有更多的自定义信息。
    • 您的月份格式字符串错误。必须是 MM 而不是 mm
    • "找不到符号 DateFormat。"下次请包括导入...
    【解决方案3】:
    var startDate = "06/27/2007";
    startDate = new Date(startDate);
    
    console.log(startDate);
    

    【讨论】:

    • 目前可行,但 Date(String) 构造函数已弃用,将来可能会完全删除。
    • 这个问题是java特有的,你发布的代码是javascript。 Java 不是 javascript。
    【解决方案4】:

    “mm”表示日期的“分钟”片段。对于“月”部分,使用“MM”。

    所以,试着把代码改成:

    DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
    Date startDate = df.parse(startDateString);
    

    编辑: 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);
            }
        }
    

    输出:

    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
    

    【讨论】:

    • 如果我打印 startDate 我仍然看到它的格式是 Mon May 23 00:00:00 PDT 2011。
    【解决方案5】:
        try 
        {  
          String datestr="06/27/2007";
          DateFormat formatter; 
          Date date; 
          formatter = new SimpleDateFormat("MM/dd/yyyy");
          date = (Date)formatter.parse(datestr);  
        } 
        catch (Exception e)
        {}
    

    月是 MM,分钟是 mm..

    【讨论】:

    • @mihsate 如果您在代码中打印日期,它将采用类似于 Mon May 23 00:00:00 PDT 2011 的格式。但我需要一个类似于我的字符串 06/27/ 的日期对象2007.
    • 哦,很好理解,这段代码只会将您的字符串转换为有效的 java 日期对象。它不会影响打印格式。然后它就变成了另一个日期对象。
    猜你喜欢
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2011-08-30
    • 2017-11-20
    • 1970-01-01
    • 2023-03-30
    • 2011-06-08
    相关资源
    最近更新 更多