Java 年月日

2018-03-12 13:57 更新

Java日期時間 - Java年月日

年表示一年,例如2012年,2013年等。

以下代碼顯示如何創(chuàng)建Year對象并對其執(zhí)行基本操作。

import java.time.Year;

public class Main {

  public static void main(String[] args) {
    Year y1 = Year.of(2014);
    System.out.println(y1);
    Year y2 = y1.minusYears(1);
    System.out.println(y2);
    Year y3 = y1.plusYears(1);
    System.out.println(y3);
    Year y4 = Year.now();
    System.out.println(y4);
    if (y1.isLeap()) {
      System.out.println(y1 + "  is a  leap year.");
    } else {
      System.out.println(y1 + "  is not  a  leap year.");
    }

  }
}

上面的代碼生成以下結(jié)果。

年月

年月表示年和月的有效組合,例如2012-05,2013-09等。

以下代碼顯示了如何創(chuàng)建Year Month對象并對其執(zhí)行一些基本操作。

import java.time.Month;
import java.time.YearMonth;

public class Main {

  public static void main(String[] args) {
    YearMonth ym1 = YearMonth.of(2014, Month.JUNE);  

    int monthLen = ym1.lengthOfMonth(); 
    System.out.println(monthLen);

    int yearLen  = ym1.lengthOfYear();
    System.out.println(yearLen);
  }
}

上面的代碼生成以下結(jié)果。


Month 枚舉有12個常量來表示12個月。

常量名稱為 一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月和十二月。

Month 枚舉從1到12按順序編號,其中一月為1,十二月為12。

Month枚舉從int值創(chuàng)建一個Month實例。

我們可以使用from()從date對象創(chuàng)建Month。

要獲取Month的int值,請使用Month枚舉 getValue()方法。

import java.time.LocalDate;
import java.time.Month;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, Month.AUGUST, 3);
    System.out.println(localDate);
    Month month1  = Month.from(localDate);
    System.out.println(month1);
    
    Month month2  = Month.of(2);
    System.out.println(month2);
    
    Month month3  = month2.plus(2);
    System.out.println(month3);
    
    Month month4  = localDate.getMonth();
    System.out.println(month4);
    
    int monthIntValue  = month2.getValue();
    System.out.println(monthIntValue);
  }
}

上面的代碼生成以下結(jié)果。

月日

MonthDay表示一個月和一個月中某一天的有效組合,例如12-15。

以下代碼顯示了如何創(chuàng)建MonthDay對象并對其執(zhí)行基本操作。

import java.time.Month;
import java.time.MonthDay;

public class Main {

  public static void main(String[] args) {
    MonthDay md1 = MonthDay.of(Month.DECEMBER, 25);
    MonthDay md2 = MonthDay.of(Month.FEBRUARY, 29);

    if (md2.isValidYear(2014)) {
      System.out.println(md2);
    }
    System.out.println(md1.getDayOfMonth());

  }
}

上面的代碼生成以下結(jié)果。

DayOfWeek

DayOfWeek 枚舉定義七個常量來表示一周中的七天。

DayOfWeek 枚舉的常量是 MONDAY,TUESDAY,WEDNESDAY,THURSDAY,F(xiàn)RIDAY,SATURDAY和SUNDAY

后端的int值為1到7。1表示星期一,2表示星期二...

以下代碼顯示了如何使用DayOfWeek枚舉。

import java.time.DayOfWeek;
import java.time.LocalDate;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, 6, 21);
    System.out.println(localDate);
    DayOfWeek  dayOfWeek1 = DayOfWeek.from(localDate);
    System.out.println(dayOfWeek1);
    int intValue = dayOfWeek1.getValue();
    System.out.println(intValue);
    DayOfWeek  dayOfWeek2 = localDate.getDayOfWeek();
    System.out.println(dayOfWeek2);
    DayOfWeek  dayOfWeekFromInteger = DayOfWeek.of(7);
    System.out.println(dayOfWeekFromInteger);
    DayOfWeek  dayOfWeekAdded = dayOfWeekFromInteger.plus(1);
    System.out.println(dayOfWeekAdded);
    DayOfWeek  dayOfWeekSubtracted = dayOfWeekFromInteger.minus(2); 
    System.out.println(dayOfWeekSubtracted);
  }
}

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號