0

0

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

王林

王林

发布时间:2023-08-28 10:33:06

|

1564人浏览过

|

来源于tutorialspoint

转载

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

本文使用不同的方法使用不同的库和 Java 语言中相应的导入语句来格式化月份。有很多方法可以在 Java 程序输出中显示月份。有时月份写成数字,有时月份写成长形式或缩写形式。月份名称也可以用其他语言书写,例如西班牙语、法语等。

算法

  • 第 1 步 - 要求用户输入日期。

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

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

    立即学习Java免费学习笔记(深入)”;

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

多种方法

我们使用不同的方法提供了解决方案。

  • 通过使用 java.time.Month

  • 通过使用 java.util.Date 和数组

  • 通过使用 String 及其方法

    Android的资源与国际化设置 中文WORD版
    Android的资源与国际化设置 中文WORD版

    本文档主要讲述的是Android的资源与国际化设置;资源是外部文件(不含代码的文件),它被代码使用并在编译时编入应用程序。Android支持不同类型的资源文件,包括XML,PNG以及JPEG文件XML文件根据描述的不同有不同格式。这份文档描述可以支持什么样的文件,语法,以及各种格式。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看

    下载
  • 通过使用 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 语言探索了月份的不同格式。还针对所使用的每种方法给出了不同的代码示例以及输出。

相关文章

全能打印神器
全能打印神器

全能打印神器是一款非常好用的打印软件,可以在电脑、手机、平板电脑等设备上使用。支持无线打印和云打印,操作非常简单,使用起来也非常方便,有需要的小伙伴快来保存下载体验吧!

下载

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

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

799

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

722

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

727

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

394

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

445

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

428

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16860

2023.08.03

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 2.1万人学习

C# 教程
C# 教程

共94课时 | 5.6万人学习

Java 教程
Java 教程

共578课时 | 39.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号