jdk1.8 之时间类
package com.github.wxpay.sdk; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; /** * jdk1.8新特性 LocalDateTime */ public class Time { public static void main(String[] args) { List<String> list = new ArrayList <>( ); for (int i = 0; i < 12; i++) { list.add( timeToString( getDate().plusHours( 2*i ) ) ); } list.stream().forEach( s -> System.out.println(s) ); } //获取到当前日期的00:00:00 public static LocalDateTime getDate() { //获取当前时间 LocalDateTime now = LocalDateTime.now(); //获取当前时间的小时值 int hour = now.getHour(); //获取当前时间的分钟值 int minute = now.getMinute(); //获取当前时间的秒数值 int second = now.getSecond(); //当前时间减去小时,分钟,秒 LocalDateTime localDateTime = now.plusHours( -hour ).plusMinutes( -minute ).plusSeconds( -second ); //返回处理后的时间 return localDateTime; } //解析时间格式的方法 public static String timeToString(LocalDateTime localDateTime){ //定义解析格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" ); String time = localDateTime.format( formatter ); return time; } }
输出:
2020-12-20 00:00:00 2020-12-20 02:00:00 2020-12-20 04:00:00 2020-12-20 06:00:00 2020-12-20 08:00:00 2020-12-20 10:00:00 2020-12-20 12:00:00 2020-12-20 14:00:00 2020-12-20 16:00:00 2020-12-20 18:00:00 2020-12-20 20:00:00 2020-12-20 22:00:00
常用API
now() //获取当前系统时间
getYear() //获取年份
详情查看JDK1.8文档

浙公网安备 33010602011771号