java 1.8 中 關於LocalDateTime 的與long 的轉換,在1.8 localdateTime 中對於時區設置是敏感的,這個須要留意 public static void main(String[] args) { Period sixMonths = Period.ofMonths(1); LocalDate date = LocalDate.now(); LocalDate future = date.minus(sixMonths); System.out.println(future); LocalDateTime d = LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault()); System.out.println(d.minus(sixMonths).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); ZoneId zoneId = ZoneId.systemDefault(); // or: ZoneId.of("Europe/Oslo"); long epoch = date.atStartOfDay(zoneId).toEpochSecond(); System.out.println(epoch); System.out.println(new Date(1490585496969l)); System.out.println(ZoneId.systemDefault()); Set<String> set = new HashSet<>(); if (set.isEmpty()){ System.out.println("true"); } }