📜  显示日历年中工作日名称的Java程序

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

显示日历年中工作日名称的Java程序

概念:在Java中,当涉及到暴力方法后的日期和时间问题时,应该始终记住Java的Date类,它不仅提供打印当前或即将到来的年、月、日、日期、时间、小时、分钟,甚至精确到秒。不仅可以显示这些参数,还可以格式化以将它们全部显示为不同的格式。这门课领先一步。

现在为了显示日历年中工作日的名称:

方法:

  • 使用现有的日期格式类
  • 使用蛮力方法

方法 1:使用DateFormat

它用于在Java中显示数据和时间以及操作日期和时间,除此之外,它还用于在Java中跨时区关联数据格式化日期、时间、周、月、年。

所以为了从一个叫做Java.utils 的包中导入这个类

句法:

import java.util.Date ;

导入此类后,可以创建 Date 类的对象以打印当前日期和时间。现在为了打印默认日期和时间,只需使用toString()方法调用打印命令来获取当前日期和时间。假设用户想要当前时间的特定日期、时间和月份:

示例示例:在继续显示工作日名称之前阐明日期类的实现的简单代码。

Java
// Java Program to Display name of the weekdays in calendar
// year Sample code showing different data class parameters
 
// Importing Libraries
import java.util.Date;
import java.util.*;
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of Date class- date
        Date date = new Date();
 
        // Printing date
        System.out.println(date.toString());
        System.out.println(date.getTime());
 
        // Remember to add 1 to it because this print
        // current month Jan is assigned 0
        System.out.println(date.getMonth() + 1);
 
        // Remember to add 1 to it because this print
        // epoch year 1970 is set as reference
        system.out.println(date.getYear() + 1900);
 
        // For week internally it starts with Monday=1
        System.out.println(date.getDay(date));
        // no ambiguity here in displaying week
    }
}


Java
// Importing generic Classes/Files
import java.io.*;
 
// Importing specific text class for formatting
//  week via inbuilt function getweek() of data class
import java.text.DateFormatSymbols;
// Dealing with week parameter of data class
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Inbuilt function to invoke weekdays by creating
        // an object of DateFormatSymbols
        String[] week_days
            = new DateFormatSymbols().getWeekdays();
 
        // Computing length to get end bound
        // for iteration
        int length = week_days.length;
 
        // Loop is started with 2 because 0th day is
        // saturday and 1st day is sunday
        for (int d = 2; d < (length - 1); d++) {
 
            // Iterating over the string array of weekdays
            // to get respective names
            String day = week_days[d];
 
            // Printing ith index weekday
            System.out.println((d - 1) + "th weekday - "
                               + day);
        }
    }
}


Java
// Importing generic Classes/Files
import java.io.*;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a list of weekdays
        String[] weekdays
            = { "Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday" };
 
        // Iterating 5 times only
        // since there are 5 weekdays
        for (int d = 1; d < 6; d++) {
 
            // Message printing weekdays in calendar year
            System.out.println(d + "th weekday - "
                               + weekdays[d - 1]);
        }
    }
}



所以现在如果要以不同格式打印日期和时间,自定义日期,时间,工作日,年份等,这是问题陈述的目的。

  1. 要导入名为 text 的类
  2. 然后使用一个名为- SimpleDateFormat 的类
  3. 现在在此之后需要调用一个方法格式

句法:

import java.text.*;

以下Java代码说明了调用的内置类的用法:

Java

// Importing generic Classes/Files
import java.io.*;
 
// Importing specific text class for formatting
//  week via inbuilt function getweek() of data class
import java.text.DateFormatSymbols;
// Dealing with week parameter of data class
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Inbuilt function to invoke weekdays by creating
        // an object of DateFormatSymbols
        String[] week_days
            = new DateFormatSymbols().getWeekdays();
 
        // Computing length to get end bound
        // for iteration
        int length = week_days.length;
 
        // Loop is started with 2 because 0th day is
        // saturday and 1st day is sunday
        for (int d = 2; d < (length - 1); d++) {
 
            // Iterating over the string array of weekdays
            // to get respective names
            String day = week_days[d];
 
            // Printing ith index weekday
            System.out.println((d - 1) + "th weekday - "
                               + day);
        }
    }
}
输出
1th weekday - Monday
2th weekday - Tuesday
3th weekday - Wednesday
4th weekday - Thursday
5th weekday - Friday

DateFormatSymbols 是Java中的一个内置类,可公开用于组合各种日期时间格式化数据实体,如月份名称、工作日和时区相关数据。 SimpleDateFormat 使用 DateFormatSymbols 对捕获的信息进行封装。此类支持 DateFormatSymbols 的内置方法 getWeekdays(),该方法用于检索日历的工作日名称。所有的天数都以字符串格式返回。该方法在Java中具有以下语法:

String[] getWeekdays() 
  • 该方法不接受任何参数或参数。
  • 它以字符串数组的形式返回日历工作日的名称。

方法二:蛮力

日历中有五个工作日,可以以字符串数组的形式进行维护,并模拟 for 或 while 循环遍历包含工作日的数组或简单的 switch case。

Java

// Importing generic Classes/Files
import java.io.*;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a list of weekdays
        String[] weekdays
            = { "Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday" };
 
        // Iterating 5 times only
        // since there are 5 weekdays
        for (int d = 1; d < 6; d++) {
 
            // Message printing weekdays in calendar year
            System.out.println(d + "th weekday - "
                               + weekdays[d - 1]);
        }
    }
}
输出
1th weekday - Monday
2th weekday - Tuesday
3th weekday - Wednesday
4th weekday - Thursday
5th weekday - Friday