java8 时间处理

package demo;

import java.sql.Timestamp;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class demo {
    public static void main(String[] args) {
        //LocalDate
        //获取当天日期
        LocalDate now = LocalDate.now();
        System.out.println(now);
        //构造指定日期
        LocalDate localDate = LocalDate.of(2020, 1, 30);
        System.out.println(localDate);
        //获取年月日等信息
        System.out.println(now.getYear());
        System.out.println(now.getMonth());
        System.out.println(now.getMonthValue());
        System.out.println(now.getDayOfMonth());
        //比较两个日期是否相等
        LocalDate localDate1 = LocalDate.of(2020, 10, 9);
        System.out.println(now.equals(localDate1));
        //LocalTime
        LocalTime now1 = LocalTime.now();
        System.out.println(now1);
        // 时间增量
        System.out.println(now1.plusSeconds(60));
        System.out.println(now1.plusMinutes(5));
        System.out.println(now1.plusHours(1));
        // 日期增量
        System.out.println(now.plus(1, ChronoUnit.DAYS));
        //大小判断
        System.out.println(now);
        System.out.println(localDate);
        System.out.println(now.isAfter(localDate));
        System.out.println(now.isBefore(localDate));
        // 上海时间
        ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime shanghaiZonedDateTime = ZonedDateTime.now(shanghaiZoneId);

        // 东京时间
        ZoneId tokyoZoneId = ZoneId.of("Asia/Tokyo");
        ZonedDateTime tokyoZonedDateTime = ZonedDateTime.now(tokyoZoneId);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("上海时间: " + shanghaiZonedDateTime.format(formatter));
        System.out.println("东京时间: " + tokyoZonedDateTime.format(formatter));
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println(now2);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now2);
        System.out.println(format);
        // 解析日期
        String dateText = "20180924";
        LocalDate date = LocalDate.parse(dateText, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("格式化之后的日期=" + date);

        // 格式化日期
        dateText = date.format(DateTimeFormatter.ISO_DATE);
        System.out.println("dateText=" + dateText);


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

        // 字符串转日期时间
        String datetimeText = "1999-12-31 23:59:59";
        LocalDateTime datetime = LocalDateTime.parse(datetimeText, formatter);
        System.out.println(datetime);

        //时间戳
        System.out.println(Instant.now());
        System.out.println(Clock.systemUTC());
        System.out.println(Clock.systemDefaultZone().getZone());
        Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now());
        System.out.println(timestamp.getTime());
        System.out.println(timestamp.getTime() / 1000);


        Instant instant = Instant.now();
        long timeStampMillis = instant.toEpochMilli();
        System.out.println(timeStampMillis);
        System.out.println(timeStampMillis / 1000);

//        Long millisecond = Instant.now().toEpochMilli();  // 精确到毫秒
//        Long second = Instant.now().getEpochSecond();// 精确到秒
        long epochSecond = Instant.now().getEpochSecond();
        System.out.println(epochSecond);
        String format1 = dateTimeFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), ZoneId.systemDefault()));
        System.out.println(format1);


        LocalDateTime parse = LocalDateTime.parse("2020-10-09 23:26:43", dateTimeFormatter);
        long l = LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        System.out.println(l);
        long epochSecond1 = LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
        System.out.println(epochSecond1);


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值