【问题标题】:Storing a date with LocalDate使用 LocalDate 存储日期
【发布时间】:2016-01-21 16:07:13
【问题描述】:

我正在尝试将以前的日期存储在 LocalDate 对象中,但我不确定如何。 到目前为止,我一直在使用自己的课程,但我想应用 java.time.LocalDate 库。 帮忙?

我的代码: LocalDate theDate = new LocalDate(theYear, theMonth, theDay);

错误信息: LocalDate theDate = new LocalDate(theYear, theMonth, theDay);

【问题讨论】:

标签: java date


【解决方案1】:

试试这样:

Java 8 java.time.LocalDate 类没有公共构造函数,

public class test {
    public static void main(String[] args) {
        int theYear=2016;
        int theMonth=1;
        int theDay=21;
        LocalDate theDate = LocalDate.of(theYear,theMonth,theDay);
        System.out.println(theDate);
    }



}

【讨论】:

    【解决方案2】:

    试试这个-

    LocalDate today = LocalDate.now();
    LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
    LocalDate yesterday = tomorrow.minusDays(2);
    

    【讨论】:

    • LocalDate theDate = new LocalDate(?);这就是我已经走了多远。问号代表我缺乏知识。
    • 我正在寻找一种使用扫描仪输入任意日期 (yyyy-mm-dd) 的方法。
    • @Erik 如果您查看LocalDate 的javadoc,您会找到LocalDate.parse(String text)。我知道,阅读文档是有争议的,但你应该尝试一下。
    • 这就是我想要做的: LocalDate theDate = new LocalDate(int theYear, int theMonth, int theDay);错误信息:构造函数 LocalDate(int, int, int) 不可见
    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 2020-01-13
    相关资源
    最近更新 更多