mysql中todays_Java Duration类| toDays()方法与示例

mysql中todays

Duration Class toDays()方法 (Duration Class toDays() method)

  • toDays() method is available in java.time package.

    toDays()方法在java.time包中可用。

  • toDays() method is used to convert this Duration into the number of days.

    toDays()方法用于将此持续时间转换为天数。

  • toDays() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    toDays()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • toDays() method does not throw an exception at the time of converting this Duration to days.

    在将此持续时间转换为天数时, toDays()方法不会引发异常。

Syntax:

句法:

    public Temporal subtractFrom(Temporal temp);

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is long, it returns the number of days exists in this Duration.

此方法的返回类型为long ,它返回此Duration中存在的天数。

Example:

例:

// Java program to demonstrate the example 
// of long toDays() method of Duration

import java.time.*;

public class ToDaysOfDuration {
    public static void main(String args[]) {
        // Instantiates two Duration objects
        Duration du1 = Duration.ofHours(72);
        Duration du2 = Duration.parse("P0DT48H");

        // Display du1 and du2
        System.out.println("du1: " + du1);
        System.out.println("du2: " + du2);

        // represents this Duration du1 in hours into
        // number of days i.e. 72H = 3 days
        long to_days = du1.toDays();

        // Display to_days
        System.out.println("du1.toDays(): " + to_days);
        
        // represents this Duration du2 in hours into
        // number of days i.e. 78H = 2 days
        to_days = du2.toDays();

        // Display to_days
        System.out.println("du2.toDays(): " + to_days);
    }
}

Output

输出量

du1: PT72H
du2: PT48H
du1.toDays(): 3
du2.toDays(): 2


翻译自: https://www.includehelp.com/java/duration-todays-method-with-example.aspx

mysql中todays

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Duration 类代表两个时间点之间的时间段,可以用于计算时间差、持续时间、时间间隔等。下面是 Duration 类的用法示例: 1. 创建 Duration 对象 可以通过 of() 方法创建 Duration 实例。例如,以下代码创建一个持续时间为 2 秒的 Duration 对象: ``` Duration duration = Duration.ofSeconds(2); ``` 也可以使用 of() 方法来创建其他时间单位的 Duration 对象,例如: ``` Duration duration = Duration.ofMinutes(5); // 持续时间为 5 分钟 Duration duration = Duration.ofHours(2); // 持续时间为 2 小时 Duration duration = Duration.ofDays(3); // 持续时间为 3 天 ``` 2. 获取 Duration 的值 可以使用 getXXX() 方法获取 Duration 对象的值,其 XXX 可以是以下任意一个时间单位: - getSeconds():获取持续时间的秒数。 - getNano():获取持续时间的纳秒数。 - toMillis():获取持续时间的毫秒数。 - toMinutes():获取持续时间的分钟数。 - toHours():获取持续时间的小时数。 - toDays():获取持续时间的天数。 例如,以下代码获取持续时间为 2 秒的 Duration 对象的秒数和毫秒数: ``` Duration duration = Duration.ofSeconds(2); long seconds = duration.getSeconds(); // 返回 2 long millis = duration.toMillis(); // 返回 2000 ``` 3. 计算时间差 可以使用 between() 方法计算两个时间点之间的时间差,返回一个 Duration 对象。例如,以下代码计算从 2021 年 1 月 1 日 0 点到现在的时间差: ``` LocalDateTime start = LocalDateTime.of(2021, 1, 1, 0, 0, 0); LocalDateTime end = LocalDateTime.now(); Duration duration = Duration.between(start, end); ``` 以上就是 Java 使用 Duration 类的基本用法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值