文章标题

    xiaoxiao2022-06-22  18

    jdk8中新加入的日期类,大大方便了我们对日期的操作,可有时也会因为对某些类的不熟悉,导致迟迟找不到解决 办法,下面我就对自己所使用的一些方法做一些记录,方便以后查阅。 1.日期 LocalDate localDate = LocalDate.now(); System.out.println(localDate.toString());//2016-09-14 System.out.println(localDate.plusDays(1));//2016-09-15 System.out.println(localDate.minusDays(1));//2016-09-13 System.out.println(localDate.atStartOfDay());//2016-09-14T00:00 System.out.println(localDate.atStartOfDay().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//2016-09-14 00:00:00 System.out.println(localDate.plusDays(1).atStartOfDay().minusSeconds(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//2016-09-14 23:59:59 System.out.println(localDate.atTime(23,59,59).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//2016-09-14 23:59:59 System.out.println(localDate.lengthOfYear());// 366,是当前年的天数 System.out.println(localDate.getDayOfYear());// 258, 是当前天在本年度中的天数 System.out.println(localDate.getDayOfWeek().getValue()); // 3 星期三 System.out.println(localDate.getMonth().getValue());// 9 代表9月 System.out.println(localDate.getMonthValue());// 9 // 2.时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime);//2016-09-14T00:00:54.048 System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//2016-09-14 00:00:54 System.out.println(localDateTime.atZone(ZoneId.of(ZoneId.SHORT_IDS.get("CTT"))).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) );//中国时区 2016-09-14 00:00:54 // 3.与Date转换 LocalDateTime localDateTime1 = LocalDateTime.now(); Instant instant = localDateTime1.atZone(ZoneId.of("Asia/Shanghai")).toInstant(); Date date = Date.from(instant); // 获取时间戳 long timestamp = date.getTime(); System.out.println(timestamp);// 1473782905307 Instant instant1 = Instant.ofEpochMilli(timestamp);// System.out.println(instant1.atZone(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));// 2016-09-14 00:08:25
    转载请注明原文地址: https://ju.6miu.com/read-1122912.html

    最新回复(0)