how to get dateformat to capitalize month and day

前端 未结 4 928
执笔经年
执笔经年 2021-01-14 10:55

I have my strings like so in my strings.xml:

EEEE
dd. MMMM         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 11:07

    There are more precise way of doing this. You can control each part of your formatter using DateFormatSymbols.

    Look at that, this is beautiful:

    Scanner s = new Scanner(System.in);
    SimpleDateFormat simpleDateFormatFrom = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat("MMM dd, yyyy");
    
    DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
    String[] months = simpleDateFormatTo.getDateFormatSymbols().getShortMonths();
    for (int i = 0; i < months.length; i++) {
        months[i] = months[i].toUpperCase();
    }
    dateFormatSymbols.setShortMonths(months);
    simpleDateFormatTo.setDateFormatSymbols(dateFormatSymbols);
    
    Date date = simpleDateFormatFrom.parse(s.next());
    System.out.println(simpleDateFormatTo.format(date));
    

提交回复
热议问题