java如何验证时间合法性
这里推荐用我常用两种方式:
1. 运用数组来进行时间合法校验
public class test {//各个月中最大天数private static int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println(judge(sc.nextInt(), sc.nextInt(), sc.nextInt()));}//判断日期合法性public static boolean judge(int year, int month, int day) {//首先判断月份是否合法if (month >= 1 && month <= 12) {//判断是否为闰年if ((year % 100 == 0 && year % 400 == 0) || year % 4 == 0) {//判断当前月份是否为2月,因为闰年的2月份为29天if (month == 2 && day <= 29) return true;else {if (day <= days[month - 1]) return true;}} else {if (day <= days[month - 1]) return true;}}return false;}}
2. 通过java8的特性来进行时间合法校验
public class test2 {/*** 验证字符串是否为指定日期格式** @param oriDateStr 待验证字符串* @param pattern 日期字符串格式, 例如 "yyyy-MM-dd"* @return 有效性结果, true 为正确, false 为错误*/public static boolean dateStrIsValid(String oriDateStr, String pattern) {if (StringUtils.isBlank(oriDateStr) || StringUtils.isBlank(pattern)) {return false;}SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);try {Date date = dateFormat.parse(oriDateStr);return oriDateStr.equals(dateFormat.format(date));} catch (ParseException e) {return false;}}
}
PS:在test2中是通过SimpleDateFormat函数来进行判断的,但是SimpleDateFormat是线程不安全的,如要考虑到线程安全性则可以用DateTimeFormat。(参考下面代码)
public static boolean legalDate(String oriDateStr, String pattern, int type) {if (StringUtils.isBlank(oriDateStr) || StringUtils.isBlank(pattern)) {return false;}DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);//判断日期if (type == 1) {LocalDate parse = LocalDate.parse(oriDateStr, df);return oriDateStr.equals(df.format(parse));} else {//精确到时间LocalDateTime parse = LocalDateTime.parse(oriDateStr, df);return oriDateStr.equals(df.format(parse));}}
谢谢观看,走过路过点个小赞!:p
java如何验证时间合法性相关推荐
- Java 中验证时间格式的 4 种方法
大家好,今天咱们来讲一下,Java 中如何检查一个字符串是否是合法的日期格式? 为什么要检查时间格式? 后端接口在接收数据的时候,都需要进行检查.检查全部通过后,才能够执行业务逻辑.对于时间格式,我们 ...
- Java验证时间格式是否正确
Java验证时间格式是否正确 /*** @author * @Description 时间格式校验* @Version 1.0* @since */ public class IsLegalDate ...
- java验证身份证合法性_Java安全性,第2部分:身份验证和授权
关于本教程 本教程是关于什么的? 也许没有比应用程序安全更重要的软件工程主题. 攻击是昂贵的,无论是来自内部还是外部,而且某些攻击可能会使软件公司承担赔偿责任. 随着计算机(尤其是Internet)技 ...
- Java 正则表达式验证小结
为什么80%的码农都做不了架构师?>>> package com.fsti.icop.util.regexp;import java.util.regex.Matcher; i ...
- Google Authenticator:将其与您自己的Java身份验证服务器配合使用
用于移动设备的Google Authenticator应用程序是一个非常方便的应用程序,它实现了TOTP算法(在RFC 6238中指定). 使用Google Authenticator,您可以生成时间 ...
- socketserver和socket的补充(验证客户端合法性)
一.socket的补充 1.参数 socket.socket(family=AF_INET,type=SOCK_STREAM,proto=0,fileno=None) 参数说明: family 地址系 ...
- Java中的时间和日期(上)
自从JDK 1.0开始,Java就提供了Date来处理时间和日期,作为老古董自然有很多东西是过时的.然后出现了Calendar来解决了很多问题,但是Calendar使用比较复杂,并且有些反人类的地方. ...
- java 正则表达式验证
package com.fsti.icop.util.regexp; import java.util.regex.Matcher; import java.util.regex.Pattern; p ...
- Java密码验证合格程序
密码要求: 1.长度超过8位 2.包括大小写字母.数字.其它符号,以上四种至少三种 3.不能有相同长度超2的子串重复 说明:长度超过2的子串 输入描述: 一组或多组长度超过2的子符串.每组占一行 输出 ...
最新文章
- SPOJ ATOMS - Atoms in the Lab
- 计算机二级中的html那道题,计算机等级考试题库:这10道二级C++语言试题是个坑,你居然不知道...
- Linux socket 网络编程常用函数总结
- 服务器资源数据结果汇总
- 算法竞赛入门与进阶 (三)贪心
- 密码学系列之:IDEA
- Quick cocos2dx学习笔记
- 单线驱动74hc595(转)
- 【linux】io_uring 和 eBPF 将如何彻底改变 Linux 中的编程
- 多核Cache一致性 伪共享 atomic的实现和cache相关的部分
- Dotween常用方法详解
- Beanshell相关
- centOS安装vnc远程桌面
- Cityscape数据集脚本readme翻译
- tiny-emitter 源码解析
- H3C交换机如何查环路
- 【DFS专题训练】踏青 C++程序题 连通块问题
- SCAU Java 实验7 银行账户存取款业务
- 基于FFmpeg的视频播放器之一:开篇
- 2D激光SLAM-雷达的特征点提取