Java8日期类学习

 

  • LocalDateTime:日期加时间的日期对象,包含年月日时分秒

  • LocalDate:日期类,包含年月日

  • LocalTime:时间类,包含时分秒

1.获取当前时间

public class Java8DateTest {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        System.out.println(localDateTime);
        System.out.println(localDate);
        System.out.println(localTime);
    }
}

返回结果:

2023-04-07T09:49:19.192
2023-04-07
09:49:19.192

2.获取当前时间的年月日时分秒

public class Java8DateTest {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        int year = localDateTime.getYear();
        int month = localDateTime.getMonthValue();
        int day = localDateTime.getDayOfMonth();
        int hour = localDateTime.getHour();
        int minute = localDateTime.getMinute();
        int second = localDateTime.getSecond();
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        System.out.println(hour);
        System.out.println(minute);
        System.out.println(second);
    }
}

返回结果:

2023
4
7
9
53
1

3.时间赋值

public class Java8DateTest {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.of(2023,4,7,9,54,20);
        System.out.println(localDateTime);
    }
}

4.时间格式化

public class Java8DateTest {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.parse("2023-04-07 09:56:30", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(localDateTime);
        LocalDateTime localDateTime1 = LocalDateTime.parse("2023.04.07 09:56:30", DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"));
        System.out.println(localDateTime1);
    }
}

输出结果

2023-04-07T09:56:30
2023-04-07T09:56:30


5.时间运算

加上对应的时间

 减去对应的时间

 

public class Java8DateTest {
    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
        System.out.println("now=" + now);
        // 加2个小时
        LocalDateTime plusHours = now.plusHours(2L);
        System.out.println(plusHours);
        // 减2个小时
        LocalDateTime minusHours = now.minusHours(2L);
        System.out.println(minusHours);
    }
}

6.时间比较

public class Java8DateTest {
    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime plusYears = now.plusYears(2L);
        // 两个时间作比较,第一个时间减去第二个时间(如果年份相同,比较月份,月份相同比较天数,以此类推)
        int compareTo = plusYears.compareTo(now);
        // 输出2
        System.out.println(compareTo);
    }
}

利用Duration计算时间差

public class Java8DateTest {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime of = LocalDateTime.of(2022, 4, 7, 10, 19, 20);
        // 返回相差的天数 后面的时间减去前面的时间
        long days = Duration.between(of, now).toDays();
        // 输出365
        System.out.println(days);
        // 返回相差的小时数
        long hours = Duration.between(of, now).toHours();
        // 输出8760
        System.out.println(hours);
    }
}

7.日期格式化

public class Java8DateTest {
    public static void main(String[] args) {
        // 自定义时间格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(LocalDateTime.now());
        // 输出:2023-04-07 10:25:26
        System.out.println(format);
    }
}
public class Java8DateTest {
    public static void main(String[] args) {
        // 本月第一天
        LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();
        System.out.println(firstDay);
        // 本月最后一天
        LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(lastDay);
    }
}
public class Java8DateTest {
    public static void main(String[] args) {
        // 判断两个时间点的前后
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime plusHours = now.plusHours(2L);
        // 返回true
        System.out.println(now.isBefore(plusHours));
        // 返回false
        System.out.println(now.isAfter(plusHours));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinxun2008081

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值