Java LocalDate

Java LocalDate class is part of Java 8 Date API.

Java LocalDate类是Java 8 Date API的一部分。

Java LocalDate (Java LocalDate)

  • Java LocalDate is immutable class and hence thread safe.

    Java LocalDate是不可变的类 ,因此是线程安全的 。
  • LocalDate provides date output in the format of YYYY-MM-dd.
    Java LocalDate_第1张图片

    LocalDate以YYYY-MM-dd格式提供日期输出。
  • The LocalDate class has no time or Timezone data. So LocalDate is suitable to represent dates such as Birthday, National Holiday etc.

    LocalDate类没有时间或时区数据。 因此LocalDate适合代表生日,国定假日等日期。
  • LocalDate class implements Temporal, TemporalAdjuster, ChronoLocalDate and Serializable interfaces.

    LocalDate类实现TemporalTemporalAdjusterChronoLocalDateSerializable接口。
  • LocalDate is a final class, so we can’t extend it.

    LocalDate是最后一个类,因此我们无法扩展它。
  • LocalDate is a value based class, so we should use equals() method for comparing if two LocalDate instances are equal or not.

    LocalDate是一个基于值的类,因此我们应该使用equals()方法比较两个LocalDate实例是否相等。

导入Java LocalDate类 (Importing Java LocalDate Class)

Java LocalDate class is in java.time package. So you can import it using import statement:

Java LocalDate类在java.time包中。 因此,您可以使用import语句将其导入:

import java.time.LocalDate;

创建LocalDate实例 (Creating LocalDate instance)

There are four standard ways to create LocalDate instance.

有四种创建LocalDate实例的标准方法。

  1. Invoking the static now() method that returns the current date from system clock.
    LocalDate ldObj = LocalDate.now();
    		
    System.out.println(ldObj); //prints "2017-11-10"

    调用静态now()方法从系统时钟返回当前日期。
  2. By passing Year, Month, and Day values to LocalDate of() method
    LocalDate ldObj = LocalDate.of(2017, 11, 6);
    		
    System.out.println(ldObj); //prints "2017-11-06"

    通过将Year,Month和Day值传递给LocalDate of()方法
  3. Using LocalDate parse() method.
    LocalDate ld = LocalDate.parse("2017-11-10");

    使用LocalDate parse()方法。
  4. Using LocalDate ofInstant() method as shown below.
    LocalDate ld = LocalDate.ofInstant(Instant.now(), ZoneId.systemDefault());

    使用LocalDate ofInstant()方法,如下所示。

从LocalDate类检索日期信息 (Retrieving Date Information from LocalDate Class)

Java LocalDate class provides a lot of useful methods to get the more details about date, for example year, month, day of year etc. Let’s look into these through some example programs.

Java LocalDate类提供了许多有用的方法来获取有关日期的更多详细信息,例如年,月,日等。让我们通过一些示例程序来研究这些内容。

  1. getYear(): returns the year contained within the LocalDate object.
    LocalDate ld = LocalDate.of(2017, 11, 10);
    		
    int year = ld.getYear();
    		
    System.out.println(year); //prints "2017"

    getYear() :返回LocalDate对象中包含的年份。
  2. getMonth(): returns the month contained within the LocalDate object.
    LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
    
    Month month = ld.getMonth();
    
    System.out.println(month + "," + month.getValue()); // prints "NOVEMBER,11"

    getMonth() :返回LocalDate对象中包含的月份。
  3. getDayOfMonth(): returns the day of the month contained within the LocalDate object.
    LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
    
    int dayOfMonth = ld.getDayOfMonth();
    
    System.out.println(dayOfMonth); // prints "10"

    getDayOfMonth() :返回LocalDate对象中包含的月份中的一天。
  4. getDayOfWeek(): returns the day of the week from the LocalDate object.
    LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
    
    DayOfWeek dayOfWeek = ld.getDayOfWeek();
    
    System.out.println(dayOfWeek + "," + dayOfWeek.getValue()); // prints "FRIDAY,5"

    getDayOfWeek() :从LocalDate对象返回星期几。
  5. getDayOfYear(): returns the day of the year from the LocalDate instance.
    LocalDate ld = LocalDate.of(2017, 2, 10);
    
    int dayOfYear = ld.getDayOfYear();
    
    System.out.println(dayOfYear); // prints "41" i.e. 31+10

    getDayOfYear() :从LocalDate实例返回一年中的一天。

