Java 程序以不同格式打印月份
本文使用各种方法格式化月份,使用不同的库和 Java 语言中的相应导入语句。有很多方法可以在 Java 程序输出中显示月份。有时月份以数字形式书写,有时月份以长格式书写,有时以短格式书写。月份名称也用其他语言书写,例如西班牙语、法语等。
算法
步骤 1 - 要求用户输入日期。
步骤 2 - 识别日期的月份部分。
步骤 3 - 指定月份的格式。
步骤 4 - 以指定的格式打印月份。
多种方法
我们提供了使用不同方法的解决方案。
通过使用 java.time.Month
通过使用 java.util.Date 和数组
通过使用字符串及其方法
通过使用 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++){
// 第一个月份以数字 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 等。子字符串方法用于识别日期中的月份部分并以指定格式打印。
示例
import java.util.Date;
public class ShowMonth11{
public static void main(String[] args) {
// 月份存储为数组
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"};
// 获取当前日期
Date dt = new Date();
String dtstr= dt.toString();
// 打印月份
for (int mn=0; mn<12; mn++){
System.out.println("The Number " + seq_arr[mn]+ " month in the year is : " +st_arr[mn]);
}
//从日期中选择月份部分
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";
//获取日期的月份部分
String substr2 = dtstr.substring(0,2);
System.out.println("\nThe date is " + dtstr);
System.out.println("\nThe current month is " + substr2);
//获取日期的月份部分
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 表示 February。
示例
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 表示日期格式,例如 Feb、Mar 等
SimpleDateFormat formatted_mmm;
// MMMM 表示日期格式,例如 February、March 等
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));
//指定显示日期时间的格式
formatted_datestyle = new SimpleDateFormat("EEEE dd MMMM yyyy kk:mm:ss");
System.out.println(formatted_datestyle.format(dtt));
//指定显示日期时间的模式
formatted_datestyle1 = new SimpleDateFormat("dd MMM yyyy kk:mm:ss");
System.out.println(formatted_datestyle1.format(dtt));
//设置西班牙语
Locale spanishDT = new Locale("es","ES");
System.out.println("现在使用西班牙语: ");
System.out.printf(spanishDT, "month: %tB\n", dtt);
//设置法语
SimpleDateFormat dt_fr = new SimpleDateFormat("dd MMM yyyy kk:mm:ss", new Locale("fr"));
System.out.println("现在使用法语: ");
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) {
//使用日历实例
Calendar cal = Calendar.getInstance();
//从 cal 获取时间
System.out.println("当前日期是:" + cal.getTime());
// 日历月份从 0 索引开始,因此添加 1 以获得正确结果
System.out.println("当前日历月份是:" + (cal.get(Calendar.MONTH) +1 ) );
}
}
输出
当前日期为:2023 年 2 月 24 日星期五 19:12:06 IST 当前日历月份为:2
结论
在本文中,我们使用 Java 语言探索了月份的不同格式。还为所使用的每种方法提供了不同的代码示例以及输出。
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

