首页 > Java > java教程 > 正文

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

王林
发布: 2023-08-25 21:49:18
转载
1034 人浏览过

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

高效编码语言的可信度取决于它管理日期和时间的能力。在Java虚拟环境中,我们获得一些内置的工具,如日期、时间和日历来处理与日期和时间相关的问题。

  • java。直到日期类 - 在Java中,有许多包含那些对于程序非常重要的类。 Date类处理日期和时间的操作。它们是具有可克隆、可序列化和可比较接口功能的类。

  • 提取当前日期和时间 - 使用 Java 提取当前日期和时间有两种技术。

    • 日期类的实现

    • 日历类的实现

在日期类的方法中,首先我们会考虑一个包含日期的字符串。通过使用它,我们将获得日期和时间作为输出。通过声明日历类,我们将创建一个实例类,通过调用 getTime() 来获取系统的当前时间。

在今天的这篇文章中,我们将构建一些 Java 代码来通过滚动小时和月份来显示时间。

通过滚动小时和月份来显示时间的算法

在此可能的算法中,我们尝试演示如何使用 Java 通过滚动小时和月份来显示时间。

  • 第 1 步 - 开始。

  • 第 2 步 - 声明年月中各天的函数。

  • 第 3 步 - 声明,int 总计 =0。

  • 第 4 步 - i

  • 第 5 步 - 如果满足条件,则使用 isLeapYear(i) 检查闰年?

  • 第 6 步 - 否则,int i= 1。并检查 i

  • 步骤 7 - 如果满足第五个条件;总计=总计+366;或者,总计=总计+365。

  • 第 8 步 - 进行 i++ 迭代。

  • 第 9 步 - 如果满足第七个条件。

  • 第 10 步 - 然后,total=total+getNumberOfDaysInMonth(year,i);。

  • 第 11 步 - 进行 i++ 迭代。

  • 第 12 步 - 如果不是,则返回总计。

  • 第 13 步 - 终止。

通过滚动小时和月份来显示时间的语法

General Syntax:
public abstract void roll(int calendar_field, boolean up_down)

Using Date class:
package com.DataFlair.DateAndTime;
import java.util.Date;
public class CurrDateUsingDateClass{
   public static void main(String args[]) {
      Date date = new Date();
      System.out.println(date.toString());
   }
}
Using Calendar Class:
package com.DataFlair.DateAndTime;
import java.util.Calendar;
public class CurrDateUsingCalenderClass{
   public static void main(String args[]) {
      Calendar current = Calendar.getInstance();
      System.out.println(current.getTime());
   }
}
登录后复制

上面,我们提到了给定问题的可能语法。通过遵循这些语法,我们将编写一些程序来通过滚动小时和月来获取时间。

方法

  • 方法 1 - Java 程序通过滚动小时和月份来显示时间

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

在这些 Java 构建代码中,我们尝试解释如何将上述算法和语法应用于按小时和月份滚动显示时间。

示例 1

import java.util.Calendar;
import java.util.Date;

public class Calendarof2023 {
	public static void main(String[] args) throws Exception{
      Date d1 = new Date();
      Calendar c1 = Calendar.getInstance();
      Calendar c2 = Calendar.getInstance();
      c1.setTime(d1);
      c2.setTime(d1);
      System.out.println("Today the date is " + d1.toString());
      c1.roll(Calendar.MONTH, 50);
      System.out.println("Date after rolling by 50 over month will be "+ c1.getTime().toString());
      c1.roll(Calendar.HOUR, 70);
      System.out.println("Date after rolling by 70 over hours will be "+ c1.getTime().toString());
      c1.roll(Calendar.YEAR, 2);
      System.out.println("Date after rolling by 2 over year is "+ c1.getTime().toString());
      c2.roll(Calendar.MONTH, false);
      System.out.println("Date after false rolling over month will be "+ c2.getTime().toString());
      c2.roll(Calendar.HOUR, true);
      System.out.println("Date after true rolling over hour will be "+ c2.getTime().toString());
      c2.roll(Calendar.YEAR, true);
      System.out.println("Date after true rolling over year is "+ c2.getTime().toString());
	}
}
登录后复制

输出

