Java验证时间格式是否正确方法类项目实战
作者:码奴生来只知道前进~ 时间:2021-05-30 02:15:15
在很多场景中我们需要验证时间日期的是否属于正确的格式,验证时间是否符合常规的。
1、验证 yyyy-MM-dd HH:mm:dd 格式的日期
String date = "2020-01-25 12:36:45";
System.out.println("date "+isLegalDate(date.length(),date,"yyyy-MM-dd HH:mm:ss"));
2、验证 yyyy-MM-dd 格式的日期
String yearMonthday = "2020-01-01";
System.out.println("yearMonthday: "+isLegalDate(yearMonthday.length(),yearMonthday,"yyyy-MM-dd"));
3、验证 yyyy-MM 格式的日期
String yearMonth = "2020-02";
System.out.println("yearMonth: "+isLegalDate(yearMonth.length(),yearMonth,"yyyy-MM"));
4、验证 yyyy 格式的日期
String year = "2020";
System.out.println("year: "+isLegalDate(year.length(),year,"yyyy"));
5、验证 HH:mm:ss 格式的日期
String hms = "12:36:89";
System.out.println("hms: "+isLegalDate(hms.length(),hms,"HH:mm:ss"));
6、下面是一个完整的方法类直接运行就可以实现验证日期格式是否正确的
package com.shucha.deveiface.biz.test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author tqf
* @Description 时间格式校验
* @Version 1.0
* @since 2020-09-15 16:49
*/
public class IsLegalDate {
public static void main(String[] args) {
//1、验证 yyyy-MM-dd HH:mm:dd 格式的日期
String date = "2020-01-25 12:36:45";
System.out.println("date "+isLegalDate(date.length(),date,"yyyy-MM-dd HH:mm:ss"));
//2、验证 yyyy-MM-dd 格式的日期
String yearMonthday = "2020-01-01";
System.out.println("yearMonthday: "+isLegalDate(yearMonthday.length(),yearMonthday,"yyyy-MM-dd"));
//3、验证 yyyy-MM 格式的日期
String yearMonth = "2020-02";
System.out.println("yearMonth: "+isLegalDate(yearMonth.length(),yearMonth,"yyyy-MM"));
//4、验证 yyyy 格式的日期
String year = "2020";
System.out.println("year: "+isLegalDate(year.length(),year,"yyyy"));
//5、验证 HH:mm:ss 格式的日期
String hms = "12:36:89";
System.out.println("hms: "+isLegalDate(hms.length(),hms,"HH:mm:ss"));
}
/**
* 根据时间 和时间格式 校验是否正确
* @param length 校验的长度
* @param sDate 校验的日期
* @param format 校验的格式
* @return
*/
public static boolean isLegalDate(int length, String sDate,String format) {
int legalLen = length;
if ((sDate == null) || (sDate.length() != legalLen)) {
return false;
}
DateFormat formatter = new SimpleDateFormat(format);
try {
Date date = formatter.parse(sDate);
return sDate.equals(formatter.format(date));
} catch (Exception e) {
return false;
}
}
}
下面是一个时间验证之后的截图
来源:https://blog.csdn.net/tanqingfu1/article/details/108615705
标签:Java,时间格式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java输入/输出流体系详解
2023-03-01 06:37:00
![](https://img.aspxhome.com/file/2023/9/65739_0s.png)
c# 颜色选择控件的实现代码
2022-04-27 07:22:58
![](https://img.aspxhome.com/file/2023/4/82234_0s.png)
Java截取字符串的方法
2023-11-07 09:30:02
java微信开发中的地图定位功能
2023-01-27 17:37:52
用JAVA实现单链表,检测字符串是否是回文串
2021-07-20 07:07:45
OpenCV和C++实现图像的翻转(镜像)、平移、旋转、仿射与透视变换
2023-07-14 23:47:22
![](https://img.aspxhome.com/file/2023/3/95523_0s.jpg)
Maven配置多仓库无效的解决
2023-11-29 04:37:10
![](https://img.aspxhome.com/file/2023/1/60681_0s.jpg)
C#使用回溯法解决背包问题实例分析
2023-11-22 20:21:22
java多次嵌套循环查询数据库导致代码中数据处理慢的解决
2023-10-28 22:17:50
Java实现XML文件学生通讯录
2023-07-23 19:21:49
java实现日期拆分的方法
2023-06-19 00:28:59
Java详细分析String类与StringBuffer和StringBuilder的使用方法
2022-04-23 15:23:30
![](https://img.aspxhome.com/file/2023/6/64946_0s.png)
SpringBoot入坑笔记之spring-boot-starter-web 配置文件的使用
2021-12-15 13:11:46
![](https://img.aspxhome.com/file/2023/0/78370_0s.png)
SpringBoot v2.2以上重复读取Request Body内容的解决方案
2022-06-27 11:00:16
解决WPF附加属性的Set函数不调用的问题
2022-09-27 09:52:38
Java 深入探究讲解简单工厂模式
2022-12-25 12:02:06
![](https://img.aspxhome.com/file/2023/7/83107_0s.png)
英语单词state与status的区别
2021-09-04 13:57:34
详解feign调用session丢失解决方案
2021-08-29 20:38:20
![](https://img.aspxhome.com/file/2023/1/82001_0s.png)
Jmeter环境搭建及安装步骤
2021-11-03 21:06:10
![](https://img.aspxhome.com/file/2023/6/81186_0s.png)
Java使用二分法进行查找和排序的示例
2023-03-16 10:16:37