【发布时间】:2022-02-02 04:29:08
【问题描述】:
我必须制作一个代码,输入用户名,然后输入一个基于格式的 ID 号(1102199344556699 - 其中前 8 个字符是他的出生日期)。 我的代码:
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
//user input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name: ");
int length = scanner.nextLine()
.length();
System.out.println(length);
//user id input
Scanner scanner2 = new Scanner(System.in);
System.out.println("Enter your id number");
String idNUmber = scanner2.nextLine();
if (idNUmber.length() < 11) {
throw new InvlaidIdException("invalid");
} else {
String dateOfBirth = idNUmber.substring(0, 8);
//found this way,on the output is giving me from input -110219935656525
//output is ok but as you can see on My output i get some extra things (please see OUTPUT)
TemporalAccessor date = DateTimeFormatter.ofPattern("ddMMyyyy").withLocale(Locale.FRANCE).parse(dateOfBirth);
System.out.println(date);
}
}
}
输入用户 ID -110219935656525
输出:
This is the birthday date: {},ISO resolved to 1993-02-11
我怎样才能摆脱 {},ISO 解析为? 谢谢,
【问题讨论】:
-
您想使用
LocalDate,而不是TemporalAccessor。只需LocalDate date = LocalDate.parse(dateOfBirth, DateTimeFormatter.ofPattern("ddMMyyyy", Locale.FRANCE));。顺便说一句,重复使用相同的Scanner,不要创建第二个。 -
非常感谢您的贡献????。你解释的我也试试看????
-
效果很好:)