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");
	
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值