You can also use the Month enum from the java.time package to convert the month number to the month name. This enum is part of the modern Java date and time API, which was introduced in Java 8 and is the recommended way to work with dates and times in Java. Here's an example of how to use it:

import java.time.LocalDateTime;
import java.time.Month;

public class Main {
  public static void main(String[] args) {
    LocalDateTime now = LocalDateTime.now();
    int month = now.getMonthValue();
    Month monthEnum = Month.of(month);
    String monthName = monthEnum.name();
    System.out.println("Current month: " + monthName);
  }
}

This code will also print the current month name to the console. The Month.of(int) method converts the month number to the corresponding Month enum value, and the name() method returns the name of the enum value as a string.


Answer by ChatGPT

标签: none

评论已关闭