用Java从3个整数创建一个LocalDate

huangapple go评论77阅读模式
英文:

Creating a LocalDate from 3 int's in Java

问题

如果我有一个从Scanner中获取的开始日期和结束日期,格式如下:"输入开始年份","输入开始月份","输入开始日期"以及"输入结束年份","输入结束月份","输入结束日期"。我该如何将这些整数组合起来创建startDate和endDate的LocalDate?以下是正确的方式:

LocalDate startDate = LocalDate.parse(startYear + "-" + startMonth + "-" + startDay);
LocalDate endDate = LocalDate.parse(endYear + "-" + endMonth + "-" + endDay);
英文:

If I have a start date and finish date which is captured from Scanner like this "enter start year", "enter start month", "enter start day" and "enter end year", "enter end month", "enter end day". How do I combine the int's to create a LocalDate for startDate and endDate? Is this right:

	LocalDate startDate = LocalDate.parse(startYear + "-" + startMonth + "-" + startDay);

答案1

得分: 3

你想要使用LocalDate类中的of工厂方法:

int startMonth = 12;
int startDay = 3;
int startYear = 1942;
LocalDate startDate = LocalDate.of(startYear, startMonth, startDay);
英文:

What you want is to use the of factory method from the LocalDate class:

    int startMonth = 12;
    int startDay = 3;
    int startYear = 1942;
    LocalDate startDate = LocalDate.of(startYear, startMonth, startDay);

huangapple
  • 本文由 发表于 2020年9月16日 09:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63911725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定