Java 如何拼贴预订日期输入并以LocalDate格式返回,以及使用布尔值验证年/月/日值?
如何在getCarBookingDateFull()方法中拼贴预订日期输入并以LocalDate格式返回,以及使用布尔值验证年/月/日值?我尝试了日期/时间格式化程序,但无法对其进行解析。抛出异常 我是Java新手,所以我不是100%了解所有概念。非常感谢Java 如何拼贴预订日期输入并以LocalDate格式返回,以及使用布尔值验证年/月/日值?,java,Java,如何在getCarBookingDateFull()方法中拼贴预订日期输入并以LocalDate格式返回,以及使用布尔值验证年/月/日值?我尝试了日期/时间格式化程序,但无法对其进行解析。抛出异常 我是Java新手,所以我不是100%了解所有概念。非常感谢 package carrentalsystem; import java.util.Scanner; import java.time.LocalDate; import java.time.format.DateTimeFormatter
package carrentalsystem;
import java.util.Scanner;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class CarAndBookingDates {
private int year;
private int month;
private int day;
int carSelection;
Scanner scan = new Scanner(System.in);
public void CarAndBookingDates() {
this.year = 0000;
this.month = 00;
this.day = 00;
this.carSelection = 0;
}
public int carSelection() {
System.out.println("To make a booking:");
System.out.println("\t" + "Select the car number from the car list:");
carSelection = scan.nextInt();
return carSelection;
}
public void setCarSelection(int carSelection){
this.carSelection = carSelection;
}
public int getCarSelection(){
return carSelection;
}
public int promptForYear() {
System.out.println("\t" + "Enter booking start date.");
System.out.println("\t"+"Please enter the year - for example '2020':");
year = scan.nextInt();
return year;
}
public void setpromptForYear(int year){
this.year = year;
}
public int getpromptForYear(){
return year;
}
public int promptForMonth() {
System.out.println("\t"+"Please enter the month number - "
+ "for example '6':");
month = scan.nextInt();
return month;
}
public void setpromptForMonth(int month){
this.month = month;
}
public int getpromptForMontrh(){
return month;
}
public int promptForDay() {
System.out.println("\t"+"Please enter the day number - "
+ "for example '21:");
day = scan.nextInt();
return day;
}
public void setpromptForDay(int day){
this.day = day;
}
public int getpromptForDay(){
return day;
}
public void getCarBookingDateFull() {
String DayParsed = String.valueOf(day);
String MonthParsed = String.valueOf(month);
String YearParsed = String.valueOf(year);
String TotalDate = DayParsed + MonthParsed + YearParsed;
//LocalDate FullDateParsed = LocalDate.parse(DateParsed);
//System.out.println(YearParsed +"/" + MonthParsed +"/" + DayParsed);
//LocalDate localDate = LocalDate.parse(TotalDate);
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("dd-MM-yyyy");
try {
LocalDate datetime = LocalDate.parse(TotalDate, pattern);
System.out.println(datetime);
} catch (DateTimeParseException e) {
}
}
}
}
像这样使用LocalDate.parse()
:-
public static LocalDate getDate(int date, int month, int year) {
String strDate = ""+date;
if(date < 9) {
strDate = "0"+date;
}
String strMonth = ""+month;
if(month < 9) {
strMonth = "0"+month;
}
return LocalDate.parse(year+"-"+strMonth+"-"+strDate);
}
public static LocalDate getDate(int date, int month, int year) {
return LocalDate.of(year,month,date);
}
如果日期无效,这两个函数都将抛出DateTimeException
。使用LocalDate.parse()
,如下所示:-
public static LocalDate getDate(int date, int month, int year) {
String strDate = ""+date;
if(date < 9) {
strDate = "0"+date;
}
String strMonth = ""+month;
if(month < 9) {
strMonth = "0"+month;
}
return LocalDate.parse(year+"-"+strMonth+"-"+strDate);
}
public static LocalDate getDate(int date, int month, int year) {
return LocalDate.of(year,month,date);
}
try {
LocalDate datetime = LocalDate.of(year, month, day);
System.out.println(datetime);
} catch (DateTimeException e) {
System.out.println("Date is invalid: " + e);
}
如果日期无效,这两个函数都将抛出DateTimeException
try {
LocalDate datetime = LocalDate.of(year, month, day);
System.out.println(datetime);
} catch (DateTimeException e) {
System.out.println("Date is invalid: " + e);
}
为了显示它捕获了错误的日期,我在从扫描仪读取任何数字之前尝试运行了这个代码段(当然,这不是您将如何使用它)。结果是:
日期无效:java.time.DateTimeException:的值无效
MonthOfYear(有效值1-12):0
进入2020年后,我得到了以下结果:11个月,27个月:
2020-11-27
进一步建议
您可能希望将日期作为LocalDate
对象存储在CarAndBookingDates
对象中。用户可能更喜欢在一行中输入,而不是在三个提示下依次输入三个数字,例如27-11-2020
或3-12-2020
。然后使用两个argLocalDate.parse(String,DateTimeFormatter)
来解析用户输入的字符串
我认为,CarAndBookingDates
可以有利地承担在将任何信息返回给调用者之前提示使用的责任。由于类现在已存在,调用方可能忘记调用提示用户的方法,然后只检索默认值
为了显示它捕获了错误的日期,我在从扫描仪读取任何数字之前尝试运行了这个代码段(当然,这不是您将如何使用它)。结果是:
日期无效:java.time.DateTimeException:的值无效
MonthOfYear(有效值1-12):0
进入2020年后,我得到了以下结果:11个月,27个月:
2020-11-27
进一步建议
您可能希望将日期作为LocalDate
对象存储在CarAndBookingDates
对象中。用户可能更喜欢在一行中输入,而不是在三个提示下依次输入三个数字,例如27-11-2020
或3-12-2020
。然后使用两个argLocalDate.parse(String,DateTimeFormatter)
来解析用户输入的字符串
我认为,
CarAndBookingDates
可以有利地承担在将任何信息返回给调用者之前提示使用的责任。由于类现在处于静止状态,调用方可能会忘记调用提示用户的方法,然后只检索默认值。问题中的验证部分如何?@John显示的代码验证日期并抛出一个DateTImeParseException
。您需要在某个地方捕获并处理它。问题中的验证部分如何?@John显示的代码验证日期,并抛出一个DateTImeParseException
,该异常无效。你需要在某处抓住并处理它。