java8新特性之时间工具类常用方法


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class DateTest {

    /**
     * Instant         时间戳
     * Duration        持续时间、时间差
     * LocalDate       只包含日期,比如:2018-09-24
     * LocalTime       只包含时间,比如:10:32:10
     * LocalDateTime   包含日期和时间,比如:2018-09-24 10:32:10
     * Peroid          时间段
     * ZoneOffset      时区偏移量,比如:+8:00
     * ZonedDateTime   带时区的日期时间
     * Clock           时钟,可用于获取当前时间戳
     * java.time.format.DateTimeFormatter      时间格式化类
     * @param args
     */
    public static void main(String[] args) {
        /**
         * 当前时间
         * 不包含 时,分,秒
         */
        LocalDate localDate = LocalDate.now();
        System.out.println("当前时间:" + localDate);
        /**
         * 构造指定日期
         * 传入 年,月,日
         */
        LocalDate localDate1 = LocalDate.of(2020, 7, 27);
        System.out.println("构造指定日期:" + localDate1);
        /**
         * 获取 年   月   日信息
         */
        System.out.printf("年=%d, 月=%d, 日=%d", localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth());
        /**
         * 比较两个日期是否相等
         */
        System.out.println("日期是否相等=" + localDate.equals(localDate1));
        /**
         *
         */
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.plusHours(2);
        System.out.println("newTime=" + newTime);

        LocalDate date = LocalDate.now();
        LocalDate newDate = date.plus(2, ChronoUnit.WEEKS);
        System.out.println("newDate=" + newDate);

        // 解析日期
        String dateText = "20200728";
        LocalDate date2 = LocalDate.parse(dateText, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("格式化之后的日期=" + date2);

        // 格式化日期
        dateText = date2.format(DateTimeFormatter.ISO_DATE);
        System.out.println("dateText=" + dateText);
        /**
         * 日期和字符串的相互转换
         */
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 日期时间转字符串
        LocalDateTime now = LocalDateTime.now();
        String nowText = now.format(formatter);
        System.out.println("nowText=" + nowText);

        // 字符串转日期时间
        String datetimeText = "2020-07-26 23:59:59";
        LocalDateTime datetime = LocalDateTime.parse(datetimeText, formatter);
        System.out.println(datetime);
        /**
         * 时区设置
         */
        // 上海时间
        ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime shanghaiZonedDateTime = ZonedDateTime.now(shanghaiZoneId);

        // 东京时间
        ZoneId tokyoZoneId = ZoneId.of("Asia/Tokyo");
        ZonedDateTime tokyoZonedDateTime = ZonedDateTime.now(tokyoZoneId);
        System.out.println("上海时间: " + shanghaiZonedDateTime.format(formatter));
        System.out.println("东京时间: " + tokyoZonedDateTime.format(formatter));
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值