• java8时间处理实例


    实例:

    package com.javaBase.time;
    
    import java.time.Clock;
    import java.time.LocalDate;
    import java.time.LocalTime;
    import java.time.MonthDay;
    import java.time.YearMonth;
    import java.time.temporal.ChronoUnit;
    
    public class GetDateWithJava8 {
    	public static void main(String[] args) {
    		//2019-04-11
    		LocalDate today=LocalDate.now();
    		//2019
    		int year=today.getYear();
    		//4
    		int month=today.getMonthValue();
    		//11
    		int day=today.getDayOfMonth();
    		//101,表示本年过了多天
    		int dayOfYear=today.getDayOfYear();
    		System.out.println("today:"+today+",year:"+year+",month:"+month+",day:"+day+",dayOfYear:"+dayOfYear);
    		//创建指定日期
    		//2019-04-11
    		LocalDate myDate=LocalDate.of(2019,4,11);
    		//日期比较,同一天则是true
    		boolean flag=today.equals(myDate);
    		//检查生日
    		LocalDate dateOfBirth=LocalDate.of(1995,3,12);
    		MonthDay birthDay=MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
    		LocalDate now=LocalDate.now();
    		MonthDay currentMonthDay=MonthDay.from(now);
    		if(currentMonthDay.equals(birthDay)) {
    			System.out.println("今天是生日...");
    		}
    		//获取当前时间,默认的格式是hh:mm:ss:nnn
    		//15:53:11.096
    		LocalTime nowTime=LocalTime.now();	
    		//增加2个小时
    		//17:53:11.096
    		LocalTime twoHour=nowTime.plusHours(2);
    		//增加一周
    		//2019-04-18
    		LocalDate oneWeek=today.plus(1,ChronoUnit.WEEKS);
    		//增加一个月
    		//2019-05-11
    		LocalDate oneMonth=today.plusMonths(1);
    		//增加一年
    		//2020-04-11
    		LocalDate oneYear=today.plus(1, ChronoUnit.YEARS);
    		//使用时钟
    		//SystemClock[Z]
    		Clock clock=Clock.systemUTC();
    		//SystemClock[Asia/Shanghai]
    		clock=Clock.systemDefaultZone();
    		//*****************日期比较**********************//
    		LocalDate tomorrow=today.plus(1,ChronoUnit.DAYS);
    		//返回true
    		today.isBefore(tomorrow);
    		//返回true
    		tomorrow.isAfter(today);
    		//*******检查本月的天数*****//
    		//2019-04
    		YearMonth currentYearMonth=YearMonth.now();
    		//获取本月的天数
    		//30
    		currentYearMonth.lengthOfMonth();
    		//检查平年还是闰年
    		//false(表示不是闰年)
    		today.isLeapYear();
    		System.out.println(today.isLeapYear());
    		// 两个日期之间包含多少天,
    		
    		
    	}
    
    }
    

    =================================================================================================================================== 

  • 相关阅读:
    Visual Studio 2015安装后没有C++ WIN32 console
    ORACLE SEQUENCE用法
    adoconnection断线重连
    海康威视网络摄像机 SDK二次开发(JAVA)(1-DEMO测试)
    FFmpeg,H.264,Directshow,和opencv及视频编码与封装格式
    DirectShow 视频采集
    CoCreateInstance运行原理
    二叉树中序线索化
    Xvid-Delphi
    风冷冷藏库的化霜周期和化霜时间是多长?
  • 原文地址:https://www.cnblogs.com/JAYIT/p/10531643.html
Copyright © 2020-2023  润新知