How to get localized short day-in-week name (Mo/Tu/We/Th…)

前端 未结 6 541
耶瑟儿~
耶瑟儿~ 2020-12-06 04:11

Can I get localized short day-in-week name (Mo/Tu/We/Th/Fr/Sa/Su for English) in Java?

6条回答
  •  暖寄归人
    2020-12-06 04:46

    You can't do it with the Calendar class (unless you write your own), but you can with the Date class. (The two are usually used hand-in-hand).

    Here's an example:

    import java.util.Date;
    
    public class DateFormatExample {
    
      public static void main(String[] args) {
        Calendar nowCal = Calendar.getInstance(); // a Calendar date
        Date now = new Date(nowCal.getTimeInMillis()); // convert to Date
        System.out.printf("localized month name: %tB/%TB\n", now, now); 
        System.out.printf("localized, abbreviated month: %tb/%Tb\n", now, now); 
        System.out.printf("localized day name: %tA/%TA\n", now, now);
        System.out.printf("localized, abbreviated day: %ta/%Ta\n", now, now); 
      }
    }
    

    Output:

    localized month name: June/JUNE 
    localized, abbreviated month: Jun/JUN 
    localized day name: Friday/FRIDAY 
    localized, abbreviated day: Fri/FRI
    

提交回复
热议问题