美文网首页
java8 时间处理

java8 时间处理

作者: 潇豪 | 来源:发表于2020-10-09 23:34 被阅读0次
    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);
    
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:java8 时间处理

          本文链接:https://www.haomeiwen.com/subject/gzarpktx.html