3535 Utility class for producing strings with formatted date/time.
3636
3737 <p>
38- This class takes as inputs a format string and a representation of a date/time.
39- The format string controls how the output is generated.
38+ Most callers should avoid supplying their own format strings to this
39+ class' {@code format} methods and rely on the correctly localized ones
40+ supplied by the system. This class' factory methods return
41+ appropriately-localized {@link java.text.DateFormat} instances, suitable
42+ for both formatting and parsing dates. For the canonical documentation
43+ of format strings, see {@link java.text.SimpleDateFormat}.
4044 </p>
4145 <p>
46+ The format methods in this class takes as inputs a format string and a representation of a date/time.
47+ The format string controls how the output is generated.
48+ This class only supports a subset of the full Unicode specification.
49+ Use {@link java.text.SimpleDateFormat} if you need more.
4250 Formatting characters may be repeated in order to get more detailed representations
4351 of that field. For instance, the format character 'M' is used to
4452 represent the month. Depending on how many times that character is repeated
@@ -152,7 +160,8 @@ public class DateFormat {
152160 public static final char MINUTE = 'm' ;
153161
154162 /**
155- This designator indicates the month of the year
163+ This designator indicates the month of the year. See also
164+ {@link #STANDALONE_MONTH}.
156165
157166 Examples for September:
158167 M -> 9
@@ -162,6 +171,14 @@ public class DateFormat {
162171 */
163172 public static final char MONTH = 'M' ;
164173
174+ /**
175+ This designator indicates the standalone month of the year,
176+ necessary in some format strings in some languages. For
177+ example, Russian distinguishes between the "June" in
178+ "June" and that in "June 2010".
179+ */
180+ public static final char STANDALONE_MONTH = 'L' ;
181+
165182 /**
166183 This designator indicates the seconds of the minute.
167184
@@ -374,7 +391,7 @@ public static final char[] getDateFormatOrder(Context context) {
374391 index ++;
375392 }
376393
377- if (!foundMonth && (c == MONTH )) {
394+ if (!foundMonth && (c == MONTH || c == STANDALONE_MONTH )) {
378395 foundMonth = true ;
379396 order [index ] = MONTH ;
380397 index ++;
@@ -494,9 +511,10 @@ public static final CharSequence format(CharSequence inFormat, Calendar inDate)
494511 break ;
495512
496513 case MONTH :
497- replacement = getMonthString (inDate , count );
514+ case STANDALONE_MONTH :
515+ replacement = getMonthString (inDate , count , c );
498516 break ;
499-
517+
500518 case SECONDS :
501519 replacement = zeroPad (inDate .get (Calendar .SECOND ), count );
502520 break ;
@@ -527,14 +545,19 @@ public static final CharSequence format(CharSequence inFormat, Calendar inDate)
527545 return s .toString ();
528546 }
529547
530- private static final String getMonthString (Calendar inDate , int count ) {
548+ private static final String getMonthString (Calendar inDate , int count , int kind ) {
549+ boolean standalone = (kind == STANDALONE_MONTH );
531550 int month = inDate .get (Calendar .MONTH );
532551
533- if (count >= 4 )
534- return DateUtils .getMonthString (month , DateUtils .LENGTH_LONG );
535- else if (count == 3 )
536- return DateUtils .getMonthString (month , DateUtils .LENGTH_MEDIUM );
537- else {
552+ if (count >= 4 ) {
553+ return standalone
554+ ? DateUtils .getStandaloneMonthString (month , DateUtils .LENGTH_LONG )
555+ : DateUtils .getMonthString (month , DateUtils .LENGTH_LONG );
556+ } else if (count == 3 ) {
557+ return standalone
558+ ? DateUtils .getStandaloneMonthString (month , DateUtils .LENGTH_MEDIUM )
559+ : DateUtils .getMonthString (month , DateUtils .LENGTH_MEDIUM );
560+ } else {
538561 // Calendar.JANUARY == 0, so add 1 to month.
539562 return zeroPad (month +1 , count );
540563 }
@@ -574,7 +597,8 @@ private static final String formatZoneOffset(int offset, int count) {
574597
575598 private static final String getYearString (Calendar inDate , int count ) {
576599 int year = inDate .get (Calendar .YEAR );
577- return (count <= 2 ) ? zeroPad (year % 100 , 2 ) : String .valueOf (year );
600+ return (count <= 2 ) ? zeroPad (year % 100 , 2 )
601+ : String .format (Locale .getDefault (), "%d" , year );
578602 }
579603
580604 private static final int appendQuotedText (SpannableStringBuilder s , int i , int len ) {
@@ -615,17 +639,6 @@ private static final int appendQuotedText(SpannableStringBuilder s, int i, int l
615639 }
616640
617641 private static final String zeroPad (int inValue , int inMinDigits ) {
618- String val = String .valueOf (inValue );
619-
620- if (val .length () < inMinDigits ) {
621- char [] buf = new char [inMinDigits ];
622-
623- for (int i = 0 ; i < inMinDigits ; i ++)
624- buf [i ] = '0' ;
625-
626- val .getChars (0 , val .length (), buf , inMinDigits - val .length ());
627- val = new String (buf );
628- }
629- return val ;
642+ return String .format (Locale .getDefault (), "%0" + inMinDigits + "d" , inValue );
630643 }
631644}
0 commit comments