题解 | #出生日期输入输出#
出生日期输入输出
https://www.nowcoder.com/practice/4a4a9dd1edb6453ba4a0432319200743
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
try(Scanner in = new Scanner(System.in);) {
//输入:20140215
String line = in.nextLine();
//方式1:使用字符串的截取
// String year = line.substring(0, 4);
// String month = line.substring(4, 6);
// String date = line.substring(6);
// System.out.printf("year=%s\n"+"month=%s\n"+"date=%s",year,month,date);
//方式2:使用字符串的
LocalDate date = LocalDate.parse(line,DateTimeFormatter.ofPattern("yyyyMMdd"));
int year = date.getYear();
int month = date.getMonthValue();
int dayOfMoth = date.getDayOfMonth();
System.out.printf("year=%d\n"+"month=%02d\n"+"date=%02d",year,month,dayOfMoth);
}
}
}


查看1道真题和解析