SimpleDateFormat的部分方法

1,语法格式:new SimpleDateFormat("yyyy-MM-dd HH:mm")

  parse:String-->Date

  format:Date-->String

2,SimpleDateFormat的setLenient(true/false)方法

  自动计算时间格式是否正确

  

public void main(){
  String dateStr="2015/13/10";
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
  sdf.setLenient(true);//自动验证时间格式,13个月就是一年以后的1月
  sdf.parse(dateStr);
  System.out.println(dateStr);
}

输出结果:2016/1/10

如果把sdf.setLenient(false);则控制台直接报错

时间: 2024-08-10 07:30:46

SimpleDateFormat的部分方法的相关文章

SimpleDateFormat 的 format 方法使用具体解释

Java中怎么才干把日期转换成想要的格式呢.或把字符串转换成一定格式的日期,如把数据库中的日期或时间转换成自己想要的格式,JAVA中提供了SimpleDateFormat类能够实现,下面是SimpleDateFormat的使用方法及实例: java.lang.Object java.text.Format java.text.DateFormat java.text.SimpleDateFormat 全部已实现的接口: Serializable, Cloneable SimpleDateForm

SimpleDateFormat 的 format 方法使用详解

Java中怎么才能把日期转换成想要的格式呢,或把字符串转换成一定格式的日期,如把数据库中的日期或时间转换成自己想要的格式,JAVA中提供了SimpleDateFormat类可以实现,以下是SimpleDateFormat的用法及实例: java.lang.Object java.text.Format java.text.DateFormat java.text.SimpleDateFormat 所有已实现的接口: Serializable, Cloneable SimpleDateFormat

SimpleDateFormat线程不安全及解决的方法

一. 为什么SimpleDateFormat不是线程安全的? Java源代码例如以下: /** * Date formats are not synchronized. * It is recommended to create separate format instances for each thread. * If multiple threads access a format concurrently, it must be synchronized * externally. */

Java日期时间操作的一些方法

1. 获得Calendar实例: Calendar c = Calendar.getInstance(); 2. 定义日期/时间的格式: SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 3. 把日期/时间转换成固定格式,使用SimpleDateFormat的format()方法: String datetime = sdf.format(c.getTime()); 4. 把字符串转换成日期/时间,使用

SimpleDateFormat非线程安全及解决办法

一. 为什么SimpleDateFormat不是线程安全的? Java源码如下: /** * Date formats are not synchronized. * It is recommended to create separate format instances for each thread. * If multiple threads access a format concurrently, it must be synchronized * externally. */ pu

ThreadLocal解决SimpleDateFormat多线程安全问题中遇到的困惑

测试代码: public class Main { public static void main(String[] args) { for (int k = 0; k < 10; k++) { Runnable target = new Runnable() { @Override public void run() { Object obj = dateFormatter.get(); System.out.println(Thread.currentThread().getName() +

SimpleDateFormat并发隐患及其解决

此文已由作者姚太行授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. SimpleDateFormat被大量使用于处理时间格式化过程,由于该类在创建时会指定一个pattern用于标明固定的时间格式,所以在使用中,一般会创建一个作用域较大(static修饰或某类的私有属性)的对象用于重复使用.由于时间转换过程遇到的多线程并发的使用场景并不多见,所以很难发现在该类的隐患,事实上,该类并非是线程安全的,在多线程使用format()和parse()方法时可能会遇到问题. 分析 在S

为什么阿里巴巴禁止把SimpleDateFormat定义为static类型的?

在日常开发中,我们经常会用到时间,我们有很多办法在Java代码中获取时间.但是不同的方法获取到的时间的格式都不尽相同,这时候就需要一种格式化工具,把时间显示成我们需要的格式.最常用的方法就是使用SimpleDateFormat类.这是一个看上去功能比较简单的类,但是,一旦使用不当也有可能导致很大的问题.在阿里巴巴Java开发手册中,有如下明确规定: 那么,本文就围绕SimpleDateFormat的用法.原理等来深入分析下如何以正确的姿势使用它.SimpleDateFormat用法SimpleD

项目中 SimpleDateFormat 的正确使用

项目中 SimpleDateFormat 的正确使用 日常开发中,我们经常需要使用时间相关类,说到时间相关类,想必大家对 SimpleDateFormat 并不陌生.主要是用它进行时间的格式化输出和解析,挺方便快捷的,但是 SimpleDateFormat 并不是一个线程安全 的类.在多线程情况下,会出现异常,想必有经验的小伙伴也遇到过.下面我们就来分析分析SimpleDateFormat为什么不安全?是怎么引发的?以及多线程下有那些SimpleDateFormat的解决方案? 先看看<阿里巴巴