📜  通过滚动小时和月来显示时间的Java程序

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

通过滚动小时和月来显示时间的Java程序

Java.util 包有一个 Calendar 类,它有一些预定义的方法,可以帮助我们做一些与日期和时间相关的操作。 Calendar 类有一个预定义的方法叫做 roll() 方法,借助它我们可以滚动月份(不改变年份)和小时(不改变月份和年份)。

滚动基本上意味着在一段时间后回到相同的状态。例如,假设现在是 10 月份,我们将月份滚动了 50,这意味着我们滚动了 2 个月,即 (50%12=2),因为总共有 12 个月在一年中,因此滚动 50 个月后的当前月份将是 12 月。

考虑另一个例子,假设我们已经滚动了 76 小时,当前时间状态是下午 5:00,日期是 2020 年 10 月 22 日,所以我们滚动了 76 小时后,时间状态将是 9:00 pm 和 data 将是 2020 年 10 月 22 日,这是因为一天总共有 24 小时和 (76%24=4),所以当前时间状态只会增加 4。

句法:

public abstract void roll(int calendar_field, boolean up_down)

在这里, roll()方法采用 2 个参数calendar_fieldup_down

  • 日历字段 是我们想要滚动的小时、月份或年份
  • up_down :它是我们必须滚动的数量,它是一个布尔值或一个整数值。在上面的例子中,展示了如何滚动一个整数值(它可以是正值也可以是负值),但是对于布尔值,如果传递的值为true,则意味着我们必须滚动按+1数量,如果传递的值为 false,则表示滚动-1数量。

示例:下面的示例解释了如何以编程方式使用 roll() 方法。

Java
// Java program to show how to roll through
// hours or years or months using roll() method
import java.util.Calendar;
import java.util.Date;
  
public class CalendarExample1 {
    public static void main(String[] args) throws Exception
    {
        // getting the current date
        Date d1 = new Date();
        
          // create two calendar instances
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        
        // setting the current date and time in calendar
        c1.setTime(d1);
        c2.setTime(d1);
            
          // prin the current date
        System.out.println("Today is " + d1.toString());
  
        // rolling over month by 50
        c1.roll(Calendar.MONTH, 50);
        System.out.println(
            "Date after rolling by 50 over  month will be "
            + c1.getTime().toString());
  
        // rolling over hour by 70
        c1.roll(Calendar.HOUR, 70);
        System.out.println(
            "Date after rolling by 70 over hours will be "
            + c1.getTime().toString());
  
        // rolling over year by 2
        c1.roll(Calendar.YEAR, 2);
        System.out.println(
            "Date after  rolling by 2 over year is "
            + c1.getTime().toString());
  
        // false rolling over month
        c2.roll(Calendar.MONTH, false);
        System.out.println(
            "Date after false rolling over month will be "
            + c2.getTime().toString());
  
        // true rolling over hour
        c2.roll(Calendar.HOUR, true);
        System.out.println(
            "Date after true rolling over hour will be "
            + c2.getTime().toString());
  
        // true rolling over year
        c2.roll(Calendar.YEAR, true);
        System.out.println(
            "Date after true rolling over year is "
            + c2.getTime().toString());
    }
}


Java
// Java code to illustrate
// roll() method
  
import java.util.*;
  
public class CalendarExample2 {
    public static void main(String args[])
    {
        // Creating a calendar
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the month
        System.out.println("The Current Month"
                           + " is: "
                           + calndr.get(Calendar.MONTH));
  
        // Incrementing the month
        // true indicates addition
        calndr.roll(Calendar.MONTH, true);
  
        // Displaying the result after operation
        System.out.println("The New Month is: "
                           + calndr.get(Calendar.MONTH));
  
        // Decrementing the month
        // false indicates subtraction
        calndr.roll(Calendar.MONTH, false);
  
        // Displaying the result after operation
        System.out.println("The new month is: "
                           + calndr.get(Calendar.MONTH));
    }
}


输出
Today is Fri Oct 30 06:19:15 UTC 2020
Date after rolling by 50 over  month will be Wed Dec 30 06:19:15 UTC 2020
Date after rolling by 70 over hours will be Wed Dec 30 04:19:15 UTC 2020
Date after  rolling by 2 over year is Fri Dec 30 04:19:15 UTC 2022
Date after false rolling over month will be Wed Sep 30 06:19:15 UTC 2020
Date after true rolling over hour will be Wed Sep 30 07:19:15 UTC 2020
Date after true rolling over year is Thu Sep 30 07:19:15 UTC 2021

示例 2:

Java

// Java code to illustrate
// roll() method
  
import java.util.*;
  
public class CalendarExample2 {
    public static void main(String args[])
    {
        // Creating a calendar
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the month
        System.out.println("The Current Month"
                           + " is: "
                           + calndr.get(Calendar.MONTH));
  
        // Incrementing the month
        // true indicates addition
        calndr.roll(Calendar.MONTH, true);
  
        // Displaying the result after operation
        System.out.println("The New Month is: "
                           + calndr.get(Calendar.MONTH));
  
        // Decrementing the month
        // false indicates subtraction
        calndr.roll(Calendar.MONTH, false);
  
        // Displaying the result after operation
        System.out.println("The new month is: "
                           + calndr.get(Calendar.MONTH));
    }
}
输出
The Current Month is: 9
The New Month is: 10
The new month is: 9