LocalDate类parse()方法 (LocalDate Class parse() method)
Syntax:
句法:
public Period until(ChronoLocalDate t_date);
public long until(Temporal temp, TemporalUnit t_unit);
until() method is available in java.time package.
在java.time包中可以使用until()方法。
until(ChronoLocalDate t_date) method is used to return the Period determined by the value between this LocalDate and the given ChronoLocalDate.
直到(ChronoLocalDate t_date)方法用于返回由此LocalDate和给定的ChronoLocalDate之间的值确定的Period。
until(Temporal temp, TemporalUnit t_unit) method is used to determine the amount of time until another date according to the given unit.
使用until(Temporal temp,TemporalUnit t_unit)方法根据给定的单位确定到另一个日期为止的时间。
These methods may throw an exception at the time of representing the Date.
这些方法在表示Date时可能会引发异常。
- DateTimeException: This exception may throw when getting any error during formatting.
- UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.
- ArithmeticException: This exception may throw when the calculated result exceeds the limit.
These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first cases, until(ChronoLocalDate t_date),
在第一种情况下,直到(ChronoLocalDate t_date),
- ChronoLocalDate t_date – represents the target date that can be in any chronology.
- ChronoLocalDate t_date –表示可以按任何时间顺序排列的目标日期。
In the second cases, until(Temporal temp, TemporalUnit t_unit),
在第二种情况下,直到(Temporal temp,TemporalUnit t_unit),
- Temporal temp – represents the object to convert to a date.
- 临时温度 –表示要转换为日期的对象。
- TemporalUnit t_unit – represents the unit to measure in amount.
- TemporalUnit t_unit –表示要计量的单位。
Return value:
返回值:
In the first case, the return type of the method is Period, it returns Period between this date and the given date.
在第一种情况下,该方法的返回类型为Period ,它返回此日期与给定日期之间的Period。
In the second case, the return type of the method is long, it returns the amount of time between this date and the given date.
在第二种情况下,该方法的返回类型为long ,它返回该日期与给定日期之间的时间量。
Example:
例:
// Java program to demonstrate the example
// of until() method of LocalDate
import java.time.*;
import java.time.temporal.*;
public class UntilOfLocalDate {
public static void main(String args[]) {
// Instantiates two LocalDate
LocalDate date1 = LocalDate.parse("2005-10-05");
LocalDate date2 = LocalDate.parse("2007-04-05");
// Display date1,date2
System.out.println("LocalDateTime date1,date2 : ");
System.out.println("date1: " + date1);
System.out.println("date2: " + date2);
System.out.println();
// Here, until(Temporal, TemporalUnit)
// calculates the amount of time of this
// date(date1) until another date(date2)
// in the given unit
long until = date1.until(date2, ChronoUnit.DAYS);
// Display until
System.out.println("date1.until(date2,ChronoUnit.DAYS): " + until);
// Here, until(ChronoLocalDate)
// calculates the period between
// this date(date1) until another
// date(date2)
Period p = date1.until(date2);
// Display p
int per_in_mon = p.getMonths();
System.out.println("date1.until(date2): " + per_in_mon);
}
}
Output
输出量
LocalDateTime date1,date2 :
date1: 2005-10-05
date2: 2007-04-05
date1.until(date2,ChronoUnit.DAYS): 547
date1.until(date2): 6
翻译自: https://www.includehelp.com/java/localdate-until-method-with-example.aspx