算法第 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); } }}
输出januaryfebruarymarchaprilmayjunejulyaugustseptemberoctobernovemberdecember
方法 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 : janthe number second month in the year is : febthe number third month in the year is : marthe number fourth month in the year is : aprthe number fifth month in the year is : maythe number sixth month in the year is : junthe number seventh month in the year is : julthe number eighth month in the year is : augthe number ninth month in the year is : septhe number tenth month in the year is : octthe number eleventh month in the year is : novthe number twelfth month in the year is : decthe time now is fri feb 24 13:19:06 ist 2023the 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-2023the current month is 02the date is 24-02-2023the 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)); }}
输出febfebruaryfriday 24 february 2023 19:48:3124 feb 2023 19:48:31now using spanish:month: febreronow 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 2023current calendar month is: 2
结论在本文中,我们使用 java 语言探索了月份的不同格式。还针对所使用的每种方法给出了不同的代码示例以及输出。
以上就是java程序以不同格式打印月份的详细内容。
