如何将yyyymm数据格式解析为Java中的Month,YYYY格式?

How to parse yyyymm data format to Month, YYYY format in Java?

我有一个等于 "202004" 的字符串值。如何将它转换为 Java 中的 "April, 2020"

使用 DateFormatSymbols() class 从字符串中实现新的日期格式

 String text="202011";
        int num=0;
        //Checking the last second character of the text for jan to sept month
        if(text.charAt(text.length()-2)==0){
            num=Integer.parseInt(""+text.charAt(text.length()-1))-1;
        }
        else {
            num=Integer.parseInt(""+text.substring(text.length()-2))-1;
        }
        //Checking correct month value
        if(num>=0&&num<=11){
        
        String month = "";
       
        DateFormatSymbols date_ = new DateFormatSymbols();
        String[] month_name = date_.getMonths();
        month = month_name[num];
       
        System.out.println(month+","+text.substring(0,4));
    }
    else{
        System.out.println("Wrong month value");
    }
SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMM");
SimpleDateFormat newFormat = new SimpleDateFormat("MMMM, yyyy");
Date date = null;
    try {
        date = oldFormat.parse("202004");
    } catch (ParseException e) {
        e.printStackTrace();
    }
String newDateString = newFormat.format(date);

SimpleDateFormat documentation

我会用 java.time 来完成这样的任务。

您可以定义两个java.time.format.DateTimeFormatter实例(一个用于将输入字符串解析为java.time.YearMonth,另一个用于将获得的YearMonth格式化为所需格式的字符串)。

像这样定义一个方法:

public static String convert(String monthInSomeYear, Locale locale) {
    // create something that is able to parse such input
    DateTimeFormatter inputParser = DateTimeFormatter.ofPattern("uuuuMM");
    // then use that formatter in order to create an object of year and month
    YearMonth yearMonth = YearMonth.parse(monthInSomeYear, inputParser);
    /*
     * the output format is different, so create another formatter
     * with a different pattern. Please note that you need a Locale
     * in order to define the language used for month names in the output.
     */
    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(
                                            "MMMM, uuuu",
                                            Locale.ENGLISH
                                        );
    // then return the desired format
    return yearMonth.format(outputFormatter);
}

然后使用它,例如在 main 方法中:

public static void main(String[] args) {
    // your example input
    String monthInAYear = "202004";
    // use the method
    String sameMonthInAYear = convert(monthInAYear, Locale.ENGLISH);
    // and print the result
    System.out.println(sameMonthInAYear);
}

输出将是这样的:

April, 2020

让我们试试我的代码片段:

SimpleDateFormat inSdf = new SimpleDateFormat("yyyyMM");
Date date = inSdf.parse("202112");
        
SimpleDateFormat outSdf = new SimpleDateFormat("MMMM, yyyy");
String sDate = outSdf.format(date);
System.out.println(sDate);

结果:

December, 2021

使用下面一行代码格式化年月

int yearMonth = Integer.parseInt("202004"); 
String yearMonthStr = new DateFormatSymbols().getMonths()[(yearMonth % 10)-1] + ", "+yearMonth/100;
System.out.println(yearMonthStr);