旧API
Date和Calendar
使用SimpleDateFormat格式化
新API
LocalDateTime和ZonedDateTime
使用DateTimeFormatter格式化
Date date = new Date(); System.out.println(date); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(date)); LocalDate d = LocalDate.now(); // 当前日期 LocalTime t = LocalTime.now(); // 当前时间 LocalDateTime dt = LocalDateTime.now(); // 当前日期和时间 System.out.println(d); // 严格按照ISO 8601格式打印 System.out.println(t); // 严格按照ISO 8601格式打印 System.out.println(dt); // 严格按照ISO 8601格式打印 ZonedDateTime zdt = ZonedDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd' 'HH:mm:ss"); System.out.println(formatter.format(zdt)); DateTimeFormatter zhFormatter = DateTimeFormatter.ofPattern("yyyy MMM dd EE HH:mm", Locale.CHINA); System.out.println(zhFormatter.format(zdt)); DateTimeFormatter usFormatter = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US); System.out.println(usFormatter.format(zdt)); Instant now = Instant.now(); System.out.println(now.getEpochSecond()); // 秒 System.out.println(now.toEpochMilli()); // 毫秒
标签:java,System,yyyy,时间,DateTimeFormatter,println,操作,now,out From: https://www.cnblogs.com/mingkewang/p/17218006.html