java8中的日期时间处理类

package com.freeOfFlying.java8.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
 * 日期-时间处理
 * @ClassName: DateTimeTest
 * @Description:
 * @author freeflying
 * @date 2018年8月24日
 */
public class DateTimeTest{
	public static void main(String[] args) {
		//获取当前系统日期
		System.out.println(LocalDate.now());//2018-08-24
		//按照指定创建LocalDate对象
		LocalDate of = LocalDate.of(2018, 5, 30);
		System.out.println(of.getDayOfMonth());//30
		//分别获取年月日
		LocalDate now = LocalDate.now();
		System.out.println(now.getYear());//2018
		System.out.println(now.getMonth());//AUGUST
		System.out.println(now.getMonthValue());//8
		System.out.println(now.getDayOfMonth());//24
		//获取时间及时分秒
		LocalTime localTime=LocalTime.now();
		System.out.println(localTime);//21:59:45.206
		LocalTime of2 = LocalTime.of(12, 32,25);
		System.out.println(of2);//12:32:25
		System.out.println(localTime.getHour());//21
		System.out.println(localTime.getMinute());//59
		System.out.println(localTime.getSecond());//45
		System.out.println(localTime.getNano());//206000000
		//同时获取年月日、时分秒。方法同上
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println(localDateTime);//2018-08-24T22:03:19.630
		System.out.println(localDateTime.getYear());//2018
		System.out.println(localDateTime.getMonthValue());//8
		System.out.println(localDateTime.getMonth());//AUGUST
		System.out.println(localDateTime.getDayOfMonth());//24
		System.out.println(localDateTime.getHour());//22
		System.out.println(localDateTime.getMinute());//6
		System.out.println(localDateTime.getSecond());//21
		System.out.println(localDateTime.getDayOfYear());//236  距本年第一天的时间
		
		//格式化
		DateTimeFormatter dFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss");
		LocalDateTime parse = localDateTime.parse("2018-08-24:22:06:21",dFormatter);
		System.out.println(parse);//2018-08-24T22:06:21
		
		//根据时区处理时间
		ZonedDateTime now2 = ZonedDateTime.now();
		System.out.println(now2);//2018-08-24T22:11:14.888+08:00[Asia/Shanghai]
		DateTimeFormatter formatter=DateTimeFormatter.ofPattern("MM/dd/yyyy:HH:mm:ss");
	
	}
}

 

你可能感兴趣的:(javase)