Java LocalDate类| 直到示例的until()方法

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值