java_java.time_LocalDateTime

Java处理日期、日历和时间的方式一直为社区所诟病,将 java.util.Date设定为可变类型,以及SimpleDateFormat的非线程安全使其应用非常受限。

新API基于ISO标准日历系统,java.time包下的所有类都是不可变类型而且线程安全。
java_java.time_LocalDateTime_第1张图片

package _05date;


import org.joda.time.format.DateTimeFormat;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class _01LocalDate {
    //java8  新特性 线程安全
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        //todo 1 获取今天日期(不包含时间)
        System.out.println(today);//2020-02-25

        //todo 2 获取年月日信息
        int year = today.getYear();//2020
        Month month = today.getMonth();//FEBRUARY
        int monthValue = today.getMonthValue();//2
        int dayOfMonth = today.getDayOfMonth();//25
        System.out.println(year + "  " + month + " " + monthValue + " " + dayOfMonth);

        //todo 3 处理特定日期
        LocalDate date = LocalDate.of(2020, 2, 25);
        System.out.println("自定义日期" + date);//2020-02-25

        //todo 4 判断两个日期是否相等
        if (today.equals(date)) {
            System.out.println("时间相等");//时间相等
        } else {
            System.out.println("时间不相等");
        }

        //todo 5 检查生日周期性时间
        MonthDay birthday = MonthDay.of(today.getMonth(), today.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(date);
        if (currentMonthDay.equals(birthday)) {
            System.out.println("is your birthday " + currentMonthDay);//--02-25
        } else {
            System.out.println("is not your birthday and your birthday is " + birthday);
        }

        //todo 6 获取当前时间
        LocalTime time = LocalTime.now();
        System.out.println("获取当前时间,不含有日期 " + time);//18:25:15.736

        //todo 7 操作当前时间
        LocalTime newTime = time.plusHours(3);
        System.out.println(newTime);//21:25:15.736

        //todo 8 操作天,周,月 加plus 减minus
        LocalDate plus = today.plus(10, ChronoUnit.DAYS);
        System.out.println(plus);//2020-03-06
        LocalDate minus = today.minus(10, ChronoUnit.YEARS);
        System.out.println(minus);//2010-02-25

        //todo 9 Clock时钟类 (个人感觉没什么用)
        Clock clock = Clock.systemUTC();
        System.out.println("Clock : " + clock.millis());//1582630204924
        Clock defaultClock = Clock.systemDefaultZone();
        System.out.println("defaultClock : " + defaultClock);//SystemClock[GMT+08:00]

        //todo 10 判断日期大小
        LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
        if (tomorrow.isAfter(today)) {
            System.out.println("之后的日期" + tomorrow);//2020-02-27
        }
        LocalDate yesterday = LocalDate.of(2020, 02, 25);
        if (yesterday.isBefore(today)) {
            System.out.println("之前的日期:" + yesterday);//2020-02-25
        }

        //todo 11 处理时区
        //This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.
        ZoneId america = ZoneId.of("America/New_York");
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);//2020-02-26T00:25:32.468
        ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(now, america);
        //2020-02-26T00:34:12.790-05:00[America/New_York]
        System.out.println("current date and time in a particular timezone america is :" + dateAndTimeInNewYork);
        System.out.println(dateAndTimeInNewYork.toLocalDate());//2020-02-26

        //todo 12 表示信用卡到期这类固定日期
        YearMonth currentYearMonth = YearMonth.now();
        //System.out.printf("days in month year %s: %d%n",currentYearMonth,currentYearMonth.lengthOfMonth());
        System.out.println(currentYearMonth + "  " + currentYearMonth.lengthOfMonth());//2020-02  29
        YearMonth creditCardExpriy = YearMonth.of(2020, Month.FEBRUARY);
        System.out.println(creditCardExpriy);//2020-02

        //todo 13 检查闰年 有具体的api
        if (today.isLeapYear()) {
            System.out.println("this year is a leap year");//this year is a leap year
        } else {
            System.out.println("this year is not a leap year");
        }

        //todo 14 计算两个日期之间的天数和月数 月份好像是看日期的 上个月25 到这个月25 才算一个月
        LocalDate java8Release = LocalDate.of(2020, 03, 25);
        Period t_r = Period.between(today, java8Release);
        System.out.println(t_r.getMonths());//0

        //todo 15 获取当前时间戳
        Instant timeStamp = Instant.now();
        //实际上Instant类确实等同于 Java 8之前的Date类,你可以使用Date类和Instant类各自的转换方法互相转换
        //Date.from(Instant)  Date.toInstant()
        System.out.println(timeStamp.toEpochMilli());
        System.out.println(timeStamp.toString());

        //todo 16 格式化转换
        String dayAfterTommorrow = "20180205";
        LocalDate formatted = LocalDate.parse(dayAfterTommorrow,
                DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(dayAfterTommorrow+" 格式化后的日期为: "+formatted);//20180205 格式化后的日期为: 2018-02-05

        //日期转字符串
        DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        String str = now.format(format1);
        System.out.println("日期转换为字符串:"+str);//日期转换为字符串:2020/02/26 14:00:52

        //字符串转日期
        DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDate date2 = LocalDate.parse(str,format2);
        LocalTime time2 = LocalTime.parse(str, format2);
        System.out.println("日期类型:"+date2);//日期类型:2020-02-26
        System.out.println(time2);//14:00:52

		DateTimeFormatter format3 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String str2 = now.format(format3);
        System.out.println("日期转换为字符串:"+str2);//2020/02/28 16:15
    }
}

你可能感兴趣的:(JAVA)