fixed DateUtils FirstWeekDay implemented neatly

pull/349/head
Name 8 years ago
parent 387b992ed0
commit 9df0af524a

@ -67,7 +67,6 @@ public class FrequencyChart extends ScrollableChart {
private boolean isBackgroundTransparent; private boolean isBackgroundTransparent;
private int firstWeekDay;
@NonNull @NonNull
private HashMap<Timestamp, Integer[]> frequency; private HashMap<Timestamp, Integer[]> frequency;
@ -221,7 +220,7 @@ public class FrequencyChart extends ScrollableChart {
pText.setColor(textColor); pText.setColor(textColor);
pGrid.setColor(gridColor); pGrid.setColor(gridColor);
for (String day : DateUtils.getLocaleDayNames(Calendar.SHORT, firstWeekDay)) { for (String day : DateUtils.getLocaleDayNames(Calendar.SHORT)) {
canvas.drawText(day, rGrid.right - columnWidth, canvas.drawText(day, rGrid.right - columnWidth,
rGrid.top + rowHeight / 2 + 0.25f * em, pText); rGrid.top + rowHeight / 2 + 0.25f * em, pText);
@ -266,14 +265,12 @@ public class FrequencyChart extends ScrollableChart {
initColors(); initColors();
initDateFormats(); initDateFormats();
initRects(); initRects();
initFirstWeekDay();
} }
private void initFirstWeekDay() { private void initFirstWeekDay() {
HabitsApplication app = (HabitsApplication) getContext().getApplicationContext(); HabitsApplication app = (HabitsApplication) getContext().getApplicationContext();
Preferences prefs = app.getComponent().getPreferences(); Preferences prefs = app.getComponent().getPreferences();
firstWeekDay = prefs.getFirstWeekDay();
} }
private void initColors() { private void initColors() {

@ -96,7 +96,6 @@ public class HistoryChart extends ScrollableChart {
@NonNull @NonNull
private Controller controller; private Controller controller;
private int firstWeekDay;
public HistoryChart(Context context) { public HistoryChart(Context context) {
super(context); super(context);
@ -272,7 +271,7 @@ public class HistoryChart extends ScrollableChart {
private void drawAxis(Canvas canvas, RectF location) { private void drawAxis(Canvas canvas, RectF location) {
float verticalOffset = pTextHeader.getFontSpacing() * 0.4f; float verticalOffset = pTextHeader.getFontSpacing() * 0.4f;
for (String day : DateUtils.getLocaleDayNames(Calendar.SHORT, firstWeekDay)) { for (String day : DateUtils.getLocaleDayNames(Calendar.SHORT)) {
location.offset(0, columnWidth); location.offset(0, columnWidth);
canvas.drawText(day, location.left + headerTextOffset, canvas.drawText(day, location.left + headerTextOffset,
location.centerY() + verticalOffset, pTextHeader); location.centerY() + verticalOffset, pTextHeader);
@ -343,7 +342,7 @@ public class HistoryChart extends ScrollableChart {
private float getWeekdayLabelWidth() { private float getWeekdayLabelWidth() {
float width = 0; float width = 0;
for (String w : DateUtils.getLocaleDayNames(Calendar.SHORT, firstWeekDay)) for (String w : DateUtils.getLocaleDayNames(Calendar.SHORT))
width = Math.max(width, pSquareFg.measureText(w)); width = Math.max(width, pSquareFg.measureText(w));
return width; return width;
@ -360,15 +359,9 @@ public class HistoryChart extends ScrollableChart {
initPaints(); initPaints();
initDateFormats(); initDateFormats();
initRects(); initRects();
initFirstWeekDay();
} }
private void initFirstWeekDay() {
HabitsApplication app = (HabitsApplication) getContext().getApplicationContext();
Preferences prefs = app.getComponent().getPreferences();
firstWeekDay = prefs.getFirstWeekDay();
}
private void initColors() { private void initColors() {
StyledResources res = new StyledResources(getContext()); StyledResources res = new StyledResources(getContext());
@ -429,7 +422,6 @@ public class HistoryChart extends ScrollableChart {
baseDate = DateUtils.getStartOfTodayCalendar(); baseDate = DateUtils.getStartOfTodayCalendar();
baseDate.setFirstDayOfWeek(firstWeekDay);
baseDate.add(Calendar.DAY_OF_YEAR, -(getDataOffset() - 1) * 7); baseDate.add(Calendar.DAY_OF_YEAR, -(getDataOffset() - 1) * 7);
nDays = (nColumns - 1) * 7; nDays = (nColumns - 1) * 7;

@ -46,6 +46,11 @@ class ListHabitsActivity : HabitsActivity() {
setScreen(screen) setScreen(screen)
component.listHabitsBehavior.onStartup() component.listHabitsBehavior.onStartup()
// added
DateUtils.setFirstWeekDay(prefs.firstWeekDay)
DateUtils.setFirstWeekDay(4)
} }
override fun onPause() { override fun onPause() {

@ -19,27 +19,35 @@
package org.isoron.uhabits.activities.settings; package org.isoron.uhabits.activities.settings;
import android.content.SharedPreferences;
import android.os.*; import android.os.*;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;
import org.isoron.androidbase.activities.*; import org.isoron.androidbase.activities.*;
import org.isoron.androidbase.utils.*; import org.isoron.androidbase.utils.*;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.core.utils.DateUtils;
/** /**
* Activity that allows the user to view and modify the app settings. * Activity that allows the user to view and modify the app settings.
*/ */
public class SettingsActivity extends BaseActivity public class SettingsActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
{
private SharedPreferences prefs;
@Override @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState) {
{
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity); setContentView(R.layout.settings_activity);
setupActionBarColor(); setupActionBarColor();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
} }
private void setupActionBarColor() private void setupActionBarColor() {
{
StyledResources res = new StyledResources(this); StyledResources res = new StyledResources(this);
int color = BaseScreen.getDefaultActionBarColor(this); int color = BaseScreen.getDefaultActionBarColor(this);
@ -48,4 +56,34 @@ public class SettingsActivity extends BaseActivity
BaseScreen.setupActionBarColor(this, color); BaseScreen.setupActionBarColor(this, color);
} }
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPref, String key) {
SharedPreferences.Editor editor = sharedPref.edit();
if (key.equalsIgnoreCase("hours_offset")) {
int hourOffset = 0;
try {
hourOffset = Integer.parseInt(sharedPref.getString(key, "0"));
editor.putString(key, String.valueOf(hourOffset));
DateUtils.setNewDayOffset(hourOffset);
} catch (NumberFormatException e) {
e.printStackTrace();
}
} else if (key.equalsIgnoreCase("pref_first_week_day")){
int firstWeekDay = 0;
try {
firstWeekDay = Integer.parseInt(sharedPref.getString(key, "0"));
editor.putString(key, String.valueOf(firstWeekDay));
DateUtils.setFirstWeekDay(firstWeekDay);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
Log.d("debugging", "hello, key is: " + key+", value: "+ sharedPref.getString(key, "0"));
editor.commit();
}
} }

@ -86,7 +86,7 @@
<string name="pref_toggle_title">Toggle with short press</string> <string name="pref_toggle_title">Toggle with short press</string>
<string name="pref_toggle_description">Put checkmarks with a single tap instead of press-and-hold. More convenient, but might cause accidental toggles.</string> <string name="pref_toggle_description">Put checkmarks with a single tap instead of press-and-hold. More convenient, but might cause accidental toggles.</string>
<string name="pref_snooze_interval_title">Snooze interval on reminders</string> <string name="pref_snooze_interval_title">Snooze interval on reminders</string>
<string name="pref_week_start_title">First day of week</string> <string name="pref_first_week_day_title">First day of week</string>
<string name="pref_rate_this_app">Rate this app on Google Play</string> <string name="pref_rate_this_app">Rate this app on Google Play</string>
<string name="pref_send_feedback">Send feedback to developer</string> <string name="pref_send_feedback">Send feedback to developer</string>
<string name="pref_view_source_code">View source code at GitHub</string> <string name="pref_view_source_code">View source code at GitHub</string>

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?><!--
<!--
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com> ~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
~ ~
~ This file is part of Loop Habit Tracker. ~ This file is part of Loop Habit Tracker.
@ -28,20 +27,20 @@
android:defaultValue="false" android:defaultValue="false"
android:key="pref_short_toggle" android:key="pref_short_toggle"
android:summary="@string/pref_toggle_description" android:summary="@string/pref_toggle_description"
android:title="@string/pref_toggle_title"/> android:title="@string/pref_toggle_title" />
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="pref_checkmark_reverse_order" android:key="pref_checkmark_reverse_order"
android:summary="@string/reverse_days_description" android:summary="@string/reverse_days_description"
android:title="@string/reverse_days"/> android:title="@string/reverse_days" />
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="pref_pure_black" android:key="pref_pure_black"
android:summary="@string/pure_black_description" android:summary="@string/pure_black_description"
android:title="@string/use_pure_black"/> android:title="@string/use_pure_black" />
</PreferenceCategory> </PreferenceCategory>
@ -56,23 +55,23 @@
android:entryValues="@array/snooze_interval_values" android:entryValues="@array/snooze_interval_values"
android:key="pref_snooze_interval" android:key="pref_snooze_interval"
android:summary="%s" android:summary="%s"
android:title="@string/pref_snooze_interval_title"/> android:title="@string/pref_snooze_interval_title" />
<Preference <Preference
android:key="reminderSound" android:key="reminderSound"
android:title="@string/reminder_sound"/> android:title="@string/reminder_sound" />
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="pref_sticky_notifications" android:key="pref_sticky_notifications"
android:title="@string/sticky_notifications" android:summary="@string/sticky_notifications_description"
android:summary="@string/sticky_notifications_description"/> android:title="@string/sticky_notifications" />
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="pref_led_notifications" android:key="pref_led_notifications"
android:title="@string/led_notifications" android:summary="@string/led_notifications_description"
android:summary="@string/led_notifications_description"/> android:title="@string/led_notifications" />
</PreferenceCategory> </PreferenceCategory>
@ -83,34 +82,42 @@
<Preference <Preference
android:key="exportDB" android:key="exportDB"
android:summary="@string/export_full_backup_summary" android:summary="@string/export_full_backup_summary"
android:title="@string/export_full_backup"> android:title="@string/export_full_backup"></Preference>
</Preference>
<Preference <Preference
android:key="exportCSV" android:key="exportCSV"
android:summary="@string/export_as_csv_summary" android:summary="@string/export_as_csv_summary"
android:title="@string/export_to_csv"> android:title="@string/export_to_csv"></Preference>
</Preference>
<Preference <Preference
android:key="importData" android:key="importData"
android:summary="@string/import_data_summary" android:summary="@string/import_data_summary"
android:title="@string/import_data"> android:title="@string/import_data"></Preference>
</Preference>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory android:title="Custom Settings">
android:title="Custom Settings">
<ListPreference <ListPreference
android:defaultValue="@string/week_start_default" android:defaultValue="@string/week_start_default"
android:dialogTitle="@string/week_start" android:dialogTitle="@string/week_start"
android:entries="@array/days_names" android:entries="@array/days_names"
android:entryValues="@array/days_values" android:entryValues="@array/days_values"
android:key="pref_week_start" android:key="pref_first_week_day"
android:summary="%s" android:summary="%s"
android:title="@string/pref_week_start_title"/> android:title="@string/pref_first_week_day_title" />
<EditTextPreference
android:defaultValue="3"
android:dialogTitle="hours_offset"
android:inputType="number"
android:key="hours_offset"
android:selectAllOnFocus="true"
android:summary="hours_offset"
android:title="hours_offset" />
<!--android:maxLength="2"-->
<!--android:numeric="integer"-->
</PreferenceCategory> </PreferenceCategory>
@ -120,11 +127,11 @@
<Preference <Preference
android:key="bugReport" android:key="bugReport"
android:title="@string/generate_bug_report"/> android:title="@string/generate_bug_report" />
<Preference <Preference
android:key="repairDB" android:key="repairDB"
android:title="@string/repair_database"/> android:title="@string/repair_database" />
</PreferenceCategory> </PreferenceCategory>
@ -135,20 +142,20 @@
<Preference android:title="@string/help"> <Preference android:title="@string/help">
<intent <intent
android:action="android.intent.action.VIEW" android:action="android.intent.action.VIEW"
android:data="@string/helpURL"/> android:data="@string/helpURL" />
</Preference> </Preference>
<Preference android:title="@string/pref_rate_this_app"> <Preference android:title="@string/pref_rate_this_app">
<intent <intent
android:action="android.intent.action.VIEW" android:action="android.intent.action.VIEW"
android:data="@string/playStoreURL"/> android:data="@string/playStoreURL" />
</Preference> </Preference>
<Preference android:title="@string/about"> <Preference android:title="@string/about">
<intent <intent
android:action="android.intent.action.VIEW" android:action="android.intent.action.VIEW"
android:targetClass="org.isoron.uhabits.activities.about.AboutActivity" android:targetClass="org.isoron.uhabits.activities.about.AboutActivity"
android:targetPackage="org.isoron.uhabits"/> android:targetPackage="org.isoron.uhabits" />
</Preference> </Preference>
</PreferenceCategory> </PreferenceCategory>
@ -160,25 +167,25 @@
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="pref_developer" android:key="pref_developer"
android:title="Enable developer mode"/> android:title="Enable developer mode" />
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="pref_feature_numerical_habits" android:key="pref_feature_numerical_habits"
android:title="Enable numerical habits"/> android:title="Enable numerical habits" />
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="pref_feature_sync" android:key="pref_feature_sync"
android:title="Enable cloud sync"/> android:title="Enable cloud sync" />
<EditTextPreference <EditTextPreference
android:key="pref_sync_address" android:key="pref_sync_address"
android:title="Sync server address"/> android:title="Sync server address" />
<EditTextPreference <EditTextPreference
android:key="pref_sync_key" android:key="pref_sync_key"
android:title="Sync key"/> android:title="Sync key" />
</PreferenceCategory> </PreferenceCategory>

@ -126,7 +126,7 @@ public class Preferences {
public int getFirstWeekDay() { public int getFirstWeekDay() {
Calendar calendar = new GregorianCalendar(); Calendar calendar = new GregorianCalendar();
return Integer.parseInt(storage.getString("pref_week_start", String.valueOf(calendar.getFirstDayOfWeek()))); return Integer.parseInt(storage.getString("pref_first_week_day", String.valueOf(calendar.getFirstDayOfWeek())));
} }
public String getSyncAddress() { public String getSyncAddress() {

@ -27,7 +27,8 @@ import java.util.*;
import static java.util.Calendar.*; import static java.util.Calendar.*;
public abstract class DateUtils { public abstract class DateUtils
{
private static Long fixedLocalTime = null; private static Long fixedLocalTime = null;
@ -38,7 +39,8 @@ public abstract class DateUtils {
/** /**
* Time of the day when the new day starts. * Time of the day when the new day starts.
*/ */
public static final int NEW_DAY_OFFSET = -9; public static /*final*/ int NEW_DAY_OFFSET = 3;
public static /*final*/ int FIRST_WEEK_DAY = 7;
/** /**
* Number of milliseconds in one day. * Number of milliseconds in one day.
@ -50,32 +52,38 @@ public abstract class DateUtils {
*/ */
public static final long HOUR_LENGTH = 60 * 60 * 1000; public static final long HOUR_LENGTH = 60 * 60 * 1000;
public static long applyTimezone(long localTimestamp) { public static long applyTimezone(long localTimestamp)
{
TimeZone tz = getTimezone(); TimeZone tz = getTimezone();
return localTimestamp - tz.getOffset(localTimestamp - tz.getOffset(localTimestamp)); return localTimestamp - tz.getOffset(localTimestamp - tz.getOffset(localTimestamp));
} }
public static String formatHeaderDate(GregorianCalendar day) { public static String formatHeaderDate(GregorianCalendar day)
{
Locale locale = getLocale(); Locale locale = getLocale();
String dayOfMonth = Integer.toString(day.get(DAY_OF_MONTH)); String dayOfMonth = Integer.toString(day.get(DAY_OF_MONTH));
String dayOfWeek = day.getDisplayName(DAY_OF_WEEK, SHORT, locale); String dayOfWeek = day.getDisplayName(DAY_OF_WEEK, SHORT, locale);
return dayOfWeek + "\n" + dayOfMonth; return dayOfWeek + "\n" + dayOfMonth;
} }
private static GregorianCalendar getCalendar(long timestamp) { private static GregorianCalendar getCalendar(long timestamp)
{
GregorianCalendar day = GregorianCalendar day =
new GregorianCalendar(TimeZone.getTimeZone("GMT"), getLocale()); new GregorianCalendar(TimeZone.getTimeZone("GMT"), getLocale());
day.setTimeInMillis(timestamp); day.setTimeInMillis(timestamp);
day.setFirstDayOfWeek(FIRST_WEEK_DAY);
return day; return day;
} }
private static String[] getDayNames(int format) { private static String[] getDayNames(int format)
{
String[] wdays = new String[7]; String[] wdays = new String[7];
Calendar day = new GregorianCalendar(); Calendar day = new GregorianCalendar();
day.set(DAY_OF_WEEK, Calendar.SATURDAY); day.set(DAY_OF_WEEK, Calendar.SATURDAY);
for (int i = 0; i < wdays.length; i++) { for (int i = 0; i < wdays.length; i++)
{
wdays[i] = wdays[i] =
day.getDisplayName(DAY_OF_WEEK, format, getLocale()); day.getDisplayName(DAY_OF_WEEK, format, getLocale());
day.add(DAY_OF_MONTH, 1); day.add(DAY_OF_MONTH, 1);
@ -84,7 +92,8 @@ public abstract class DateUtils {
return wdays; return wdays;
} }
public static long getLocalTime() { public static long getLocalTime()
{
if (fixedLocalTime != null) return fixedLocalTime; if (fixedLocalTime != null) return fixedLocalTime;
TimeZone tz = getTimezone(); TimeZone tz = getTimezone();
@ -96,12 +105,16 @@ public abstract class DateUtils {
* @return array with weekday names starting according to locale settings, * @return array with weekday names starting according to locale settings,
* e.g. [Mo,Di,Mi,Do,Fr,Sa,So] in Germany * e.g. [Mo,Di,Mi,Do,Fr,Sa,So] in Germany
*/ */
public static String[] getLocaleDayNames(int format, int firstWeekDay) { public static String[] getLocaleDayNames(int format)
{
String[] days = new String[7]; String[] days = new String[7];
Calendar calendar = new GregorianCalendar(); Calendar calendar = new GregorianCalendar();
calendar.set(DAY_OF_WEEK, firstWeekDay); calendar.setFirstDayOfWeek(FIRST_WEEK_DAY);
for (int i = 0; i < days.length; i++) { calendar.set(DAY_OF_WEEK, calendar.getFirstDayOfWeek());
for (int i = 0; i < days.length; i++)
{
days[i] = calendar.getDisplayName(DAY_OF_WEEK, format, days[i] = calendar.getDisplayName(DAY_OF_WEEK, format,
getLocale()); getLocale());
calendar.add(DAY_OF_MONTH, 1); calendar.add(DAY_OF_MONTH, 1);
@ -114,86 +127,104 @@ public abstract class DateUtils {
* @return array with week days numbers starting according to locale * @return array with week days numbers starting according to locale
* settings, e.g. [2,3,4,5,6,7,1] in Europe * settings, e.g. [2,3,4,5,6,7,1] in Europe
*/ */
public static Integer[] getLocaleWeekdayList() { public static Integer[] getLocaleWeekdayList()
{
Integer[] dayNumbers = new Integer[7]; Integer[] dayNumbers = new Integer[7];
Calendar calendar = new GregorianCalendar(); Calendar calendar = new GregorianCalendar();
calendar.setFirstDayOfWeek(FIRST_WEEK_DAY);
calendar.set(DAY_OF_WEEK, calendar.getFirstDayOfWeek()); calendar.set(DAY_OF_WEEK, calendar.getFirstDayOfWeek());
calendar.set(DAY_OF_WEEK, MONDAY); // NOT RESPONSIBLE for (int i = 0; i < dayNumbers.length; i++)
{
for (int i = 0; i < dayNumbers.length; i++) {
dayNumbers[i] = calendar.get(DAY_OF_WEEK); dayNumbers[i] = calendar.get(DAY_OF_WEEK);
calendar.add(DAY_OF_MONTH, 1); calendar.add(DAY_OF_MONTH, 1);
} }
return dayNumbers; return dayNumbers;
} }
public static String[] getLongDayNames() { public static String[] getLongDayNames()
{
return getDayNames(GregorianCalendar.LONG); return getDayNames(GregorianCalendar.LONG);
} }
public static String[] getShortDayNames() { public static String[] getShortDayNames()
{
return getDayNames(SHORT); return getDayNames(SHORT);
} }
@NonNull @NonNull
public static Timestamp getToday() { public static Timestamp getToday()
{
return new Timestamp(getStartOfToday()); return new Timestamp(getStartOfToday());
} }
public static long getStartOfDay(long timestamp) { public static long getStartOfDay(long timestamp)
{
return (timestamp / DAY_LENGTH) * DAY_LENGTH; return (timestamp / DAY_LENGTH) * DAY_LENGTH;
} }
public static long getStartOfToday() { public static long getStartOfToday()
{
return getStartOfDay(getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH); return getStartOfDay(getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH);
} }
public static long millisecondsUntilTomorrow() { public static long millisecondsUntilTomorrow()
{
return getStartOfToday() + DAY_LENGTH - return getStartOfToday() + DAY_LENGTH -
(getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH); (getLocalTime() - NEW_DAY_OFFSET * HOUR_LENGTH);
} }
public static GregorianCalendar getStartOfTodayCalendar() { public static GregorianCalendar getStartOfTodayCalendar()
{
return getCalendar(getStartOfToday()); return getCalendar(getStartOfToday());
} }
private static TimeZone getTimezone() { private static TimeZone getTimezone()
if (fixedTimeZone != null) return fixedTimeZone; {
if(fixedTimeZone != null) return fixedTimeZone;
return TimeZone.getDefault(); return TimeZone.getDefault();
} }
public static void setFixedTimeZone(TimeZone tz) { public static void setFixedTimeZone(TimeZone tz)
{
fixedTimeZone = tz; fixedTimeZone = tz;
} }
public static long removeTimezone(long timestamp) { public static long removeTimezone(long timestamp)
{
TimeZone tz = getTimezone(); TimeZone tz = getTimezone();
return timestamp + tz.getOffset(timestamp); return timestamp + tz.getOffset(timestamp);
} }
public static void setFixedLocalTime(Long timestamp) { public static void setFixedLocalTime(Long timestamp)
{
fixedLocalTime = timestamp; fixedLocalTime = timestamp;
} }
public static void setFixedLocale(Locale locale) { public static void setFixedLocale(Locale locale)
{
fixedLocale = locale; fixedLocale = locale;
} }
private static Locale getLocale() { private static Locale getLocale()
if (fixedLocale != null) return fixedLocale; {
if(fixedLocale != null) return fixedLocale;
return Locale.getDefault(); return Locale.getDefault();
} }
public static Long truncate(TruncateField field, long timestamp) { public static Long truncate(TruncateField field, long timestamp)
{
GregorianCalendar cal = DateUtils.getCalendar(timestamp); GregorianCalendar cal = DateUtils.getCalendar(timestamp);
cal.setFirstDayOfWeek(FIRST_WEEK_DAY);
switch (field) { switch (field)
{
case MONTH: case MONTH:
cal.set(DAY_OF_MONTH, 1); cal.set(DAY_OF_MONTH, 1);
return cal.getTimeInMillis(); return cal.getTimeInMillis();
case WEEK_NUMBER: case WEEK_NUMBER:
int firstWeekday = cal.getFirstDayOfWeek(); int firstDayOfWeek = cal.getFirstDayOfWeek();
int firstWeekday = firstDayOfWeek;
int weekday = cal.get(DAY_OF_WEEK); int weekday = cal.get(DAY_OF_WEEK);
int delta = weekday - firstWeekday; int delta = weekday - firstWeekday;
if (delta < 0) delta += 7; if (delta < 0) delta += 7;
@ -216,7 +247,20 @@ public abstract class DateUtils {
} }
} }
public enum TruncateField { public enum TruncateField
{
MONTH, WEEK_NUMBER, YEAR, QUARTER MONTH, WEEK_NUMBER, YEAR, QUARTER
} }
public static void setNewDayOffset(int offset){
NEW_DAY_OFFSET = offset;
}
public static void setFirstWeekDay(int firstWeekDay){
FIRST_WEEK_DAY = firstWeekDay;
}
} }

Loading…
Cancel
Save