FormattedDateBuilder.java revision 35c3bb793ac3ea8f63ff73d282c0dabfee5a49ce
1/*
2 * Copyright (C) 2012 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package com.android.mail;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.text.format.DateUtils;
22import android.text.format.Time;
23
24import java.util.Formatter;
25
26/**
27 * Convenience class to efficiently make multiple short date strings. Instantiating and reusing
28 * one of these builders is faster than repeatedly bringing up all the locale stuff.
29 *
30 */
31public class FormattedDateBuilder {
32
33    private StringBuilder sb;
34    private Formatter dateFormatter;
35    private Context mContext;
36
37    public FormattedDateBuilder(Context context) {
38        mContext = context;
39        sb = new StringBuilder();
40        dateFormatter = new Formatter(sb);
41    }
42
43    public CharSequence formatShortDate(long when) {
44        return DateUtils.getRelativeTimeSpanString(mContext, when);
45    }
46
47    public CharSequence formatLongDateTime(long when) {
48        final Resources resources = mContext.getResources();
49
50        if (DateUtils.isToday(when)) {
51            return resources.getString(R.string.date_message_received_today, formatLongTime(when));
52        } else if (isYesterday(when)) {
53            return resources.getString(
54                    R.string.date_message_received_yesterday, formatLongTime(when));
55        } else {
56            return resources.getString(R.string.date_message_received,
57                    formatLongDayAndDate(when), formatLongTime(when));
58        }
59    }
60
61    private CharSequence formatLongDayAndDate(long when) {
62        sb.setLength(0);
63        DateUtils.formatDateRange(mContext, dateFormatter, when, when,
64                DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY
65                        | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_ALL);
66        return sb.toString();
67    }
68
69    private CharSequence formatLongTime(long when) {
70        sb.setLength(0);
71        DateUtils.formatDateRange(mContext, dateFormatter, when, when,
72                DateUtils.FORMAT_SHOW_TIME);
73        return sb.toString();
74    }
75
76    /**
77     * @return true if the supplied when is today else false
78     */
79    private static boolean isYesterday(long when) {
80        final Time time = new Time();
81        time.set(when);
82
83        final int thenYear = time.year;
84        final int thenMonth = time.month;
85        final int thenMonthDay = time.monthDay;
86
87        time.set(System.currentTimeMillis());
88        return (thenYear == time.year)
89                && (thenMonth == time.month)
90                && (thenMonthDay == (time.monthDay-1));
91    }
92}
93