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

前端 未结 6 532
耶瑟儿~
耶瑟儿~ 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:25

    If standard abbreviations are fine with you, just use Calendar class like this:

    myCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
    
    0 讨论(0)
  • 2020-12-06 04:27

    The best way is with java.text.DateFormatSymbols

    DateFormatSymbols symbols = new DateFormatSymbols(new Locale("it"));
    // for the current Locale :
    //   DateFormatSymbols symbols = new DateFormatSymbols(); 
    String[] dayNames = symbols.getShortWeekdays();
    for (String s : dayNames) { 
       System.out.print(s + " ");
    }
    // output :  dom lun mar mer gio ven sab 
    
    0 讨论(0)
  • 2020-12-06 04:28

    An example using SimpleDateFormat:

    Date now = new Date();
    // EEE gives short day names, EEEE would be full length.
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US); 
    String asWeek = dateFormat.format(now);
    

    SimpleDateFormat as been around longer than the C-style String.format and System.out.printf, and I think you'd find most Java developers would be more familiar with it and more in use in existing codebases, so I'd recommend that approach.

    0 讨论(0)
  • 2020-12-06 04:29

    java.time

    Update for those using Java 8 and later.

    ZoneId zoneId = ZoneId.of("America/Los_Angeles");
    
    Instant instant = Instant.now();
    
    ZonedDateTime zDateTime = instant.atZone(zoneId);
    
    DayOfWeek day = zDateTime.getDayOfWeek();
    

    Show output.

    System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.US));
    System.out.println(day.getDisplayName(TextStyle.NARROW, Locale.US));
    

    When run. See similar code run live at IdeOne.com.

    Tue

    T

    0 讨论(0)
  • 2020-12-06 04:44
    //you need to use joda library           
    List<String> dayList = new ArrayList<String>();
    String[] days = new String[7];
    int a=2; 
    //a =starting day of week 1=sunday ,2=monday
    Calendar c2 = Calendar.getInstance(); 
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
    c2.set((Calendar.DAY_OF_WEEK),a); 
    int maxDay = c2.getActualMaximum(Calendar.DAY_OF_WEEK);
    for(int i=0;i<maxDay;i++)
    {                 
       days[i] = df.format(c2.getTime());
       c2.add(Calendar.DAY_OF_MONTH, 1);
       String dayOfWeek = new LocalDate( days[i]).dayOfWeek().getAsShortText();
       dayList.add(dayOfWeek);         
    }            
    for(int i=0;i<maxDay;i++)
    {
        System.out.print(" '"+dayList.get(i)+"'");
    }            
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题