九阴真经


Java8日期

<h5>日期、字符串互转</h5> <pre><code>String date = "2012-02-11 12:23:23"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //字符串转日期 LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter).plusMonths(3); //日期转字符串 System.out.println(dateTimeFormatter.format(localDateTime)); //LoclDateTime 转 Date LocalDateTime localDateTime = LocalDateTime.now(); Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); System.out.println(date); //Date 转 LocalDateTime LocalDate localDateTime_ = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(localDateTime_);</code></pre> <h5>计算</h5> <pre><code>LocalDateTime localDateTime = LocalDateTime.now(); String string = "2018-12-01 23:22:22"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime start = LocalDateTime.parse(string, dateTimeFormatter); Duration duration = Duration.between(start, localDateTime); long days = duration.toDays(); System.out.println("天数:" + days); long seconds = duration.getSeconds(); System.out.println("秒数:" + seconds);</code></pre> <h5>时间戳转日期</h5> <pre><code> /** * System.out.println(new Date().getTime()); 打印时间戳 * 时间戳转日期 */ public static Date stampToTime(String stamp) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sd = sdf.format(new Date(Long.parseLong(stamp))); // 时间戳转换日期 try { Date date = sdf.parse(sd); return date; } catch (ParseException e) { e.printStackTrace(); } return new Date(); }</code></pre>

页面列表

ITEM_HTML