1 package hjw;
2
3 import java.util.Scanner;
4 import java.util.Date;
5 import java.text.SimpleDateFormat;
6 import java.text.ParseException;
7 public class ScannerDemo {
8 public static void main(String[] args) {
9 Scanner sc=new Scanner(System.in);
10 String str=null;
11 Date dt=null;
12 if(sc.hasNext("^\\d{4}-\\d{2}-\\d{2}$")){//正则验证是否为日期
13 str=sc.next("^\\d{4}-\\d{2}-\\d{2}$");//接收字符串
14 try{//转换成日期
15 dt=new SimpleDateFormat("yyyy-MM-dd").parse(str);
16 }catch (ParseException e){
17 e.printStackTrace();
18 }
19 }else{
20 System.out.println("输入的日期格式错误、、、、");
21 }
22 }
23
24 }

 

相关文章:

  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
  • 2021-07-29
  • 2021-06-22
  • 2021-09-26
猜你喜欢
  • 2022-02-07
  • 2022-12-23
  • 2021-06-02
  • 2021-09-24
  • 2022-12-23
  • 2019-11-04
相关资源
相似解决方案