Java8中LocalDateTime相关类和方法

Java8中LocalDateTime相关类和方法都在这了【破涕为笑】

import org.junit.Test;

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

/**
 * @author Tuzi
 * @since 1.0.0
 */
public class LocalDateTimeDemo {

    @Test
    public void testLocalDate() {
        // 获取LocalDate
        LocalDate now = LocalDate.now();
        LocalDate localDate = LocalDate.of(1995, 10, 19);
        LocalDate localDate2 = LocalDate.parse("2017-02-22");

        // 昨天
        LocalDate yesterday = now.minusDays(1);

        // 上个月的今天
        LocalDate localDate1 = now.minus(1, ChronoUnit.MONTHS);

        DayOfWeek dayOfWeek = localDate2.getDayOfWeek();
        int dayOfMonth = localDate2.getDayOfMonth();

        // 判断今年是不是闰年
        boolean leapYear = now.isLeapYear();

        // 获取这个月的第一天
        LocalDate firstDayOfMonth = now.withDayOfMonth(1);
        LocalDate firstDayOfMonth2 = now.with(TemporalAdjusters.firstDayOfMonth());

        // 判断今天是不是生日
        LocalDate birthday = LocalDate.parse("1995-10-19");
        MonthDay birthdayInMonthDay = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
        MonthDay today = MonthDay.from(now);
        boolean todayIsMyBrithday = birthdayInMonthDay.equals(today);
    }

    @Test
    public void testLocalTime() {
        // 获取LocalTime
        LocalTime now = LocalTime.now();    // 16:39:45.252
        LocalTime localTime = LocalTime.of(16, 40);
        LocalTime localTime1 = LocalTime.parse("16:40");

        LocalTime nextHour = now.plus(1, ChronoUnit.HOURS);

        int hour = now.getHour();
        int minute = now.getMinute();

        boolean after = localTime.isAfter(now);

        // 常量
        System.out.println(LocalTime.MAX);      // 23:59:59.999999999
        System.out.println(LocalTime.MIN);      // 00:00
    }

    @Test
    public void testLocalDateTime() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = LocalDateTime.parse("2017-11-06T16:48:15.801");
        LocalDateTime localDateTime1 = LocalDateTime.of(2017, 11, 06, 16, 48, 15, 801);

        // 加减
        LocalDateTime plusHours = now.plusHours(1);
        LocalDateTime minus = now.minus(2, ChronoUnit.DAYS);

        System.out.println(now.getDayOfMonth());

        LocalDate localDate = localDateTime.toLocalDate();
        LocalTime localTime = localDateTime.toLocalTime();
        LocalDateTime localDateTime2 = LocalDateTime.from(localDate.atStartOfDay());
    }

    @Test
    public void testDateTimeFormatter() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("默认格式: " + now);
        System.out.println("自定义格式:  " + now.format(dateTimeFormatter));

        LocalDateTime.parse("2017-11-06 17:02:05", dateTimeFormatter);

        dateTimeFormatter.format(LocalDateTime.now());
    }

    @Test
    public void testPeriod() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime plusTwoDay = now.plus(Period.ofDays(2));
        long between = ChronoUnit.DAYS.between(now, plusTwoDay);
    }

    @Test
    public void transfer() {
        Date date = Date.from(Instant.now());
        date.toInstant();

        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

        LocalDateTime now = LocalDateTime.now();
        Date.from(now.atZone(ZoneId.systemDefault()).toInstant());

        LocalDate localDate = LocalDate.now();
        Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值