Java LocalDate方法–加号和减号 (Java LocalDate methods – plus and minus)

  1. plusYears(long yearsToAdd): adds the yearsToAdd to the year value and return the copy of LocalDate object. Since LocalDate is immutable, the specified object doesn’t change. Also if the new date is invalid then the last valid date is returned.
    LocalDate ld = LocalDate.of(2017, 2, 10);
    
    LocalDate ldNew = ld.plusYears(3);
    
    System.out.println(ldNew + ":" + ld); // prints "2020-02-10:2017-02-10"
    
    ld = LocalDate.of(2016, 2, 29);
    ldNew = ld.plusYears(1); // 2017-02-29 is invalid date, so it will return 2017-02-28
    System.out.println(ldNew); // prints "2017-02-28"

    plusYears(long yearsToAdd) :将yearsToAdd添加到年份值并返回LocalDate对象的副本。 由于LocalDate是不可变的,因此指定的对象不会更改。 同样,如果新日期无效,则返回最后一个有效日期。
  2. plusWeeks(long weeksToAdd): returns a new LocalDate instance after adding weeksToAdd weeks, no change in the specified object.
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.plusWeeks(3);
    
    System.out.println(ldNew); // 2017-12-01

    plusWeeks(long weeksToAdd) :在添加weeksToAdd周之后,返回一个新的LocalDate实例,指定对象没有变化。
  3. plusMonths(long monthsToAdd): returns a copy of specified LocalDate object after adding monthsToAdd months.
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.plusMonths(3);
    
    System.out.println(ldNew); // 2018-02-10

    plusMonths(long monthsToAdd) :添加monthsToAdd months后,返回指定LocalDate对象的副本。
  4. plusDays(long daysToAdd): returns a copy of specified LocalDate object after adding daysToAdd days.
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.plusDays(10);
    
    System.out.println(ldNew); // 2017-11-20

    plusDays(long daysToAdd) :添加daysToAdd天后,返回指定LocalDate对象的副本。
  5. minusDays(long daysToSubtract): returns a copy of specified LocalDate object after subtracting daysToSubtract days.
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.minusDays(10);
    
    System.out.println(ldNew); // 2017-10-31

    minusDays(long daysToSubtract) :减去daysToSubtract days之后,返回指定LocalDate对象的副本。
  6. minusWeeks(long weeksToSubtract): returns a copy of specified LocalDate object after subtracting weeksToSubtract weeks.
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.minusWeeks(1);
    
    System.out.println(ldNew); // 2017-11-03

    minusWeeks(long weeksToSubtract) :在减去weeksToSubtract周之后,返回指定LocalDate对象的副本。
  7. minusMonths(long monthsToSubtract): returns a copy of specified LocalDate object after subtracting monthsToSubtract months.
    LocalDate ld = LocalDate.of(2017, 11, 10);
    
    LocalDate ldNew = ld.minusMonths(1);
    
    System.out.println(ldNew); // 2017-10-10

    minusMonths(long monthsToSubtract) :减去monthsToSubtract months之后,返回指定LocalDate对象的副本。
  8. minusYears(long yearsToSubtract): returns a copy of specified LocalDate object after subtracting yearsToSubtract years. If the new LocalDate is invalid (29-Feb), then last valid date is returned.
    LocalDate ld = LocalDate.of(2016, 2, 29);
    
    LocalDate ldNew = ld.minusYears(2);
    
    System.out.println(ldNew); // 2014-02-28

    minusYears(long yearsToSubtract) :减去yearsToSubtract years之后,返回指定LocalDate对象的副本。 如果新的LocalDate无效(2月29日),则返回上一个有效日期。

迄今为止的Java LocalDate (Java LocalDate to Date)

We should avoid using legacy java.util.Date class but sometimes we have to convert LocalDate to Date for legacy support. Below are two ways to convert LocalDate to Date, you can use any of them.

我们应该避免使用旧版java.util.Date类,但有时我们必须将LocalDate转换为Date以获取旧版支持。 以下是将LocalDate转换为Date的两种方法,您可以使用其中任何一种。

package com.journaldev.java;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class JavaLocalDateExample {

	public static void main(String args[]) {

		LocalDate ld = LocalDate.now();

		Date date = Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant());

		System.out.println(date); // prints "Fri Nov 10 00:00:00 IST 2017"
		
		Date date1 = java.sql.Date.valueOf(ld);
		
		System.out.println(date1); // prints "2017-11-10"
	}
}

That’s all for java LocalDate class.

Java LocalDate类就这些了。

References: API Doc, StackOverflow Article

参考: API Doc , StackOverflow文章

翻译自: https://www.journaldev.com/16896/java-localdate

你可能感兴趣的:(java,大数据,jdk,sql,json)