当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LocalDateTime minusWeeks()用法及代码示例


LocalDateTime类minusWeeks()方法

  • minusWeeks() 方法可在java.time包。
  • minusWeeks() 方法用于从此日期时间对象中减去给定的周数并返回 LocalDateTime。
  • minusWeeks() 方法是一个非静态方法,它只能通过类对象访问,如果我们尝试使用类名访问方法,那么我们将得到一个错误。
  • minusWeeks() 方法可能会在执行减法时抛出异常。
    DateTimeException – 当计算的结果值超过限制时可能会抛出此异常。

用法:

    public LocalDateTime minusWeeks(long weeks_val);

参数:

  • long weeks_val– 表示要从此 LocalDateTime 中减去的周数。

返回值:

这个方法的返回类型是LocalDateTime,它返回保存从该 LocalDateTime 减去给定周的值的 LocalDateTime。

例:

// Java program to demonstrate the example 
// of minusWeeks(long weeks_val) method 
// of LocalDateTime

import java.time.*;

public class MinusWeeksOfLocalDateTime {
    public static void main(String args[]) {
        long weeks = 1;
        // Instantiates two LocalDateTime
        LocalDateTime da_ti1 = LocalDateTime.parse("2005-10-05T10:10:10.00");
        LocalDateTime da_ti2 = LocalDateTime.now();

        // Display da_ti1, da_ti2 and
        // weeks
        System.out.println("LocalDateTime da_ti1 and da_ti2:");
        System.out.println("da_ti1:" + da_ti1);
        System.out.println("da_ti2:" + da_ti2);
        System.out.println("weeks to subtract:" + weeks);

        System.out.println();

        // Here, this method subtracts the given
        // weeks from this date-time object 
        // i.e. here we are subracting 1 week from 
        // this object da_ti1
        LocalDateTime minus_weeks = da_ti1.minusWeeks(weeks);

        // Display minus_weeks
        System.out.print("da_ti1.minusWeeks(weeks):");
        System.out.println(minus_weeks);

        // Here, this method subtracts the given
        // weeks from this date-time object i.e.
        // here we are subracting 1 week from 
        // this object da_ti2
        minus_weeks = da_ti2.minusWeeks(weeks);

        // Display minus_weeks
        System.out.print("da_ti2.minusWeeks(weeks):");
        System.out.println(minus_weeks);
    }
}

输出

LocalDateTime da_ti1 and da_ti2:
da_ti1:2005-10-05T10:10:10
da_ti2:2020-06-08T03:03:18.495162
weeks to subtract:1

da_ti1.minusWeeks(weeks):2005-09-28T10:10:10
da_ti2.minusWeeks(weeks):2020-06-01T03:03:18.495162


相关用法


注:本文由纯净天空筛选整理自 Java LocalDateTime Class | minusWeeks() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。