1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-25 09:08:49 -05:00
k-9/src/com/fsck/k9/helper/DateFormatter.java
cketti 203dab5c64 Revert changes for issue 549 (import/export settings).
The feature will be polished in branch "issue549".
2011-02-28 22:55:34 +01:00

83 lines
2.9 KiB
Java

package com.fsck.k9.helper;
import android.content.Context;
import android.content.SharedPreferences.Editor;
import com.fsck.k9.Preferences;
import com.fsck.k9.R;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
public class DateFormatter {
private DateFormatter() {
}
private final static Calendar SAMPLE_DATE = Calendar.getInstance();
static {
SAMPLE_DATE.set(SAMPLE_DATE.get(Calendar.YEAR), SAMPLE_DATE.getActualMaximum(Calendar.MONTH), SAMPLE_DATE.getActualMaximum(Calendar.DAY_OF_MONTH));
}
public static final String SHORT_FORMAT = "SHORT";
public static final String MEDIUM_FORMAT = "MEDIUM";
public static final String DEFAULT_FORMAT = SHORT_FORMAT;
public static final String PREF_KEY = "dateFormat";
private static volatile String sChosenFormat = null;
public static String getSampleDate(Context context, String formatString) {
java.text.DateFormat formatter = getDateFormat(context, formatString);
return formatter.format(SAMPLE_DATE.getTime());
}
public static String[] getFormats(Context context) {
return context.getResources().getStringArray(R.array.date_formats);
}
private static ThreadLocal<Map<String, DateFormat>> storedFormats = new ThreadLocal<Map<String, DateFormat>>() {
@Override
public synchronized Map<String, DateFormat> initialValue() {
return new HashMap<String, DateFormat>();
}
};
public static DateFormat getDateFormat(Context context, String formatString) {
java.text.DateFormat dateFormat;
if (SHORT_FORMAT.equals(formatString)) {
dateFormat = android.text.format.DateFormat.getDateFormat(context);
} else if (MEDIUM_FORMAT.equals(formatString)) {
dateFormat = android.text.format.DateFormat.getMediumDateFormat(context);
} else {
Map<String, DateFormat> formatMap = storedFormats.get();
dateFormat = formatMap.get(formatString);
if (dateFormat == null) {
dateFormat = new SimpleDateFormat(formatString);
formatMap.put(formatString, dateFormat);
}
}
return dateFormat;
}
public static void setDateFormat(Editor editor, String formatString) {
sChosenFormat = formatString;
editor.putString(PREF_KEY, formatString);
}
public static String getFormat(Context context) {
if (sChosenFormat == null) {
Preferences prefs = Preferences.getPreferences(context);
sChosenFormat = prefs.getPreferences().getString(PREF_KEY, DEFAULT_FORMAT);
}
return sChosenFormat;
}
public static DateFormat getDateFormat(Context context) {
String formatString = getFormat(context);
return getDateFormat(context, formatString);
}
}