LocalDateTime和Date的比较与区别

通过本篇文章了解

             

  • 为什么需要LocalDate、LocalTime、LocalDateTime【java8新提供的类】
  • java8新的时间API的使用方式,包括创建、格式化、解析、计算、修改

 

为什么需要LocalDate、LocalTime、LocalDateTime

Date如果不格式化,打印出的日期可读性差

Tue Sep 10 09:34:04 CST 2019

使用SimpleDateFormat对时间进行格式化,但SimpleDateFormat是线程不安全的 SimpleDateFormat的format方法最终调用代码:

private StringBuffer format(Date date, StringBuffer toAppendTo,
                              FieldDelegate delegate) {
        // Convert input date to time field list
        calendar.setTime(date);

        boolean useDateFormatSymbols = useDateFormatSymbols();

        for (int i = 0; i < compiledPattern.length; ) {
            int tag = compiledPattern[i] >>> 8;
            int count = compiledPattern[i++] & 0xff;
            if (count == 255) {
                count = compiledPattern[i++] << 16;
                count |= compiledPattern[i++];
            }

            switch (tag) {
            case TAG_QUOTE_ASCII_CHAR:
                toAppendTo.append((char)count);
                break;

            case TAG_QUOTE_CHARS:
                toAppendTo.append(compiledPattern, i, count);
                i += count;
                break;

            default:
                subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
                break;
            }
        }
        return toAppendTo;
    }

calendar是共享变量,并且这个共享变量没有做线程安全控制。当多个线程同时使用相同的SimpleDateFormat对象【如用static修饰的SimpleDateFormat】调用format方法时,多个线程会同时调用calendar.setTime方法,可能一个线程刚设置好time值另外的一个线程马上把设置的time值给修改了导致返回的格式化时间可能是错误的。

在多并发情况下使用SimpleDateFormat需格外注意
SimpleDateFormat除了format是线程不安全以外,parse方法也是线程不安全的。parse方法实际调用alb.establish(calendar).getTime()方法来解析,alb.establish(calendar)方法里主要完成了

  • 重置日期对象cal的属性值
  • 使用calb中中属性设置cal
  • 返回设置好的cal对象
    但是这三步不是原子操作

多线程并发如何保证线程安全

  • 避免线程之间共享一个SimpleDateFormat对象,每个线程使用时都创建一次SimpleDateFormat对象 => 创建和销毁对象的开销大
  • 对使用format和parse方法的地方进行加锁 => 线程阻塞性能差
  • 使用ThreadLocal保证每个线程最多只创建一次SimpleDateFormat对象 => 较好的方法

Date对时间处理比较麻烦,比如想获取某年、某月、某星期,以及n天以后的时间,如果用Date来处理的话真是太难了,你可能会说Date类不是有getYear、getMonth这些方法吗,获取年月日很Easy,但都被弃用了啊

一起使用java8全新的日期和时间API

 

创建LocalDate 只获取某年某月


//获取当前年月日
LocalDate localDate = LocalDate.now();
System.out.println("当前的年月日:"+localDate);
//构造指定的年月日
LocalDate localDate1 = LocalDate.of(2019, 9, 10);
System.out.println("指定的年月日:"+localDate1);
 

  当前的年月日:2020-01-13
  指定的年月日:2019-09-10

 

获取年、月、日、星期几

LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int year1 = localDate.get(ChronoField.YEAR);
System.out.println("当前年:"+year);
System.out.println("当前年:"+year1);
Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
System.out.println("当前月:"+month);
System.out.println("当前月:"+month1);
int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
System.out.println("当前天:"+day);
System.out.println("当前天:"+day1);
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
System.out.println("当前星期:"+dayOfWeek);
System.out.println("当前星期:"+dayOfWeek1);


当前年:2020
当前年:2020
当前月:JANUARY
当前月:1
当前天:13
当前天:13
当前星期:MONDAY
当前星期:1

获取时分秒

  创建LocalTime

  LocalTime localTime = LocalTime.of(13, 51, 10);
        LocalTime localTime1 = LocalTime.now();
        System.out.println("当前时间:"+localTime);
        System.out.println("当前时间:"+localTime1);

        //获取时分秒
        //获取小时
        int hour = localTime.getHour();
        int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
        System.out.println("当前小时:"+hour);
        System.out.println("当前小时:"+hour1);
        //获取分
        int minute = localTime.getMinute();
        int minute1 =             localTime.get(ChronoField.MINUTE_OF_HOUR);
        System.out.println("当前分钟:"+minute);
        System.out.println("当前分钟:"+minute1);
        //获取秒
        int second = localTime.getMinute();
        int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
        System.out.println("当前秒:"+second);
        System.out.println("当前秒:"+second1);



    当前时间:13:51:10
    当前时间:10:08:13.687
    当前小时:13
    当前小时:13
    当前分钟:51
    当前分钟:51
    当前秒:51
    当前秒:10

LocalDateTime

获取年月日时分秒,等于LocalDate+LocalTime

创建LocalDateTime

LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
LocalDateTime localDateTime3 = localDate.atTime(localTime);
LocalDateTime localDateTime4 = localTime.atDate(localDate);

 

 

还有一些对时间的操作api 相当于之前的旧API简单了很多,

和SimpleDateFormat相比,DateTimeFormatter是线程安全的

 

 

 

个人公众号:

 

 

posted @ 2020-01-13 10:14  彭阿三  阅读(24024)  评论(0编辑  收藏  举报