The Scanner class verifies birthdays by using regular expressions (string to date)

Perform birthday verification through the Scanner class and output the birthday (regular expression + string to date)

package day19_FileOperation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

/*
 * 使用Scanner实现键盘数据输入
 * 生日验证
 */
public class java_Scanner_Demo {
public static void main(String[] args) throws ParseException {
	Scanner sc = new Scanner (System.in) ;
	System.out.println("请输入你的生日");
	if (sc.hasNext("\\d{4-\\d{2}-\\d{2}}")) { //正则表达式匹配输入数据
		String str = sc.next("\\d{4}-\\d{2}-\\d{2}");
		System.out.println("输入信息为"+new SimpleDateFormat("yyyy-MM-dd").parse(str)); //将字符串转为日期
	}
	sc.close();
}	
}

 

Guess you like

Origin blog.csdn.net/qq_41663470/article/details/114015767