首頁  >  文章  >  Java  >  Java程式以不同格式列印月份

Java程式以不同格式列印月份

王林
王林轉載
2023-08-28 10:33:061284瀏覽

Java程式以不同格式列印月份

本文使用不同的方法使用不同的函式庫和 Java 語言中對應的導入語句來格式化月份。有很多方法可以在 Java 程式輸出中顯示月份。有時月份寫成數字,有時月份寫成長形式或縮寫形式。月份名稱也可以用其他語言書寫,例如西班牙語、法語等。

演算法

  • 第 1 步 - 要求使用者輸入日期。

  • 第 2 步 - 決定日期的月份部分。

  • 第 3 步 - 指定月份的格式。

  • 第 4 步 - 以指定格式列印月份。

多種方法

我們使用不同的方法提供了解決方案。

  • 透過使用 java.time.Month

  • 透過使用 java.util.Date 和陣列

  • #透過使用 String 及其方法

  • 透過使用 java.text.SimpleDateFormat

  • #透過使用 java.util.Calendar

#讓我們一一看看該程式及其輸出。

方法 1:使用 java.time.Month

在此方法中,透過指定從數字 1 開始的月份編號來列印月份。例如 -> Month.of(1) 將給出 JANUARY。

範例

import java.time.Month;
public class ShowMonth {
   public static void main(String[] args)
   {
      Month mon;
      for (int mn=1; mn<=12; mn++){
         
         // The first month starts with number 1
         mon = Month.of(mn);
         System.out.println(mon);
      }
   }
}

輸出

JANUARY
FEBRUARY
MARCH
APRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER

方法 2:使用 java.util.Date 和陣列

在這種方法中,字串陣列用於儲存月份的簡短形式,例如 Jan、Feb 等。使用 substring 方法來識別日期中的月份部分,並以指定的格式列印它。

範例

import java.util.Date;
public class ShowMonth11{
   public static void main(String[] args) {

      // Months are stored as arrays
      String[] st_arr={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec"};
      String[] seq_arr={"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth","Tenth","Eleventh","Twelfth"};

      // Fetch the current date
      Date dt = new Date();
      String dtstr= dt.toString();
      
      // printing the months
      for (int mn=0; mn<12; mn++){
         System.out.println("The Number " + seq_arr[mn]+ " month in the year is : " +st_arr[mn]);
      }

      //selecting the month part from the date
      String substr2 = dtstr.substring(4,7);
      System.out.println("\nThe Time now is " + dt);
      System.out.println("\nThe current month is " + substr2);
   }
}

輸出

The Number First month in the year is : Jan
The Number Second month in the year is : Feb
The Number Third month in the year is : Mar
The Number Fourth month in the year is : Apr
The Number Fifth month in the year is : May
The Number Sixth month in the year is : Jun
The Number Seventh month in the year is : Jul
The Number Eighth month in the year is : Aug
The Number Ninth month in the year is : Sep
The Number Tenth month in the year is : Oct
The Number Eleventh month in the year is : Nov
The Number Twelfth month in the year is : Dec
The Time now is Fri Feb 24 13:19:06 IST 2023
The current month is Feb

方法 3:使用字串及其方法

在此方法中,日期作為字串輸入。然後使用子字串方法識別並列印月份部分。即使以數字形式也可以顯示月份。

範例

public class ShowMonth22{
   public static void main(String[] args) {
      String dtstr= "02-24-2023";
      String dtstr1= "24-02-2023";
      
      //getting the month part of the date
      String substr2 = dtstr.substring(0,2);
      System.out.println("\nThe date is " + dtstr);
      System.out.println("\nThe current month is " + substr2);
      
      //getting the month part of the date
      String substr3 = dtstr1.substring(3,5);
      System.out.println("\nThe date is " + dtstr1);
      System.out.println("\nThe current month is " + substr3);
   }
}

輸出

The date is 02-24-2023
The current month is 02
The date is 24-02-2023
The current month is 02

方法 4:使用 java.text.SimpleDateFormat

在此方法中,可以指定不同的格式作為顯示日期的模式。日期中的月份格式以 MMM 形式(例如 Feb)或 MMMM 形式(例如 February)列印。它也可用於以不同的區域設定格式和語言顯示月份名稱。對於 eq,西班牙文 febrero 表示二月。

範例

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class ShowMonth33{
   public static void main(String[] args) {
      Date dtt = new Date();
      
      // MMM means date specification such as Feb, Mar etc
      SimpleDateFormat formatted_mmm;
      
      // MMMM means date specification such as February, March etc
      SimpleDateFormat formatted_mmmm;
      SimpleDateFormat formatted_datestyle;
      SimpleDateFormat formatted_datestyle1;
      formatted_mmm = new SimpleDateFormat("MMM");
      System.out.println(formatted_mmm.format(dtt));
      formatted_mmmm = new SimpleDateFormat("MMMM");
      System.out.println(formatted_mmmm.format(dtt));
      
      //Specifying the pattern of showing date time
      formatted_datestyle = new SimpleDateFormat("EEEE dd MMMM yyyy kk:mm:ss");
      System.out.println(formatted_datestyle.format(dtt));
      
      //Specifying the pattern of showing date time
      formatted_datestyle1 = new SimpleDateFormat("dd MMM yyyy kk:mm:ss");
      System.out.println(formatted_datestyle1.format(dtt));
      
      //setting for the Spanish language
      Locale spanishDT = new Locale("es","ES");
      System.out.println("Now using Spanish: ");
      System.out.printf(spanishDT, "month: %tB\n", dtt);
      
      //setting for the French language
      SimpleDateFormat dt_fr = new SimpleDateFormat("dd MMM yyyy kk:mm:ss", new Locale("fr"));
      System.out.println("Now using French: ");
      System.out.println(dt_fr.format(dtt));
   }
}

輸出

Feb
February
Friday 24 February 2023 19:48:31
24 Feb 2023 19:48:31
Now using Spanish:
month: febrero
Now using French:
24 févr. 2023 19:48:31

方法 5:使用 java.util.Calendar

在此方法中,使用 Calender 物件並使用 Calendar.MONTH 來尋找月份。這裡索引從 0 開始,因此將 1 加到 Calendar.MONTH 以在日期中列印正確的月份。

範例

import java.util.Calendar;
public class ShowMonth44{
   public static void main(String[] args) {
      
      //using Calendar instance
      Calendar cal = Calendar.getInstance();
      
      //getting Time from cal
      System.out.println("The Current Date is: " + cal.getTime());
      
      // Calendar Months start with 0 index therefore 1 is added for the correct result
      System.out.println("Current Calendar Month is : " + (cal.get(Calendar.MONTH) +1 ) );
   }
}

輸出

The Current Date is: Fri Feb 24 19:12:06 IST 2023
Current Calendar Month is: 2

結論

在本文中,我們使用 Java 語言探索了月份的不同格式。也針對所使用的每種方法給出了不同的程式碼範例以及輸出。

以上是Java程式以不同格式列印月份的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除