Java时间处理的几个常见方法及问题

格式化时间

public class Test {
    public static void main(String[] args) {
        Date date = new Date();  //实例化日期
        String a = "yyyy-MM-dd HH:mm:ss";  //日期格式
        SimpleDateFormat ss = new SimpleDateFormat(a);  //用SimpleDateFormat 设置格式
        System.out.println(ss.format(date));  //输出日期
    }
}

SimpleDateFormat的线程不安全问题

SimpleDateFormat作为成员变量,而且一般写成静态的,被多个线程调用时,就会出现线程安全问题

当多个线程并发时,SimpleDateFormat字符串解析成日期就极有可能报错

public class SimpleDateFormateTest { 
    public static void main(String[] args) {
        final DateFormat df = new SimpleDateFormat("yyyyMMdd,HHmmss");
        ExecutorService ts = Executors.newFixedThreadPool(100);
        for (;;) {
            ts.execute(new Runnable() {
                public void run() {
                    try {
                        //生成随机数,格式化日期
                        String format =  df.format(new Date(Math.abs(new Random().nextLong())));
                        System.out.println(format);
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.exit(1);
                    }
                }
            });
        }
    }
}
public class Test2 extends Thread{
        private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        private String name;
        private String dateStr;
	public Test2(String name, String dateStr) {
            this.name = name;
            this.dateStr = dateStr;
        }
        @Override
        public void run() {
            try {
                Date date = sdf.parse(dateStr);
                System.out.println(name+": date:"+date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            ExecutorService executorService = Executors.newFixedThreadPool(3);
            executorService.execute(new Test2("A", "2019-09-15"));
            executorService.execute(new Test2("B", "2019-09-13"));
            executorService.shutdown();

    }
}

在这里插入图片描述
以上报错便是在多线程并发环境中SimpleDateFormat的线程不安全问题,为什么会产生这种问题呢?

private void initializeCalendar(Locale loc) {
        if (calendar == null) {
            assert loc != null;
            // The format object must be constructed using the symbols for this zone.
            // However, the calendar should use the current default TimeZone.
            // If this is not contained in the locale zone strings, then the zone
            // will be formatted using generic GMT+/-H:MM nomenclature.
            calendar = Calendar.getInstance(TimeZone.getDefault(), loc);
        }
    }

以上是SimpleDateFormat类的源码中的initializeCalender方法,Calendar是对象引用,可以存储SimpleDateFormat的信息,当SimpleDateFormat是静态static的,那么你创建的多个线程都会共用这个SimpleDateFormat,就相当于共用Calendar引用 . 所以多个线程并发时,就极有可能报类型转换的错误.

以下是几种解决方法:

  1. 将SimpleDateFormat定义为局部变量 (缺点:每次调用一次方法就会创建一个对象,用完就要回收)
  2. 给同步线程上锁synchronized 使线程同一时刻只会一个一个执行 (缺点:每次都必须等锁释放才执行其他线程,效率低)
  3. 用ThreadLocal本地线程,可有效解决此问题 , 一般使用此方法
  4. 使用DateUtils工具类处理(阿里规范)
private static final ThreadLocal<DateFormat> df = newThreadLocal<DateFormat>() {     
       @Override      
       protected DateFormatinitialValue() {
        return newSimpleDateFormat("yyyy-MM-dd");     
      } 
 };  
  1. 使用DateTimeFormatter代替SimpleDateFormat

获取当前时间

public class Ws {
    public static void main(String[] args){

        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        System.out.println("现在时间:" + sdf.format(date)); // 输出已经格式化的现在时间(24小时制)
    }
}

结果: 现在时间:2019-09-07 10:08:35 上午

根据生日获取当前年龄

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日");                
        //字符串变为时间Date类,解析p格式化f,pf        
        String birthday = "1995年05月02日";        
        Date d = sdf.parse(birthday);                
        //获得时间毫秒值        
        long myTime = d.getTime();        
        //当前日期毫秒值        
        long currentTime = new Date().getTime();        
        System.out.println((currentTime-myTime)/1000/60/60/24/365);  

使用Calendar获取年,月,日

 public class Calender {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println("当期时间: " + cal.getTime());
        System.out.println("日期: " + cal.get(Calendar.DATE));
        System.out.println("月份: " + (cal.get(Calendar.MONTH) + 1));
        System.out.println("年份: " + cal.get(Calendar.YEAR));
        System.out.println("一周的第几天: " + cal.get(Calendar.DAY_OF_WEEK));  // 星期日为一周的第一天输出为 1,星期一输出为 2,以此类推
        System.out.println("一月中的第几天: " + cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("一年的第几天: " + cal.get(Calendar.DAY_OF_YEAR));
    }
}

当期时间: Sat Sep 07 10:14:13 CST 2019
日期: 7
月份: 9
年份: 2019
一周的第几天: 7
一月中的第几天: 7
一年的第几天: 250

将时间戳转换成时间

public class Main{
        public static void main(String[] args){
            Long timeStamp = System.currentTimeMillis();  //获取当前时间戳
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));      // 时间戳转换成时间
            System.out.println("格式化结果:" + sd);
     
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒");
            String sd2 = sdf2.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
            System.out.println("格式化结果:" + sd2);
       }
    }

时间差值的计算

1.只计算两时间的差值天数

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化时间格式
        try
        {
            Date d1 = df.parse("2017-06-26 13:31:40");//定义两个时间
            Date d2 = df.parse("2019-09-07 11:30:24");
            long diff = Math.abs(d2.getTime() - d1.getTime());
            long days = diff / (1000 * 60 * 60 * 24); //只计算天数
            System.out.println(days);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

2.计算天数小时分钟秒数

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse("2017-06-26 10:31:40"); //用parse需要
        Date date = sdf.parse("2019-09-07 10:30:24");
        long l=Math.abs(date1.getTime()-date.getTime());//绝对值,计算两个时间的差值
        long day=l/(24*60*60*1000);
        long hour=(l/(60*60*1000)-day*24);
        long min=((l/(60*1000))-day*24*60-hour*60);
        long s=(l/1000-day*24*60*60-hour*60*60-min*60);
        System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值