【问题标题】:String to local date error字符串到本地日期错误
【发布时间】:2013-03-12 10:08:06
【问题描述】:

我正在尝试从字符串转换为 localdate(JODA TIME),但它给了我错误

String theDate = w.getPSDate();  == 6/03/2013
LocalDate ld = new LocalDate(theDate);
System.out.println(ld);

出于某种原因,我必须使用字符串而不是日期。我想将日期打印为 (06/03/2013)。代码有什么错误?

错误

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "06/03/2013" is malformed at "/03/2013"
at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:747)
at org.joda.time.convert.StringConverter.getPartialValues(StringConverter.java:87)
at org.joda.time.LocalDate.<init>(LocalDate.java:406)
at org.joda.time.LocalDate.<init>(LocalDate.java:354)
at Date.GetDate.main(GetDate.java:94)

Java 结果:1

【问题讨论】:

    标签: java date jodatime


    【解决方案1】:

    改用DateTimeFormatter:

    // Are you sure it's 6/03/2013 rather than 06/03/2013? dd would be nicer...
    DateTimeFormatter formatter = DateTimeFormat.forPattern("d/MM/yyyy");
    LocalDate date = formatter.parseLocalDate(text);
    

    【讨论】:

    • 如果我打印出来的日期是 2013-03-06,而不是 06/03/2013。现在是 2013 年 6 月 3 日。
    • @vijay:您的问题是关于将 from String to LocalDate 转换为。要在另一个方向进行转换,您可以使用相同的格式化程序及其print 方法。
    猜你喜欢
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多