How to get the date format per month and day

I am using Joda Time to format dateTime as follows:

DateTimeFormatter dateFormatter = DateTimeFormat.longDate();
String startTimeStr = dateFormatter.print(localStartTime);

Variable Values:

localStartTime={org.joda.time.LocalDateTime@830018681648}"2013-04-06T23:54:35.000"
startTimeStr={java.lang.String@830018688880}"2013年4月6日"

The problem is, how can I get the locale date format in month and day? I tried the following codes:

DateTimeFormatter monDayFormatter = DateTimeFormat.forPattern("MMMd");
String startTimeStr = monDayFormatter.print(localStartTime);

and the values ​​of the variables are:

localStartTime={org.joda.time.LocalDateTime@830018681648}"2013-04-06T23:54:35.000"
startTimeStr={java.lang.String@830018683220}"4月6"

I was expecting startTimeStr 4月6日. Here is the Chinese character = Day. I don’t want to hardcode the template to “MMMd 日” because it needs to be configured according to the current locale information. Any help would be appreciated.

+4
source share
2 answers

- CLDR, , ( = gregorian):

<dateFormats>
  <dateFormatLength type="full">
     <dateFormat>
    <pattern>y年M月d日EEEE</pattern>
     </dateFormat>
  </dateFormatLength>
  <dateFormatLength type="long">
     <dateFormat>
        <pattern>y年M月d日</pattern>
     </dateFormat>
  </dateFormatLength>
  <dateFormatLength type="medium">
     <dateFormat>
       <pattern>y年M月d日</pattern>
     </dateFormat>
  </dateFormatLength>
  <dateFormatLength type="short">
     <dateFormat>
       <pattern>yy/M/d</pattern>
     </dateFormat>
  </dateFormatLength>
</dateFormats>

JodaTime, JDK dateformat , "日". CLDR month-day-only-format, year-month-day, ,

DateTimeFormatter dateFormatter = DateTimeFormat.longDate().withLocale(Locale.CHINESE);
LocalDateTime localStartTime = new LocalDateTime(2013,4,6,23,54,35);
String startTimeStr = dateFormatter.print(localStartTime);
System.out.println(startTimeStr); // output: 2013年4月6日

:

DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("M月d日");
LocalDateTime localStartTime = new LocalDateTime(2013,4,6,23,54,35);
String startTimeStr = dateFormatter.print(localStartTime);
System.out.println(startTimeStr); // output: 4月6日

, . :

"MMMd 日"

, . , . CLDR . , - , , Map < Locale, String > .

2016-12-06:

, , Joda-Time. , Joda-Time, JSR-310 (java.time -) , . JDK-issue 8168532. , Time4J . CLDR , , . "Md", "MMMd" "MMMMd" . Time4J (, , JSR-310 ):

    PlainTimestamp tsp = Iso8601Format.EXTENDED_DATE_TIME.parse("2013-04-06T23:54:35.000");
    AnnualDate ad = AnnualDate.of(tsp.getMonth(), tsp.getDayOfMonth());
    ChronoFormatter<AnnualDate> chinese =
        ChronoFormatter.ofStyle(DisplayMode.LONG, Locale.CHINESE, AnnualDate.chronology());
    ChronoFormatter<AnnualDate> english =
        ChronoFormatter.ofStyle(DisplayMode.LONG, Locale.ENGLISH, AnnualDate.chronology());
    System.out.println(chinese.format(ad)); // 4月6日
    System.out.println(english.format(ad)); // Apr 6

, , . .

+5

:

    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat day_week = new SimpleDateFormat("EEEE");
    SimpleDateFormat month_date = new SimpleDateFormat("MMMMMMMMM");
    Date d = new Date();
    String dayOfTheWeek = day_week.format(d);
    String month = month_date.format(calendar.getTime());

, .

+2

Source: https://habr.com/ru/post/1526038/


All Articles