2020了你还不会Java8新特性?(九)时间日期API&&Java8总结

joda-time

在开始学习jdk8.time之前,先接触一下joda-time。

public static void main(String[] args) {
    // 基本使用方式.
    DateTime today = new DateTime();
    DateTime dateTime = today.plusDays(1);
    //今天
    System.out.println(today.toString("yyyy-MM-dd"));
    //明天
    System.out.println(dateTime.toString("yyyy-MM-dd"));
    System.out.println("- - - - -");
    //当月的第一天
    DateTime dateTime1 = today.withDayOfMonth(1);
    System.out.println(dateTime1.toString("yyyy-MM-dd"));
    // 当前时间后边三个月的第后一天的日期
    LocalDate localDate = new LocalDate();
    localDate = localDate.plusMonths(3).dayOfMonth().withMaximumValue();
    System.out.println(localDate);
    // 当前时间后边三个月的第一天的日期
    localDate = localDate.plusMonths(3).dayOfMonth().withMinimumValue();
    System.out.println(localDate);
    //计算两年前的第三个月的最后一天的时期
    DateTime localDate1 = new DateTime();
    localDate1.minusYears(2).monthOfYear().setCopy(3).dayOfMonth().withMaximumValue();
    System.out.println(localDate1);
}
  • example:
public class JodaTest2 {
    // 标准UTC时间. 转换成日期类型  2014-11-11T02:22:22.222z

    public static Date to2c(String date) {
        //服务器端转换成客户端的时间
        DateTime parse = DateTime.parse(date, DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
        return parse.toDate();
    }

    public static String toString(Date date) {
//        客户端的时间转成服务器的时间
        DateTime date1 = new DateTime(date, DateTimeZone.UTC);
        return date1.toString();
    }

    public static String date2String(Date date,String dateFort) {
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(dateFort);
    }

    public static void main(String[] args) {
        System.out.println(JodaTest2.to2c("2014-11-11T02:22:22.222z"));
        System.out.println(JodaTest2.toString(new Date()));  // 标准的时间为 差8个小时
        System.out.println(JodaTest2.date2String(new Date(),"yyyy-MM-dd"));

    }
}

Java中日期时间的api

Java8中的所有时间都是不可变的,确保了线程安全。

没有必要去研究源代码。会用就可以了。省下时间去学习更重要,更有价值的事情上。

{
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        //获取年
        System.out.println(localDate.getYear());
        //获取月
        System.out.println(localDate.getMonthValue());

        //根据年月日构造
        LocalDate localDate1 = LocalDate.of(2030, 3, 22);
        System.out.println(localDate1);

        //根据是时分秒构造
        LocalDate localDate2 = LocalDate.of(2020,3,25);
        MonthDay monthDay = MonthDay.of(localDate2.getMonth(), localDate2.getDayOfMonth());

        //根据是时分秒构造
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);

        // + 20分钟, -2个小时
        LocalTime localTime1 = localTime.plusMinutes(20).minusHours(2);
        System.out.println(localTime1);

        System.out.println("- - - - -");

        //现在的时间增加两周                                    (增加的长度,增加的单位)
        LocalDate localDate3 = LocalDate.now().plus(2, ChronoUnit.WEEKS);
        System.out.println(localDate3);

        //现在的时间减两周
        LocalDate localDate4 = localDate.minus(2, ChronoUnit.MONTHS);
        System.out.println(localDate4);

        // Clock对象
        Clock clock = Clock.systemDefaultZone();
        System.out.println(clock);

        // 两个日期进行的判断
        LocalDate localDate5 = LocalDate.now();
        LocalDate localDate6 = LocalDate.of(2020,1,21);
        System.out.println(localDate5.isBefore(localDate6));
        System.out.println(localDate5.isAfter(localDate6));
        System.out.println(localDate5.equals(localDate6));

        //关于时区的概念.
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        availableZoneIds.forEach(System.out::println);

        //将上边的无序的时区set进行排序
        Set treeSet = new TreeSet<String>(){
            {addAll(availableZoneIds);}
        };
        treeSet.stream().forEach(System.out::println);

        //使用时区做一些例子.
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
        System.out.println(zonedDateTime);

        System.out.println("- - -- - -");

         // 年月的对象
        YearMonth yearMonth = YearMonth.now();
        System.out.println(yearMonth);
        System.out.println(yearMonth.lengthOfMonth());
        System.out.println(yearMonth.isLeapYear());

        YearMonth yearMonth1 = YearMonth.of(2019, 2);
        System.out.println(yearMonth1);
        System.out.println(yearMonth1.lengthOfMonth());
        System.out.println(yearMonth1.lengthOfYear());
        System.out.println(yearMonth1.isLeapYear()); // 是否闰年

        LocalDate localDate7 = LocalDate.now();
        LocalDate localDate8 = LocalDate.of(2017, 3, 22);
        // Period 周期性的.. 比较两个年份的差别
        Period period = Period.between(localDate7, localDate8); //
        System.out.println(period);
        System.out.println(period.getDays());

        System.out.println("- - -- - - ");
        // Instant  获取不带时区的UTC的标准时间.
        System.out.println(Instant.now());

        //,,,  剩下的用到的使用自行Google

    }
}

Java8的回顾和复盘

总共50节课,从开始到结束。学习到的不止是技术,更多的是学习方法。

系统的学习jdk8

  • Java 8新特性介绍
  • Lambda表达式介绍
  • 使用Lambda表达式代替匿名内部类
  • Lambda表达式的作用
  • 外部迭代与内部迭代
  • Java Lambda表达式语法详解
  • 函数式接口详解
  • 传递值与传递行为
  • Stream深度解析
  • Stream API详解
  • 串行流与并行流
  • Stream构成
  • Stream源生成方式
  • Stream操作类型
  • Stream转换
  • Optional详解
  • 默认方法详解
  • 方法与构造方法引用
  • Predicate接口详解
  • Function接口详解
  • Consumer接口剖析
  • Filter介绍
  • Map-Reduce讲解、中间操作与终止操作
  • 新的Date API分析

更多的时间是了解底层是怎么实现的。

最后说一句:基础的重要性

猜你喜欢

转载自www.cnblogs.com/wobushitiegan/p/12179786.html