📜  以短格式显示日历年月份名称的Java程序

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

以短格式显示日历年月份名称的Java程序

我们知道,在一个日历年中,总共有 12 个月。为了将月份名称转换为更短的格式,基本上有两种方法,即我们可以使用DateFormatSymbols 类或SimpleDateFormat 类。这些类具有用于将月份名称转换为较短格式的方法,例如,如果月份名称是十月,那么在较短的格式中,它将表示为十月。

DateFormatSymbols 类和 SimpleDateFormat 类之间的主要区别在于 DateFormatSymbols 类登记日历年的所有月份,而 SimpleDateFormat 类登记该月的特定日期以及当前月份和年份。

以下是讨论将日历年中的月份名称转换为较短格式的方法:

  • 使用 DateFormatSymbols 类
  • 使用 SimpleDateFormat 类

示例 1:使用 DateFormatSymbols 类以较短的格式显示月份

Java
// Java program to convert the names of the months into the
// shorter format using DateFormatSymbols class
 
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
 
public class GFG {
    public static void main(String[] args)
    {
        // making the object of the DateFormatSymbols class
        DateFormatSymbols dateFormatSymbolsobject = new DateFormatSymbols();
       
        // calling the method of the DateFormatSymbols class
        String[] shortFormatMonthsNames = dateFormatSymbolsobject.getShortMonths();
 
        for (int i = 0;i < (shortFormatMonthsNames.length - 1); i++) {
           
            // getting the month name  from particular index
            String shortMonthName = shortFormatMonthsNames[i];
           
            System.out.println("Name of Month In Shorter Format "
                + shortMonthName);
        }
    }
}


Java
// Java program to display the name of the month in shorter
// format using SimpleDateFormat class
 
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
 
public class GFG {
    public static void main(String[] argv) throws Exception
    {
        // The format of the date which we want to display
        String dateFormat = "dd-MMM-yyyy";
       
        // getting the date using getTime() method of the
        // Calendar class
        Date d = Calendar.getInstance().getTime();
       
        // getting the date in the form of dateFormat String
        // that we have mentioned above we have mentioned
        // Locale.English explicitly since,if we write
        // Locale.FRENCH,then we would have get the date in
        // some other notation
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
       
        // printing the date with the month name in the
        // shorter format
        System.out.println(sdf.format(d));
    }
}



输出
Name of Month In Shorter Format Jan
Name of Month In Shorter Format Feb
Name of Month In Shorter Format Mar
Name of Month In Shorter Format Apr
Name of Month In Shorter Format May
Name of Month In Shorter Format Jun
Name of Month In Shorter Format Jul
Name of Month In Shorter Format Aug
Name of Month In Shorter Format Sep
Name of Month In Shorter Format Oct
Name of Month In Shorter Format Nov
Name of Month In Shorter Format Dec

示例 2:使用 SimpleDateFormat 类以较短的格式显示月份

Java

// Java program to display the name of the month in shorter
// format using SimpleDateFormat class
 
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
 
public class GFG {
    public static void main(String[] argv) throws Exception
    {
        // The format of the date which we want to display
        String dateFormat = "dd-MMM-yyyy";
       
        // getting the date using getTime() method of the
        // Calendar class
        Date d = Calendar.getInstance().getTime();
       
        // getting the date in the form of dateFormat String
        // that we have mentioned above we have mentioned
        // Locale.English explicitly since,if we write
        // Locale.FRENCH,then we would have get the date in
        // some other notation
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
       
        // printing the date with the month name in the
        // shorter format
        System.out.println(sdf.format(d));
    }
}


输出
01-Feb-2021