Hiển thị tháng trong định dạng viết tắt trong Java



Miêu tả vấn đề

Cách hiển thị tháng trong định dạng viết tắt trong Java?

Giải pháp

Ví dụ sau minh họa cách hiển thị tháng trong định dạng viết tắt bởi sử dụng phương thức DateFormatSymbols().getShortMonths() của lớp DateFormatSymbols trong Java.

import java.text.SimpleDateFormat;
import java.text.DateFormatSymbols;public class Main {
   public static void main(String[] args) {
      String[] shortMonths = new DateFormatSymbols()
      .getShortMonths();
      for (int i = 0; i < (shortMonths.length-1); i++) {
         String shortMonth = shortMonths[i];
         System.out.println("shortMonth = " + shortMonth);
      }
   }
}

Kết quả

Code trên sẽ cho kết quả sau:

shortMonth = Jan
shortMonth = Feb
shortMonth = Mar
shortMonth = Apr
shortMonth = May
shortMonth = Jun
shortMonth = Jul
shortMonth = Aug
shortMonth = Sep
shortMonth = Oct
shortMonth = Nov
shortMonth = Dec

date_time_trong_java.jsp