ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java 8 日期操作

2022-01-26 10:04:31  阅读:220  来源: 互联网

标签:java System 日期 println 操作 now LocalDate final out


文章目录


java 8 的日期类已经出来很长一段时间了,一直以来也没怎么用过,最近用到了感觉太好用了,真香。 在这里插入图片描述

java 8 在java.time提供了很多日期、时间相关类可以使用,这些类都是线程安全的,而且使用起来比Date日期类方便很多,常用的应该就是LocalDate和LocalDateTime,LocalDate只保存年月日,LocalDateTime保存年月日时分秒。
Talk is cheap,show me the code。

LocalDate、LocalDateTime 和 Date互转

  • dateToLocalDate
   public static LocalDate dateToLocalDate(Date date) {
       Instant instant = date.toInstant();
       ZoneId zoneId = ZoneId.systemDefault();
       return instant.atZone(zoneId).toLocalDate();;
   }
  • dateToLocalDateTime
	public static LocalDateTime dateToLocalDateTime(final Date date){
        return date.toInstant().atZone( ZoneId.systemDefault() ).toLocalDateTime();
    }
  • localDateToDate
	public static Date localDateToDate(final LocalDate localDate){
        return Date.from( localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
  • localDateTimeToDate
	public static Date localDateTimeToDate(final LocalDateTime localDateTime){
        return Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
    }

LocalDate、LocalDateTime格式化

java 8 之前格式化java.util.Date都是用java.text.SimpleDateFormat类,java 8开始如果格式化LocalDate、LocalDateTime要使用java.time.format.DateTimeFormatter类。
LocalDateTime的api跟LocalDate大多数是相同的,LocalDate格式化的方式同样适用于LocalDateTime。
LocalDate.toString()的默认格式是yyyy-MM-dd

localDateToString:

	public static String localDateToString(LocalDate localDate , DateTimeFormatter dateTimeFormatter){
        return localDate.format(dateTimeFormatter);
    }
  • stringToLocalDate
	public static LocalDate stringToLocalDate(String str){
        return LocalDate.parse(str);
    }

	public static LocalDate stringToLocalDate(String str , DateTimeFormatter dateTimeFormatter ){
        return LocalDate.parse(str , dateTimeFormatter );
    }

有关日期的格式化可以看JAVA 日期格式化

示例

final LocalDate now = LocalDate.now();

        System.out.println("今天是 "+now);

        System.out.println("1970年到现在一共 "+now.toEpochDay() +" 天");

        final int lengthOfYear = now.lengthOfYear();
        System.out.println("今年一共 "+lengthOfYear+" 天");

        final int lengthOfMonth = now.lengthOfMonth();
        System.out.println("本月一共 "+ lengthOfMonth +" 天");

        final boolean leapYear = now.isLeapYear();
        System.out.println("今年是否是闰年:"+leapYear);

        final LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("本月的第一天是 : "+firstDayOfMonth);

        //下一个周一
        final LocalDate withMONDAY = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println("下周一日期是 :"+withMONDAY);

        System.out.println(" 日期在当前时间之后: "+ withMONDAY.isAfter(now));
        System.out.println(" 日期在当前时间之前: "+ withMONDAY.isBefore(now));

        //最后一个周一
        final LocalDate lastMONDAY = now.with(TemporalAdjusters.lastInMonth(DayOfWeek.TUESDAY));
        System.out.println("本月最后一个周二是 :"+lastMONDAY);

        final LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("本月最后一天是 : "+lastDay);

        // 加一年
        final LocalDate plusYears = now.plusYears(1);
        System.out.println("当前日期加一年 : "+plusYears);

        //两个日期相差天数
        System.out.println("两个日期相差天数:"+(plusYears.toEpochDay() - now.toEpochDay()));

        final LocalDate plusMonths1 = now.plusMonths(12);
        System.out.println("当前日期加12 个月 :"+plusMonths1);

        final LocalDate minusDays = now.minusDays(1);
        System.out.println("当前日期减 1 天 : "+minusDays);

        final LocalDate plusDays = now.plusDays(1);
        System.out.println("当前日期加 1 天 : "+plusDays);

        final int dayOfMonth = now.getDayOfMonth();
        System.out.println("今天是这个月的第 "+dayOfMonth +" 天");
        final int monthValue = now.getMonthValue();
        System.out.println("本月是今年的第  "+monthValue + "月");
        final Month month = now.getMonth();
        System.out.println("本月的英文 : "+month);

        // 本周的周几
        final DayOfWeek dayOfWeek = now.getDayOfWeek();
        System.out.println("今天是周几英文: " + dayOfWeek);
        System.out.println("今天是本周周几: " + dayOfWeek.getValue());

        // string 转 localDate
        final LocalDate parse = LocalDate.parse("2021-07-12");
        final LocalDate parse1 = LocalDate.parse("2021-07-12", DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        System.out.println(parse1);
        System.out.println(" 转日期 "+parse);
        System.out.println("DateTimeFormatter 转日期 "+parse1);

        //获取指定日期
        final LocalDate startDate = LocalDate.of(2021 , 6, 30);
        System.out.println(startDate);

        final LocalDateTime nowDateTime = LocalDateTime.now();
        System.out.println("当前日期时间:"+nowDateTime);

        final LocalTime localTime = LocalTime.now();
        System.out.println("当前时间: "+localTime);
        final String format = nowDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a"));
        System.out.println("当前日期时间 格式化"+format);

结果:

今天是 2022-01-25
1970年到现在一共 19017 天
今年一共 365 天
本月一共 31 天
今年是否是闰年:false
本月的第一天是 : 2022-01-01
下周一日期是 :2022-01-31
 日期在当前时间之后: true
 日期在当前时间之前: false
本月最后一个周二是 :2022-01-25
本月最后一天是 : 2022-01-31
当前日期加一年 : 2023-01-25
两个日期相差天数:365
当前日期加12 个月 :2023-01-25
当前日期减 1 天 : 2022-01-24
当前日期加 1 天 : 2022-01-26
今天是这个月的第 25 天
本月是今年的第  1月
本月的英文 : JANUARY
今天是周几英文: TUESDAY
今天是本周周几: 2
2021-07-12
 转日期 2021-07-12
DateTimeFormatter 转日期 2021-07-12
2021-06-30
当前日期时间:2022-01-25T18:37:57.652
当前时间: 18:37:57.652
当前日期时间 格式化2022-01-25 18:37:57 下午

java 8 日期 操作还有很多api,感兴趣的可以自己多尝试一下。
end

能力一般,水平有限,如有错误,请多指出。
如果对你有用点个关注给个赞呗

标签:java,System,日期,println,操作,now,LocalDate,final,out
来源: https://blog.csdn.net/qq_39654841/article/details/122682613

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有