Java8 时间处理最全整理

import org.joda.time.DateTime;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Aaron.H.Shen
 * @date 1/23/2021 8:34 AM
 */
public class DateTest {

    /**
     * 传统方法
     * @throws ParseException
     */
    @Test
    public void DatetimeTest() throws ParseException {
        Date dNow = new Date(); //当前时间bai
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
        dBefore = calendar.getTime(); //得到前一天的时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore); //格式化前一天
        System.out.println(defaultStartDate);
        defaultStartDate = defaultStartDate+" 00:00:00";
        String defaultEndDate = defaultStartDate.substring(0,10)+" 23:59:59";
        System.out.println("前一天的起始时间是:" + defaultStartDate);
        System.out.println("前一天的结束时间是:" + defaultEndDate);
        System.out.println(defaultStartDate.substring(0, 10));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = formatter.parse(defaultEndDate);
        System.out.println(date);
        System.out.println(new DateTime(date));
    }

    /**
     * LocalDate
     */
    @Test
    public void LocalDateTest() {
        //获取当前日期
        LocalDate today = LocalDate.now();
        System.out.println("今天的日期:"+today);
        LocalTime time = LocalTime.now();
        System.out.println("获取当前的时间,不含有日期:"+time);
        System.out.println(LocalDateTime.now());

        //获取年、月、日信息
        int year = today.getYear();
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();
        System.out.println("year:"+year);
        System.out.println("month:"+month);
        System.out.println("day:"+day);

        //处理特定日期,自定义日期
        LocalDate date = LocalDate.of(2024,2,22);
        System.out.println("自定义日期:"+date);

        //判断两个日期是否相等
        System.out.println(today.equals(date));

        //检查像生日这种周期性事件
        MonthDay birthday = MonthDay.of(date.getMonth(),date.getDayOfMonth()); //today.getDayOfYear()
        System.out.println("birthday: " + birthday);
        MonthDay currentMonthDay = MonthDay.from(today);
        System.out.println(currentMonthDay);
    }

    /**
     * 时间计算
     */
    @Test
    public void localTimeTest() {
        LocalTime time = LocalTime.now();

        //时间加减
        LocalTime newTime = time.plusHours(3);
        System.out.println("三个小时后的时间为:"+newTime);

        LocalDate today = LocalDate.now();
        LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
        //today.plus(1, ChronoUnit.HOURS);
        System.out.println("一周后的日期为:"+nextWeek);

        //判断日期是早于还是晚于另一个日期
        LocalDate tomorrow = LocalDate.of(2032,2,6);
        if(tomorrow.isAfter(today)){
            System.out.println("之后的日期:"+tomorrow);
        }

        LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);
        if(yesterday.isBefore(today)){
            System.out.println("之前的日期:"+yesterday);
        }

        //计算闰年
        System.out.println("今年是否闰年:" + today.isLeapYear());
        //计算两个日期之间的天数和月数
        Period period = Period.between(today, nextWeek);
        System.out.println("计算两个日期之间的天数" + period.getDays());
    }

    /**
     * 时钟类
     */
    @Test
    public void ClockTest() {

        //当前的时间戳
        System.out.println(System.currentTimeMillis());
        //Clock
        Clock clock = Clock.systemUTC();
        System.out.println("Clock : " + clock.millis());

        Instant timestamp = Instant.now();
        System.out.println("What is value of this instant " + timestamp.toEpochMilli());

        //处理时区 America/Sao_Paulo   Asia/Tokyo  Asia/Shanghai
        ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println("指定时区的时间 : " + dateAndTimeInNewYork);

        ZoneId america1 = ZoneId.of("Asia/Shanghai");
        LocalDateTime localtDateAndTime1 = LocalDateTime.now();
        ZonedDateTime dateAndTimeInNewYork1  = ZonedDateTime.of(localtDateAndTime1, america1 );
        System.out.println("指定时区的时间 : " + dateAndTimeInNewYork1);
    }

    /**
     * 日期转换
     */
    @Test
    public void formatterDateTest() {
        String dayAfter = "20210222";
        LocalDate formatted = LocalDate.parse(dayAfter,
                DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(dayAfter+"  格式化后的日期为:  "+formatted);
        //System.out.println(LocalDate.parse(dayAfter, DateTimeFormatter.ISO_LOCAL_DATE));

        LocalDateTime date = LocalDateTime.now();

        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        //日期转字符串
        String str = date.format(format);

        System.out.println("日期转换为字符串:"+str);
        //字符串转日期
        LocalDate date2 = LocalDate.parse(str,format);
        System.out.println("日期类型:"+date2);
    }
}

你可能感兴趣的:(Java8 时间处理最全整理)