📌  相关文章
📜  Java中的 JapaneseDate minus(long, TemporalUnit) 方法及示例

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

Java中的 JapaneseDate minus(long, TemporalUnit) 方法及示例

Java.time.chrono.JapaneseDate类的minus()方法用于在从当前日文日期中减去一定数量的时间访问器单元后获取日文日期。
句法:

public JapaneseDate minus(long amountToAdd,
                          TemporalUnit unit)

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

  • amountToSubtract:这是要从当前日本日期中减去的时间单位的值。
  • 单位:这是时间单位或计时单位的对象。

返回值:此方法在从当前日本日期中减去一定数量的时间访问器单元后返回日本日期。
以下是说明minus()方法的示例:
示例 1:

Java
// Java program to demonstrate minus() 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.now();
 
            // Display the result
            System.out.println("old japanese date: "
                               + hidate);
 
            // Subtracting japanese date
            // by using minus() method
            JapaneseDate newdate
                = hidate.minus(
                    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 minus() 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.now();
 
            // Display the result
            System.out.println("old japanese date: "
                               + hidate);
 
            // Subtracting japanese date
            // by using minus() method
            JapaneseDate newdate
                = hidate.minus(
                    4, ChronoUnit.DECADES);
 
            // 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 32-03-23
new japanese date: Japanese Heisei 32-03-01

示例 2:

Java

// Java program to demonstrate minus() 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.now();
 
            // Display the result
            System.out.println("old japanese date: "
                               + hidate);
 
            // Subtracting japanese date
            // by using minus() method
            JapaneseDate newdate
                = hidate.minus(
                    4, ChronoUnit.DECADES);
 
            // 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 32-03-23
new japanese date: Japanese Showa 55-03-23

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