Today the date is Mon Apr 10 10:42:31 GMT 2023
Date after rolling by 50 over month will be Sat Jun 10 10:42:31 GMT 2023
Date after rolling by 70 over hours will be Sat Jun 10 08:42:31 GMT 2023
Date after rolling by 2 over year is Tue Jun 10 08:42:31 GMT 2025
Date after false rolling over month will be Fri Mar 10 10:42:31 GMT 2023
Date after true rolling over hour will be Fri Mar 10 11:42:31 GMT 2023
Date after true rolling over year is Sun Mar 10 11:42:31 GMT 2024
登录后复制

示例 2

import java.util.*;
public class Calendar2023 {
	public static void main(String args[]){
      Calendar calndr = Calendar.getInstance();
      System.out.println("The Current Month of the year"+ calndr.get(Calendar.MONTH));
      calndr.roll(Calendar.MONTH, true);
      System.out.println("The New Month is from the year: "+ calndr.get(Calendar.MONTH));
      calndr.roll(Calendar.MONTH, false);

      // Displaying the result after operation
      System.out.println("The new month is: "+ calndr.get(Calendar.MONTH));
	}
}
登录后复制

输出

The Current Month of the year3
The New Month is from the year: 4
The new month is: 3
登录后复制

示例 3

import java.util.*;

public class Main {
   public static void main(String[] args) throws Exception {
      Date d1 = new Date();
      Calendar cl = Calendar. getInstance();
      
      cl.setTime(d1);
      System.out.println("today is the date - @ "+ d1.toString());
      
      cl. roll(Calendar.MONTH, 100);
      System.out.println("date after a month will be as per the calculation - > " + cl.getTime().toString() );
      
      cl. roll(Calendar.HOUR, 70);
      System.out.println("date after 7 hrs will be today is ->> "+ cl.getTime().toString() );
   }
}
登录后复制

输出

today is the date - @ Mon Apr 10 10:44:41 GMT 2023
date after a month will be as per the calculation - > Thu Aug 10 10:44:41 GMT 2023
date after 7 hrs will be today is ->> Thu Aug 10 08:44:41 GMT 2023
登录后复制

示例 4

import java.util.Calendar;

public class CalendarExample {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      System.out.println("Time is ----- >>:" + cal.getTime());
      
      cal.roll(Calendar.YEAR, false);
      System.out.println("Time rolling down the year, result is here--->>:" + cal.getTime());
      
      cal.roll(Calendar.HOUR, true);
      System.out.println("Time rolling up the hour is now ---->>>:" + cal.getTime());
   }
}

登录后复制

输出

Time is ----- >>:Mon Apr 10 10:45:26 GMT 2023
Time rolling down the year, result is here--->>:Sun Apr 10 10:45:26 GMT 2022
Time rolling up the hour is now ---->>>:Sun Apr 10 11:45:26 GMT 2022
登录后复制

示例 5

import java.util.*;
 
public class GetCurrentDateAndTime2023{
   public static void main(String args[]){
      int day, month, year;
      int second, minute, hour;
      GregorianCalendar date = new GregorianCalendar();
 
      day = date.get(Calendar.DAY_OF_MONTH);
      month = date.get(Calendar.MONTH);
      year = date.get(Calendar.YEAR);
 
      second = date.get(Calendar.SECOND);
      minute = date.get(Calendar.MINUTE);
      hour = date.get(Calendar.HOUR);
 
      System.out.println("Current date is now --->>  "+day+"/"+(month+1)+"/"+year);
      System.out.println("Current time is now --->> "+hour+" : "+minute+" : "+second);
   }
}
登录后复制

输出

Current date is now --->>  10/4/2023
Current time is now --->> 10 : 46 : 24
登录后复制

示例 6

package com.DataFlair.DateAndTime;
import java.util.*;
import java.text.*;
public class DateFormatting{
   public static void main(String args[]) {
      Date CurrDate = new Date( );
      SimpleDateFormat formatDate = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
      System.out.println("Current Date(Formatted) Like This:---> " + formatDate.format(CurrDate));
   }
}
登录后复制

输出

Current Date(Formatted) Like This:---> Mon 2023.04.10 at 10:47:17 AM GM
登录后复制

示例 7

import java.util.Scanner;
public class Tptimedateexample {
 
