从 java 中输入 ID 用户打印日期
Print date from input ID user in java
我必须制作一个代码,输入用户名,然后输入 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 解析为?
谢谢,
经过更多研究
我得到了我需要的输出:)
完整代码如下:
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);
//id user input
Scanner input = new Scanner(System.in);
String civilIdStr = "";
System.out.println("Enter your id number");
civilIdStr = input.nextLine();
if (civilIdStr.length() < 11) {
throw new InvlaidIdException("invalid");
} else {
// sout the date from the Id user input (format DD-MM-YY)
String dob = civilIdStr.substring(0, 8);
System.out.println("Date of birth from user ID: " +
dob.substring(0, 2) + "/" + //DATE
dob.substring(2, 4) + "/" + //MONTH
dob.substring(6, 8)); //YEAR
}
}
}
输出:
Enter your name:
Victor
6
Enter your id number
11021993554644
Date of birth from user ID: 11/02/93
我必须制作一个代码,输入用户名,然后输入 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 解析为? 谢谢,
经过更多研究 我得到了我需要的输出:)
完整代码如下:
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);
//id user input
Scanner input = new Scanner(System.in);
String civilIdStr = "";
System.out.println("Enter your id number");
civilIdStr = input.nextLine();
if (civilIdStr.length() < 11) {
throw new InvlaidIdException("invalid");
} else {
// sout the date from the Id user input (format DD-MM-YY)
String dob = civilIdStr.substring(0, 8);
System.out.println("Date of birth from user ID: " +
dob.substring(0, 2) + "/" + //DATE
dob.substring(2, 4) + "/" + //MONTH
dob.substring(6, 8)); //YEAR
}
}
}
输出:
Enter your name:
Victor
6
Enter your id number
11021993554644
Date of birth from user ID: 11/02/93