集册 Java实例教程 使用Year、YearMonth和MonthDay部分

使用Year、YearMonth和MonthDay部分

欢马劈雪     最近更新时间:2020-01-02 10:19:05

788
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用Year、YearMonth和MonthDay部分

import java.time.Month;

import java.time.MonthDay;

import java.time.Year;//来 自 n  o  w  j  a  v  a . c o m

import java.time.YearMonth;


public class Main {

  public static void main(String[] args) {

    // Use Year

    Year y1 = Year.of(2012);    // 2012

    Year y2 = y1.minusYears(1); // 2011

    Year y3 = y1.plusYears(1);  // 2013

    Year y4 = Year.now();       // current year

    if (y1.isLeap()) {

      System.out.println(y1 + " is a leap year.");

    } 

    else {

      System.out.println(y1 + " is not a leap year.");

    }


    // Use YearMonth

    YearMonth ym1 = YearMonth.of(2012, Month.MAY); // 2012-05
    /* from 
    nowjava*/

    

    // Get the number of days in the month

    int monthLen = ym1.lengthOfMonth(); // 31

    System.out.println("Days in month in " + ym1 + ": " + monthLen);

    

    // Get the number of days in the year

    int yearLen = ym1.lengthOfYear(); // 366

    System.out.println("Days in year in " + ym1 + ": " + yearLen);

    

    MonthDay md1 = MonthDay.of(Month.DECEMBER, 25);

    
展开阅读全文