【问题标题】:"... is not public in org.bp.threeten.format.DateTimeFormatter" error when trying to turn String to LocalDate尝试将字符串转换为 LocalDate 时出现“...在 org.bp.threeten.format.DateTimeFormatter 中不公开”错误
【发布时间】:2020-03-18 14:59:04
【问题描述】:

我正在尝试使用此代码将字符串转换为LocalDate

String end = sharedPref.getString("endDate", "Not available");
DateTimeFormatter formatter = new DateTimeFormatter("yyyy-MM-dd");
LocalDate endDate = LocalDate.parse(end, formatter);

但它显示了标题中提到的错误。我如何解决它? 如果有更好的方法我愿意接受建议

【问题讨论】:

    标签: java android time localtime


    【解决方案1】:

    没有采用字符串格式的构造函数,您必须像这样调用静态方法ofPattern

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    

    【讨论】:

      【解决方案2】:

      在您的情况下,您根本不需要显式格式化程序。

          LocalDate endDate = LocalDate.parse(end);
      

      只需删除formatter 的声明即可。您尝试解析的格式符合 ISO 8601。LocalDate 将此格式解析为默认格式,即不使用格式化程序。

      关于问题的解释,请参阅 YCF_L 的回答。

      链接:ISO 8601

      【讨论】:

        猜你喜欢
        • 2016-10-27
        • 2022-09-22
        • 1970-01-01
        • 2021-10-06
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多