📌  相关文章
📜  Java中的 JapaneseDate plus(long, TemporalUnit) 方法

📅  最后修改于: 2022-05-13 01:55:29.984000             🧑  作者: Mango

Java中的 JapaneseDate plus(long, TemporalUnit) 方法

Java.time.chrono.JapaneseDate类的plus()方法用于在与当前日本日期相加一定数量的时间存取单元后获取日本日期。

句法:

public JapaneseDate plus(long amountToAdd,
                         TemporalUnit unit)

参数:此方法将以下参数作为参数:

  • amountToAdd:这是要与当前日本日期相加的时间单位的数量
  • 单位:这是时间单位或计时单位的对象

返回值:该方法在与当前日文日期相加一定数量的时间存取单元后返回日文日期。

以下是说明plus()方法的示例:

示例 1:

Java
// Java program to demonstrate
// plus() method
  
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // Creating and initializing
            // JapaneseDate Object
            JapaneseDate hidate
                = JapaneseDate.of(
                    4000, 06, 23);
  
            // Display the result
            System.out.println(
                "Old Japanese date: "
                + hidate);
  
            // Adding some amount in Japanese date
            // by using plus() method
            JapaneseDate newdate
                = hidate.plus(
                    22,
                    ChronoUnit.DAYS);
  
            // Display the result
            System.out.println(
                "New Japanese date: "
                + newdate);
        }
        catch (DateTimeException e) {
  
            System.out.println(
                "Passed parameter can"
                + " not form a date");
  
            System.out.println(
                "Exception thrown: "
                + e);
        }
    }
}


Java
// Java program to demonstrate
// plus() method
  
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // Creating and initializing
            // JapaneseDate Object
            JapaneseDate hidate
                = JapaneseDate.of(
                    4000, 06, 23);
  
            // Display the result
            System.out.println(
                "Old Japanese date: "
                + hidate);
  
            // Adding some amount in Japanese date
            // by using minus() method
            JapaneseDate newdate
                = hidate.plus(
                    6,
                    ChronoUnit.YEARS);
  
            // Display the result
            System.out.println(
                "New Japanese date: "
                + newdate);
        }
        catch (DateTimeException e) {
  
            System.out.println(
                "Passed parameter can"
                + " not form a date");
  
            System.out.println(
                "Exception thrown: "
                + e);
        }
    }
}


输出:
Old Japanese date: Japanese Heisei 2012-06-23
New Japanese date: Japanese Heisei 2012-07-15

示例 2:

Java

// Java program to demonstrate
// plus() method
  
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // Creating and initializing
            // JapaneseDate Object
            JapaneseDate hidate
                = JapaneseDate.of(
                    4000, 06, 23);
  
            // Display the result
            System.out.println(
                "Old Japanese date: "
                + hidate);
  
            // Adding some amount in Japanese date
            // by using minus() method
            JapaneseDate newdate
                = hidate.plus(
                    6,
                    ChronoUnit.YEARS);
  
            // Display the result
            System.out.println(
                "New Japanese date: "
                + newdate);
        }
        catch (DateTimeException e) {
  
            System.out.println(
                "Passed parameter can"
                + " not form a date");
  
            System.out.println(
                "Exception thrown: "
                + e);
        }
    }
}
输出:
Old Japanese date: Japanese Heisei 2012-06-23
New Japanese date: Japanese Heisei 2018-06-23

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/JapaneseDate.html#plus-long-java.time.temporal.TemporalUnit-