   public static void main(String[] args) {
      long totalMilliseconds = System.currentTimeMillis();
      long totalSeconds = totalMilliseconds / 1000;
      long currentSecond = (int)(totalSeconds % 60);
      long totalMinutes = totalSeconds / 60;
      long currentMinute = (int)(totalMinutes % 60);
      long totalHours = totalMinutes / 60;
      long currentHour = (int)(totalHours % 24);
      long totalDays = totalHours / 24;
      int currentYear = (int)(totalDays / 365) + 1970;
      long daysInCurrentYear = (totalDays - numberOfLeapYearsSince1970(currentYear)) % 365;
      if (currentHour > 0) daysInCurrentYear++; 
      int currentMonthNum = getMonthFromDays(currentYear, (int) daysInCurrentYear);
      String month = getMonthName(currentMonthNum);
      int daysTillCurrentMonth = getNumOfDaysToReachThatMonth(currentYear, currentMonthNum);

      int startDay = getStartDay(currentYear, currentMonthNum);
      int today = (int) daysInCurrentYear - daysTillCurrentMonth;
      String dayOfWeek = dayNameOfWeek( ((startDay + today) % 7));
      System.out.println("Current date and time is here. Note That---->>>>: " + dayOfWeek + " " + month + " " + today +", " + currentYear +" " + currentHour + ":"+ currentMinute + ":" + currentSecond );

   }

   public static String dayNameOfWeek(int dayOfWeek) {

      switch (dayOfWeek) {
         case 2: return "Monday";
         case 3: return "Tuesday";
         case 4: return "Wednesday";
         case 5: return "Thursday";
         case 6: return "Friday";
         case 7: return "Saturday";
         case 1: return "Sunday";
         default: return null;
      }
   }

   public static int numberOfLeapYearsSince1970(long year) {

      int count = 0;
      for (int i = 1970; i <= year; i++) {
         if (isLeapYear(i))count++;
      }
      return count;
   }


   public static int getMonthFromDays(int year, int days) {
      int dayTracker = 0;
      for (int i = 1; i <= 12; i++) {

         dayTracker += getNumberOfDaysInMonth(year, i);

         if (dayTracker > days) return i;
      }
      return 12;
   }

   public static int getNumOfDaysToReachThatMonth(int year, int month) {
      int dayTracker = 0;
      for (int i = 1; i < month; i++) {
         dayTracker += getNumberOfDaysInMonth(year, i);
      }
      return dayTracker;
   }
    public static int getStartDay(int year, int month) {
      final int START_DAY_FOR_JAN_1_1800 = 3;
      int totalNumberOfDays = getTotalNumberOfDays(year, month);
      return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
   }
   public static int getTotalNumberOfDays(int year, int month) {
      int total = 0;
      for (int i = 1800; i < year; i++)
         if (isLeapYear(i))
            total = total + 366;
         else
            total = total + 365;
      for (int i = 1; i < month; i++)
      total = total + getNumberOfDaysInMonth(year, i);

      return total;
   }
   public static int getNumberOfDaysInMonth(int year, int month) {

      if (month == 1 || month == 3 || month == 5 || month == 7 ||
                month == 8 || month == 10 || month == 12)
            return 31;

        if (month == 4 || month == 6 || month == 9 || month == 11)
            return 30;

        if (month == 2) return isLeapYear(year) ? 29 : 28;

      return 0; 
   }
   public static boolean isLeapYear(int year) {
      return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
   }
   public static String getMonthName(int month) {
      String monthName = "";
      switch (month) {
         case 1: monthName = "January"; break;
         case 2: monthName = "February"; break;
         case 3: monthName = "March"; break;
         case 4: monthName = "April"; break;
         case 5: monthName = "May"; break;
         case 6: monthName = "June"; break;
         case 7: monthName = "July"; break;
         case 8: monthName = "August"; break;
         case 9: monthName = "September"; break;
         case 10: monthName = "October"; break;
         case 11: monthName = "November"; break;
         case 12: monthName = "December";
      }

      return monthName;
   }

}
登录后复制

输出

Current date and time is here. Note That---->>>>: Monday April 10, 2023 10:54:30
登录后复制

结论

在这里,我们通过语法和算法了解了一些可能的java代码的时间滚动方法。希望本文能帮助您了解这里提到的各种滚动方法的操作方式,我们通过这些方法解决了这个问题。

以上是Java程序通过滚动小时和月份显示时间的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!