首页 > 编程语言 > 详细

Java8-time常用API

时间:2020-09-11 16:16:20      阅读:36      评论:0      收藏:0      [点我收藏+]

Java8新特性-time常用API

LocalDate

	//获取当前系统时间表示的日期对象 默认格式是yyyy-MM-dd
        LocalDate date = LocalDate.now();
	//格式化LocalDate
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = date.format(fmt);
        System.out.println(date);
        //获取星期几
        System.out.println(date.getDayOfWeek().getValue());
        //根据日期构建一个特定的LocalDate对象
        LocalDate date1 = LocalDate.of(2020,8,10);
        //获取LocalDate对象中的年份 返回值为int
        System.out.println(date.getYear());
        //获取LocalDate对象中的月份,返回的枚举型英文单词
        System.out.println(date.getMonth());
        //获取LocalDate对象中的月份,返回是int值
        System.out.println(date.getMonthValue());
        //获取一年的第几天
        System.out.println(date.getDayOfYear());
        //获取一个月的第几天
        System.out.println(date.getDayOfMonth());
        //获取这个月的星期几 返回为枚举
        System.out.println(date.getDayOfWeek());
        //获取这个月的星期几
        System.out.println(date.getDayOfWeek().getValue());
        //在date基础上添加天数并返回,返回值为LocalDate类型
        System.out.println(date.plusDays(1));
        //在date后面拼接改日期午夜开始的时间,返回值为LocalDateTime
        System.out.println(date.atStartOfDay());

LocalTime

	//表示一个标准格式的时间,格式:HH:mm:ss.SSS显示
        LocalTime time = LocalTime.now();
        System.out.println(time);
        //获取时
        System.out.println(time.getHour());
        //获取分
        System.out.println(time.getMinute());
        //获取秒
        System.out.println(time.getSecond());
        //根据时间构建一个LocalTime对象
        LocalTime timer = LocalTime.of(12,8,10,111);
        System.out.println(timer);
        //在LocalTime上加1小时
        System.out.println(time.withHour(1));
        //在LocalTime上加20分钟
        System.out.println(time.withMinute(20));
        //在LocalTime上加40秒
        System.out.println(time.withSecond(40));
        //根据time返回从当天到该时间点经过的秒数
        System.out.println(time.toSecondOfDay());

LocaldateTime

        // LocalDateTime类用于表示一个标准日期时间格式,
        // 通常以yyyy-MM-ddTHH:mm:ss.SSS格式显示
        //(如:2020-08-07T18:18:18)
        LocalDateTime time = LocalDateTime.now();
        System.out.println(time);
        // 转成LocalDate
        LocalDate localDate = time.toLocalDate();
        System.out.println(localDate);
        // 转成LocalTime
        LocalTime localTime = time.toLocalTime();
        System.out.println(localTime);
        //根据指定日期构建一个LocalDateTime
        LocalDateTime times = LocalDateTime.of(2020,8,9,19,22,12);
        System.out.println(times);

DateTimeFormatter

/***
     * 注意:
     * DateTimeFormatter与DateFormat和SimpleDateFormat的区别在于,DateTimeFormatter是线程安全的实现,
     * 在多线程并发的时候可以让多个线程使用同一当前实例,能保证数据的一致性;但是DateFormat是线程非安全的实现,
     * 因此在多线程并发时,需要每个线程单独创建该实例。
     */
    public static void main(String[] args) {
        // DateTimeFormatter是jdk8新增Java.time包中的一个用于对LocalDate,LocalTime,LocalDateTime进行格式化和解析的解析类
        // 获取系统时间
        LocalDateTime dateTime = LocalDateTime.now();
        //创建一个格式化解析对象
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
        //方法一:
        String time = fmt.format(dateTime);
        //方法二:
        String time2 = dateTime.format(fmt);
        // 两种方式获取的字符串日期格式是一致的,均为:2020年08月10日 09时11分59秒
        System.out.println(time);
        System.out.println(time2);

        //将字符串根据格式转换成LocalDateTime对象
        String info = "2020年07月08日 09时33分21秒";
        dateTime = LocalDateTime.parse(info,fmt);
        System.out.println(dateTime);

        System.out.println(LocalDate.parse("20200708101211", 	DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
    }

Java8-time常用API

原文:https://www.cnblogs.com/miker-lcy/p/13650898.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!