• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.contacts.util;
18 
19 import android.content.Context;
20 import android.text.format.DateFormat;
21 
22 import java.text.ParsePosition;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.Locale;
26 import java.util.TimeZone;
27 
28 /**
29  * Utility methods for processing dates.
30  */
31 public class DateUtils {
32     public static final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone("UTC");
33 
34     // All the SimpleDateFormats in this class use the UTC timezone
35     public static final SimpleDateFormat NO_YEAR_DATE_FORMAT =
36             new SimpleDateFormat("--MM-dd", Locale.US);
37     public static final SimpleDateFormat FULL_DATE_FORMAT =
38             new SimpleDateFormat("yyyy-MM-dd", Locale.US);
39     public static final SimpleDateFormat DATE_AND_TIME_FORMAT =
40             new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
41     public static final SimpleDateFormat NO_YEAR_DATE_AND_TIME_FORMAT =
42             new SimpleDateFormat("--MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
43 
44     // Variations of ISO 8601 date format.  Do not change the order - it does affect the
45     // result in ambiguous cases.
46     private static final SimpleDateFormat[] DATE_FORMATS = {
47         FULL_DATE_FORMAT,
48         DATE_AND_TIME_FORMAT,
49         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'", Locale.US),
50         new SimpleDateFormat("yyyyMMdd", Locale.US),
51         new SimpleDateFormat("yyyyMMdd'T'HHmmssSSS'Z'", Locale.US),
52         new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.US),
53         new SimpleDateFormat("yyyyMMdd'T'HHmm'Z'", Locale.US),
54     };
55 
56     private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_MONTH_FIRST =
57             new SimpleDateFormat("MMMM dd");
58 
59     private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_DAY_FIRST =
60             new SimpleDateFormat("dd MMMM");
61 
62     static {
63         for (SimpleDateFormat format : DATE_FORMATS) {
64             format.setLenient(true);
65             format.setTimeZone(UTC_TIMEZONE);
66         }
67         NO_YEAR_DATE_FORMAT.setTimeZone(UTC_TIMEZONE);
68         FORMAT_WITHOUT_YEAR_MONTH_FIRST.setTimeZone(UTC_TIMEZONE);
69         FORMAT_WITHOUT_YEAR_DAY_FIRST.setTimeZone(UTC_TIMEZONE);
70     }
71 
72     /**
73      * Parses the supplied string to see if it looks like a date. If so,
74      * returns the date.  Otherwise, returns null.
75      */
parseDate(String string)76     public static Date parseDate(String string) {
77         ParsePosition parsePosition = new ParsePosition(0);
78         for (int i = 0; i < DATE_FORMATS.length; i++) {
79             SimpleDateFormat f = DATE_FORMATS[i];
80             synchronized (f) {
81                 parsePosition.setIndex(0);
82                 Date date = f.parse(string, parsePosition);
83                 if (parsePosition.getIndex() == string.length()) {
84                     return date;
85                 }
86             }
87         }
88         return null;
89     }
90 
91     /**
92      * Parses the supplied string to see if it looks like a date. If so,
93      * returns the same date in a cleaned-up format for the user.  Otherwise, returns
94      * the supplied string unchanged.
95      */
formatDate(Context context, String string)96     public static String formatDate(Context context, String string) {
97         if (string == null) {
98             return null;
99         }
100 
101         string = string.trim();
102         if (string.length() == 0) {
103             return string;
104         }
105 
106         ParsePosition parsePosition = new ParsePosition(0);
107 
108         Date date;
109 
110         synchronized (NO_YEAR_DATE_FORMAT) {
111             date = NO_YEAR_DATE_FORMAT.parse(string, parsePosition);
112         }
113 
114         if (parsePosition.getIndex() == string.length()) {
115             java.text.DateFormat outFormat = isMonthBeforeDay(context)
116                     ? FORMAT_WITHOUT_YEAR_MONTH_FIRST
117                     : FORMAT_WITHOUT_YEAR_DAY_FIRST;
118             synchronized (outFormat) {
119                 return outFormat.format(date);
120             }
121         }
122 
123         for (int i = 0; i < DATE_FORMATS.length; i++) {
124             SimpleDateFormat f = DATE_FORMATS[i];
125             synchronized (f) {
126                 parsePosition.setIndex(0);
127                 date = f.parse(string, parsePosition);
128                 if (parsePosition.getIndex() == string.length()) {
129                     java.text.DateFormat outFormat = DateFormat.getDateFormat(context);
130                     outFormat.setTimeZone(UTC_TIMEZONE);
131                     return outFormat.format(date);
132                 }
133             }
134         }
135         return string;
136     }
137 
isMonthBeforeDay(Context context)138     public static boolean isMonthBeforeDay(Context context) {
139         char[] dateFormatOrder = DateFormat.getDateFormatOrder(context);
140         for (int i = 0; i < dateFormatOrder.length; i++) {
141             if (dateFormatOrder[i] == DateFormat.DATE) {
142                 return false;
143             }
144             if (dateFormatOrder[i] == DateFormat.MONTH) {
145                 return true;
146             }
147         }
148         return false;
149     }
150 }
151