英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论