SimpleDateFormat.setLenient(bool flag);是一个可自动判定日期是否合法的方法,在使用过程中很方便;
使用方法例如:输入“2015.10.33”在SimpleDateFormat.setLenient(true);的时候可以输出;而在SimpleDateFormat.setLenient(false);抛出异常。
package date; import java.text.SimpleDateFormat; /** * Created by taoyongpan on 2016/10/12. */ public class JudgeDate { public static void main(String[]args) { String str="2007-5-32"; if(isDate(str,"yyyy.MM.dd")|isDate(str,"yyyy-MM-dd")) System.out.print(str); else System.out.println("This is wrong"); } public static boolean isDate(String str_input,String rDateFormat){ if (!isNull(str_input)) { SimpleDateFormat format = new SimpleDateFormat(rDateFormat); format.setLenient(false); try { format.format(format.parse(str_input)); } catch (Exception e) { return false; } return true; } return false; } public static boolean isNull(String str){ if(str==null) return true; else return false; } } 输出结果是“This is wrong”
package date; import java.text.SimpleDateFormat; /** * Created by taoyongpan on 2016/10/12. */ public class JudgeDate { public static void main(String[]args) { String str="2007-5-32"; if(isDate(str,"yyyy.MM.dd")|isDate(str,"yyyy-MM-dd")) System.out.print(str); else System.out.println("This is wrong"); } public static boolean isDate(String str_input,String rDateFormat){ if (!isNull(str_input)) { SimpleDateFormat format = new SimpleDateFormat(rDateFormat); format.setLenient(true); try { format.format(format.parse(str_input)); } catch (Exception e) { return false; } return true; } return false; } public static boolean isNull(String str){ if(str==null) return true; else return false; } }
输出结果是“2007-5-32”; 综上 ,我们使用的过程中,要设置为false;
相关推荐
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
`SimpleDateFormat.js`这个文件很可能包含了上述`SimpleDateFormat`类的完整实现,包括对各种日期格式的支持,如年、月、日、小时、分钟、秒等,并且处理了闰年、时区等复杂情况。在实际使用中,你可以这样使用它: ...
System.out.println(simpleDateFormat.format(date)); } }); } } } ② 使用 synchronized 加锁执行 当多个线程共享同一个 SimpleDateFormat 实例时,可以通过 synchronized 关键字进行加锁,确保同一时刻只有...
Date parse = simpleDateFormat.parse("2019-09-08"); //字符串转日期 String format = simpleDateFormat.format(parse); //日期转字符串 System.out.println(parse +"fsgfsf"+format ); cal.setTime(parse); ...
例如,我们可以使用`Calendar.add()`方法增加日期,再用`SimpleDateFormat.format()`方法输出格式化的日期。 下面是一个简单的示例,展示了如何使用`SimpleDateFormat`和`Calendar`: ```java import java.text....
### SimpleDateFormat的常用方法说明 #### 一、简介 `SimpleDateFormat`是Java中用于格式化日期和时间的一个类。它允许我们自定义日期/时间的显示格式,这在实际开发中非常有用,尤其是在处理不同地区或语言环境下...
- 再通过 `simpleDateFormat.parse()` 方法将字符串解析回 `Date` 对象。 #### 总结 此示例代码展示了如何使用 Java 标准库中的 `Date`, `Calendar`, 和 `SimpleDateFormat` 类来执行基本的日期时间操作。这些...
simpleDateFormat.setLenient(false); webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true)); } } ``` 在这个例子中,我们创建了一个`CustomDateEditor`,用于自定义...
- 当需要将字符串转换为日期对象时,可以使用`SimpleDateFormat.parse()`方法。 - 示例: ```java SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date date = sdf.parse("25/04/2003"); // 将...
计算机后端-Java-Java核心基础-第22章 常用类 10. SimpleDateFormat的使用.avi
描述中提到了几个相关的文件,如`date.format.js`, `DateFormat.js`, `Format.js`, `SimpleDateFormat.js`, `jsjava_2_0`, `jsjava_2_2_bin`,这些文件可能是不同的JavaScript库或函数,用于实现日期和时间的格式化...
3. SimpleDateFormat 可以使用 applyPattern 方法修改格式化方式。 SimpleDateFormat 的实例 在实际应用中,我们可以使用 SimpleDateFormat 来实现日期和时间的格式化和解析。例如,在导出数据库数据到 excel 文件...
要解决SimpleDateFormat类的线程安全问题,有多种方法可以选择: 1. 使用ThreadLocal: 可以使用ThreadLocal将SimpleDateFormat对象封装在ThreadLocal中,这样每个线程都有自己的SimpleDateFormat对象,从而避免了...
SimpleDateFormat 还提供了其他一些有用的方法,例如 applyPattern 方法,可以用于修改格式化方式。例如: ```java SimpleDateFormat myFmt = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒"); ...
可以使用System类的currentTimeMillis()方法获取当前系统的毫秒值,然后传递给Date类的构造方法来创建一个表示当前时间的Date对象。 【Calendar类】 Calendar是Java中的一个抽象类,它是Date类的替代品,提供了更...
`setLenient(false)`方法将解析器设置为非宽容模式,这意味着它会严格检查日期的合法性,例如2月不能有30天或31天(除非是闰年的2月29日)。 `parse`方法尝试将日期字符串转换为`Date`对象。如果日期字符串符合格式...
在Java编程语言中,`SimpleDateFormat` 是一个非常重要的日期和时间格式化工具类,它允许程序员以特定的模式来解析和格式化日期。当我们需要从系统获取当前时间并按照自定义格式显示时,`SimpleDateFormat` 就派上了...
例如,你可以定义一个模式如"yyyy-MM-dd HH:mm:ss",然后用`SimpleDateFormat.format(Date)`和`SimpleDateFormat.parse(String)`方法进行转换。 在时间预测方面,我们可以使用`Calendar`或`java.time`包中的`Period...
我们可以使用`java.io.File`类提供的方法来实现这一功能。以下是对标题和描述中涉及的知识点的详细说明: ### 文件类(File)介绍 `File`类是Java I/O系统的基础,它用于表示文件和目录路径名。`File`对象可以代表...