目录
算法
多种方法
方法 1:使用 java.time.Month
示例
输出
方法 2:使用 java.util.Date 和数组
方法 3:使用字符串及其方法
方法 4:使用 java.text.SimpleDateFormat
方法 5:使用 java.util.Calendar
结论
首页 Java java教程 Java程序以不同格式打印月份

Java程序以不同格式打印月份

Aug 28, 2023 am 10:33 AM
java 打印 月份

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Java 中的平方根 Java 中的平方根 Aug 30, 2024 pm 04:26 PM

Java 中的平方根指南。下面我们分别通过例子和代码实现来讨论平方根在Java中的工作原理。

Java 中的完美数 Java 中的完美数 Aug 30, 2024 pm 04:28 PM

Java 完美数指南。这里我们讨论定义,如何在 Java 中检查完美数?,示例和代码实现。

Java 中的阿姆斯特朗数 Java 中的阿姆斯特朗数 Aug 30, 2024 pm 04:26 PM

Java 中的阿姆斯特朗数指南。这里我们讨论一下java中阿姆斯特朗数的介绍以及一些代码。

Java 中的随机数生成器 Java 中的随机数生成器 Aug 30, 2024 pm 04:27 PM

Java 随机数生成器指南。在这里,我们通过示例讨论 Java 中的函数,并通过示例讨论两个不同的生成器。

Java中的Weka Java中的Weka Aug 30, 2024 pm 04:28 PM

Java 版 Weka 指南。这里我们通过示例讨论简介、如何使用weka java、平台类型和优点。

Java 中的史密斯数 Java 中的史密斯数 Aug 30, 2024 pm 04:28 PM

Java 史密斯数指南。这里我们讨论定义,如何在Java中检查史密斯号?带有代码实现的示例。

Java Spring 面试题 Java Spring 面试题 Aug 30, 2024 pm 04:29 PM

在本文中,我们保留了最常被问到的 Java Spring 面试问题及其详细答案。这样你就可以顺利通过面试。

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

See all articles