java - 如何检查字符串是否匹配某种日期格式??JAVA
问题描述
屏幕格式应为:
"dd-MM-yyyy HH:mm"
到目前为止是有
try
{
boolean correct = false;
while(!correct)
{
Scanner in = new Scanner(System.in);
System.out.print("Date: ");
String ans = in.next();
if(ans.matches("dd-MM-yyyy HH:mm"))
{
correct = true;
date = ans;
}
else{
System.out.println("Please use this format (dd-MM-yyyy HH:mm)");
}
}
}catch (InputMismatchException e)
{
System.out.println("--Please Enter an String--");
}
验证(检查)用户这样输入的最佳方法是什么?
解决方案
String.matches
使用正则表达式模式,并且不理解日期格式。
LocalDateTime.parse
例如,您需要实际尝试使用 解析字符串
推荐阅读
- java - angular7应用程序的自定义等待硒
- npm - 跨环境未设置 NODE_ENV
- php - 使用 domXpath 获取父元素的当前 ID
- kubernetes - 如何使用 Kubernetes 同时更新 2 个服务?
- javascript - 从 Flask 服务我无法加载 javascript 文件
- codenameone - 如何防止拖动(或捏合)的结束被传递到手指下方的按钮?
- excel - 从 URL 获取 XML 信息
- node.js - Issue integrating Firebase Cloud Functions
- c# - "The operation has timed out in ftp" this error occurs when downloading a zip file with slow network from the server
- c# - How to Trigger End of Conversation in Botframework V4?