输入“33/12/2011”,用SimpleDateFormat parse()方法,转化为Date(2012,01,02).这样处理相当“33/12/2011”是正常输入,如果需要"33/12/2011”报错,即把"33/12/2011"当作错误格式,刚开始自己写了段逻辑判断:
把转成的日期再反转回来,再比较是否一致,即使用format方法再转换成字符串,和传入的那个串作比较,如果不相等,则证明传入的那个日期格式是错误的
private String getDestDateStrFromSrcDateStr(String dateStr, String srcDateFormat, String descDateFormat) { try { final SimpleDateFormat src_sdf = new SimpleDateFormat(srcDateFormat); final Date date = src_sdf.parse(dateStr); //把转成的日期再反转回来,再比较是否一致 if (srcDateFormat.length() != dateStr.length() || !dateStr.equals(src_sdf.format(date))) { LOGGER.error("the src date format is {} , but input the date string value is {}, input illegal", srcDateFormat, dateStr); throw new ParseMessageException( ErrorKeys.PAYMENT_AG_CONFIG_SERVICE_PARAM_VALIDATE_FAILED); } //把转成的date类型转换为目标格式日期字符串(yyyyMMdd) final SimpleDateFormat dest_sdf = new SimpleDateFormat( descDateFormat); LOGGER.info("the converted dest date str:{}" + dest_sdf.format(date)); return dest_sdf.format(date); } catch (java.text.ParseException e) { LOGGER.error("the src date format is {} , but input the date string value is {}, input illegal", srcDateFormat, dateStr); throw new ParseMessageException( ErrorKeys.PAYMENT_AG_CONFIG_SERVICE_PARAM_VALIDATE_FAILED); } }
总觉得这种方法显得很笨拙,后来找找API,发现有一个方法:setLenient(false)可以直接使用。哎~何必费这么大劲呢
测试方法:
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateTest { public static void main(String[] args) throws ParseException { DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); format.setLenient(false); Date date = format.parse("33/12/2011"); System.out.println(date); } }
该方法的作用:
setLenient用于设置Calendar是否宽松解析字符串,如果为false,则严格解析;默认为true,宽松解析
相关推荐
JAVA:日期字符串与日期互相转换 (日期类型转字符串,字符串转日期格式) SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
检查日期格式字符串是否能转化成日期,字符串可通过new SimpleDateFormat("xxxx");得到日期字符串
一、日期格式化, 以及字符串转换成日期 主要用到 SimpleDateFormat 类实现日期的格式化,以及日期字符串的解析。 示例: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // ...
判断字符串是否为日期字符串 获取日期字符串的日期风格。失敗返回null。 将日期字符串转化为日期。失败返回null。 将日期字符串转化为另一日期字符串。失败返回null。 增加日期的年份。失败返回null。 增加...
由浅入深解析 SimpleDateFormat 由浅入深解析 SimpleDateFormat
日期操作。。。基础的SimpleDateFormat格式化日期!!操作!》初级学习代码
Java 实例 - 格式化时间SimpleDateFormat使用源代码-详细教程.zip
时间格式化 使用举例: var fmt = SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); var aTime = fmt.parse("2020-12-31 23:59:59");//Date 类型 var strTime = fmt.format(aTime);//String 类型
java代码-SimpleDateFormat YYYY解析问题
编写一个方法参数接收一个字符串,返回一个Date对象(在多种日期格式中找到与字符串匹配的那一个)用到解析异常ParseException import java.text.ParseException; import java.text.SimpleDateFormat; import java....
* 使用预设格式提取字符串日期 * @param strDate 日期字符串 * @return */ public static Date parse(String strDate) { return parse(strDate,DATE_FULL_STR); } /** * 使用用户格式提取字符串...
主要为大家详细介绍了Java SimpleDateFormat中英文时间格式化转换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
目录SimpleDateFormat诡异bug复现SimpleDateFormat诡异bug字符串日期转Date日期(parse)Date日期转String类型(format)SimpleDateFormat出现bug的原因如何解决SimpleDateFormat多线程安全问题局部变量使用...
高并发之-SimpleDateFormat类的线程安全问题和解决方案.docx
* 日期和时间格式由日期和时间模式字符串指定。在日期和时间模式字符串中,未加引号的字母 'A' 到 'Z' 和 'a' 到 'z' * 被解释为模式字母,用来表示日期或时间字符串元素。文本可以使用单引号 (') 引起来,以免...
1.创建SimpleDateFormat对象,确定日期被格式化的格式 2.使用循环,在循环中调用Thread的sleep方法,让线程休眠1s后打印当前时间的字符串
import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[] args) { //当前日期和时间 Date date=new Date(); System.out.println(date); //将...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date.setTime(time); System.out.println(sdf.format(date)); 发现时间于想要的时间不符,请运行Time.reg文件
NULL 博文链接:https://chaoyi.iteye.com/blog/2082317
DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat类完成,下面我们具体来看一下两个类的用法: