java 8 日期操作

2022/1/26 11:05:10

本文主要是介绍java 8 日期操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • LocalDate、LocalDateTime 和 Date互转
  • LocalDate、LocalDateTime格式化
  • 示例

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 8 日期操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程