mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Refactor reminders; replace int by WeekdayList
This commit is contained in:
@@ -45,7 +45,7 @@ public class HabitLogger
|
||||
int min = Math.min(3, habit.getName().length());
|
||||
String name = habit.getName().substring(0, min);
|
||||
|
||||
DateFormat df = DateUtils.getBackupDateFormat();
|
||||
DateFormat df = DateFormats.getBackupDateFormat();
|
||||
String time = df.format(new Date(reminderTime));
|
||||
|
||||
Log.i("ReminderHelper",
|
||||
|
||||
@@ -151,7 +151,7 @@ public class HabitsCSVExporter
|
||||
|
||||
private String writeZipFile() throws IOException
|
||||
{
|
||||
SimpleDateFormat dateFormat = DateUtils.getCSVDateFormat();
|
||||
SimpleDateFormat dateFormat = DateFormats.getCSVDateFormat();
|
||||
String date = dateFormat.format(DateUtils.getStartOfToday());
|
||||
String zipFilename =
|
||||
String.format("%s/Loop Habits CSV %s.zip", exportDirName, date);
|
||||
|
||||
@@ -203,7 +203,7 @@ public class RewireDBImporter extends AbstractImporter
|
||||
|
||||
int hour = rewireReminder / 60;
|
||||
int minute = rewireReminder % 60;
|
||||
Integer days = DateUtils.packWeekdayList(reminderDays);
|
||||
WeekdayList days = new WeekdayList(reminderDays);
|
||||
|
||||
Reminder reminder = new Reminder(hour, minute, days);
|
||||
habit.setReminder(reminder);
|
||||
|
||||
@@ -161,7 +161,7 @@ public abstract class CheckmarkList
|
||||
|
||||
int values[] = getAllValues();
|
||||
long timestamp = DateUtils.getStartOfToday();
|
||||
SimpleDateFormat dateFormat = DateUtils.getCSVDateFormat();
|
||||
SimpleDateFormat dateFormat = DateFormats.getCSVDateFormat();
|
||||
|
||||
for (int value : values)
|
||||
{
|
||||
|
||||
@@ -19,29 +19,25 @@
|
||||
|
||||
package org.isoron.uhabits.models;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
public final class Reminder
|
||||
{
|
||||
private final int hour;
|
||||
|
||||
private final int minute;
|
||||
|
||||
private final int days;
|
||||
private final WeekdayList days;
|
||||
|
||||
public Reminder(int hour, int minute, int days)
|
||||
public Reminder(int hour, int minute, @NonNull WeekdayList days)
|
||||
{
|
||||
this.hour = hour;
|
||||
this.minute = minute;
|
||||
this.days = days;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the days of the week the reminder should be shown.
|
||||
* <p>
|
||||
* This field can be converted to a list of booleans using the method
|
||||
* DateUtils.unpackWeekdayList and converted back to an integer by using the
|
||||
* method DateUtils.packWeekdayList.
|
||||
*/
|
||||
public int getDays()
|
||||
@NonNull
|
||||
public WeekdayList getDays()
|
||||
{
|
||||
return days;
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ public abstract class ScoreList implements Iterable<Score>
|
||||
public void writeCSV(Writer out) throws IOException
|
||||
{
|
||||
computeAll();
|
||||
SimpleDateFormat dateFormat = DateUtils.getCSVDateFormat();
|
||||
SimpleDateFormat dateFormat = DateFormats.getCSVDateFormat();
|
||||
|
||||
for (Score s : this)
|
||||
{
|
||||
|
||||
65
app/src/main/java/org/isoron/uhabits/models/WeekdayList.java
Normal file
65
app/src/main/java/org/isoron/uhabits/models/WeekdayList.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.models;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class WeekdayList
|
||||
{
|
||||
public static WeekdayList EVERY_DAY = new WeekdayList(127);
|
||||
|
||||
private final boolean[] weekdays;
|
||||
|
||||
public WeekdayList(int packedList)
|
||||
{
|
||||
weekdays = new boolean[7];
|
||||
int current = 1;
|
||||
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if ((packedList & current) != 0) weekdays[i] = true;
|
||||
current = current << 1;
|
||||
}
|
||||
}
|
||||
|
||||
public WeekdayList(boolean weekdays[])
|
||||
{
|
||||
this.weekdays = Arrays.copyOf(weekdays, 7);
|
||||
}
|
||||
|
||||
public boolean[] toArray()
|
||||
{
|
||||
return weekdays;
|
||||
}
|
||||
|
||||
public int toInteger()
|
||||
{
|
||||
int packedList = 0;
|
||||
int current = 1;
|
||||
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if (weekdays[i]) packedList |= current;
|
||||
current = current << 1;
|
||||
}
|
||||
|
||||
return packedList;
|
||||
}
|
||||
}
|
||||
@@ -154,7 +154,7 @@ public class HabitRecord extends Model implements SQLiteRecord
|
||||
Reminder reminder = model.getReminder();
|
||||
this.reminderHour = reminder.getHour();
|
||||
this.reminderMin = reminder.getMinute();
|
||||
this.reminderDays = reminder.getDays();
|
||||
this.reminderDays = reminder.getDays().toInteger();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,8 +186,8 @@ public class HabitRecord extends Model implements SQLiteRecord
|
||||
|
||||
if (reminderHour != null && reminderMin != null)
|
||||
{
|
||||
habit.setReminder(
|
||||
new Reminder(reminderHour, reminderMin, reminderDays));
|
||||
habit.setReminder(new Reminder(reminderHour, reminderMin,
|
||||
new WeekdayList(reminderDays)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -241,8 +241,7 @@ public class ReminderReceiver extends BroadcastReceiver
|
||||
Long timestamp =
|
||||
intent.getLongExtra("timestamp", DateUtils.getStartOfToday());
|
||||
|
||||
boolean reminderDays[] =
|
||||
DateUtils.unpackWeekdayList(reminder.getDays());
|
||||
boolean reminderDays[] = reminder.getDays().toArray();
|
||||
int weekday = DateUtils.getWeekday(timestamp);
|
||||
|
||||
return reminderDays[weekday];
|
||||
|
||||
@@ -26,7 +26,6 @@ import android.view.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.tasks.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import java.io.*;
|
||||
@@ -72,7 +71,7 @@ public class BaseSystem
|
||||
@NonNull
|
||||
public File dumpBugReportToFile() throws IOException
|
||||
{
|
||||
String date = DateUtils.getBackupDateFormat().format(
|
||||
String date = DateFormats.getBackupDateFormat().format(
|
||||
DateUtils.getLocalTime());
|
||||
|
||||
if (context == null) throw new RuntimeException(
|
||||
|
||||
@@ -281,8 +281,8 @@ public class FrequencyChart extends ScrollableChart
|
||||
|
||||
private void initDateFormats()
|
||||
{
|
||||
dfMonth = DateUtils.getDateFormat("MMM");
|
||||
dfYear = DateUtils.getDateFormat("yyyy");
|
||||
dfMonth = DateFormats.fromSkeleton("MMM");
|
||||
dfYear = DateFormats.fromSkeleton("yyyy");
|
||||
}
|
||||
|
||||
private void initRects()
|
||||
|
||||
@@ -385,8 +385,8 @@ public class HistoryChart extends ScrollableChart
|
||||
|
||||
private void initDateFormats()
|
||||
{
|
||||
dfMonth = DateUtils.getDateFormat("MMM");
|
||||
dfYear = DateUtils.getDateFormat("yyyy");
|
||||
dfMonth = DateFormats.fromSkeleton("MMM");
|
||||
dfYear = DateFormats.fromSkeleton("yyyy");
|
||||
}
|
||||
|
||||
private void initRects()
|
||||
|
||||
@@ -412,9 +412,9 @@ public class ScoreChart extends ScrollableChart
|
||||
|
||||
private void initDateFormats()
|
||||
{
|
||||
dfYear = DateUtils.getDateFormat("yyyy");
|
||||
dfMonth = DateUtils.getDateFormat("MMM");
|
||||
dfDay = DateUtils.getDateFormat("d");
|
||||
dfYear = DateFormats.fromSkeleton("yyyy");
|
||||
dfMonth = DateFormats.fromSkeleton("MMM");
|
||||
dfDay = DateFormats.fromSkeleton("d");
|
||||
}
|
||||
|
||||
private void initPaints()
|
||||
|
||||
@@ -31,7 +31,6 @@ import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.ui.common.dialogs.*;
|
||||
import org.isoron.uhabits.utils.DateUtils;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import java.util.*;
|
||||
@@ -102,7 +101,7 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
Reminder reminder = modifiedHabit.getReminder();
|
||||
outState.putInt("reminderMin", reminder.getMinute());
|
||||
outState.putInt("reminderHour", reminder.getHour());
|
||||
outState.putInt("reminderDays", reminder.getDays());
|
||||
outState.putInt("reminderDays", reminder.getDays().toInteger());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +124,8 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
|
||||
if (hour >= 0 && minute >= 0)
|
||||
{
|
||||
Reminder reminder = new Reminder(hour, minute, days);
|
||||
Reminder reminder =
|
||||
new Reminder(hour, minute, new WeekdayList(days));
|
||||
modifiedHabit.setReminder(reminder);
|
||||
}
|
||||
}
|
||||
@@ -173,7 +173,7 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
|
||||
WeekdayPickerDialog dialog = new WeekdayPickerDialog();
|
||||
dialog.setListener(new OnWeekdaysPickedListener());
|
||||
dialog.setSelectedDays(DateUtils.unpackWeekdayList(reminder.getDays()));
|
||||
dialog.setSelectedDays(reminder.getDays().toArray());
|
||||
dialog.show(getFragmentManager(), "weekdayPicker");
|
||||
}
|
||||
|
||||
@@ -215,7 +215,7 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
public void onTimeSet(RadialPickerLayout view, int hour, int minute)
|
||||
{
|
||||
Reminder reminder =
|
||||
new Reminder(hour, minute, DateUtils.ALL_WEEK_DAYS);
|
||||
new Reminder(hour, minute, WeekdayList.EVERY_DAY);
|
||||
modifiedHabit.setReminder(reminder);
|
||||
helper.populateReminderFields(modifiedHabit);
|
||||
}
|
||||
@@ -232,7 +232,7 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
Reminder oldReminder = modifiedHabit.getReminder();
|
||||
modifiedHabit.setReminder(
|
||||
new Reminder(oldReminder.getHour(), oldReminder.getMinute(),
|
||||
DateUtils.packWeekdayList(selectedDays)));
|
||||
new WeekdayList(selectedDays)));
|
||||
helper.populateReminderFields(modifiedHabit);
|
||||
}
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ public class BaseDialogHelper
|
||||
tvReminderTime.setText(time);
|
||||
llReminderDays.setVisibility(View.VISIBLE);
|
||||
|
||||
boolean weekdays[] = DateUtils.unpackWeekdayList(reminder.getDays());
|
||||
boolean weekdays[] = reminder.getDays().toArray();
|
||||
tvReminderDays.setText(
|
||||
DateUtils.formatWeekdayList(frag.getContext(), weekdays));
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ public abstract class DatabaseUtils
|
||||
{
|
||||
File db = getDatabaseFile();
|
||||
|
||||
SimpleDateFormat dateFormat = DateUtils.getBackupDateFormat();
|
||||
SimpleDateFormat dateFormat = DateFormats.getBackupDateFormat();
|
||||
String date = dateFormat.format(DateUtils.getLocalTime());
|
||||
File dbCopy = new File(
|
||||
String.format("%s/Loop Habits Backup %s.db", dir.getAbsolutePath(),
|
||||
|
||||
62
app/src/main/java/org/isoron/uhabits/utils/DateFormats.java
Normal file
62
app/src/main/java/org/isoron/uhabits/utils/DateFormats.java
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.utils;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
import static android.os.Build.VERSION.*;
|
||||
import static android.os.Build.VERSION_CODES.*;
|
||||
import static android.text.format.DateFormat.*;
|
||||
|
||||
public class DateFormats
|
||||
{
|
||||
@NonNull
|
||||
private static SimpleDateFormat fromSkeleton(@NonNull String skeleton,
|
||||
@NonNull Locale locale)
|
||||
{
|
||||
SimpleDateFormat df = new SimpleDateFormat(skeleton, locale);
|
||||
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
return df;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static SimpleDateFormat fromSkeleton(@NonNull String skeleton)
|
||||
{
|
||||
Locale locale = Locale.getDefault();
|
||||
|
||||
if (SDK_INT >= JELLY_BEAN)
|
||||
skeleton = getBestDateTimePattern(locale, skeleton);
|
||||
|
||||
return fromSkeleton(skeleton, locale);
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getBackupDateFormat()
|
||||
{
|
||||
return fromSkeleton("yyyy-MM-dd HHmmss", Locale.US);
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getCSVDateFormat()
|
||||
{
|
||||
return fromSkeleton("yyyy-MM-dd", Locale.US);
|
||||
}
|
||||
}
|
||||
@@ -19,21 +19,17 @@
|
||||
|
||||
package org.isoron.uhabits.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.format.DateFormat;
|
||||
import android.content.*;
|
||||
import android.text.format.*;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Calendar.*;
|
||||
|
||||
public abstract class DateUtils
|
||||
{
|
||||
public static int ALL_WEEK_DAYS = 127;
|
||||
|
||||
private static Long fixedLocalTime = null;
|
||||
|
||||
@@ -44,11 +40,9 @@ public abstract class DateUtils
|
||||
|
||||
public static String formatHeaderDate(GregorianCalendar day)
|
||||
{
|
||||
String dayOfMonth =
|
||||
Integer.toString(day.get(GregorianCalendar.DAY_OF_MONTH));
|
||||
String dayOfWeek = day.getDisplayName(GregorianCalendar.DAY_OF_WEEK,
|
||||
GregorianCalendar.SHORT, Locale.getDefault());
|
||||
|
||||
Locale locale = Locale.getDefault();
|
||||
String dayOfMonth = Integer.toString(day.get(DAY_OF_MONTH));
|
||||
String dayOfWeek = day.getDisplayName(DAY_OF_WEEK, SHORT, locale);
|
||||
return dayOfWeek + "\n" + dayOfMonth;
|
||||
}
|
||||
|
||||
@@ -94,24 +88,6 @@ public abstract class DateUtils
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getBackupDateFormat()
|
||||
{
|
||||
SimpleDateFormat dateFormat =
|
||||
new SimpleDateFormat("yyyy-MM-dd HHmmss", Locale.US);
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
return dateFormat;
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getCSVDateFormat()
|
||||
{
|
||||
SimpleDateFormat dateFormat =
|
||||
new SimpleDateFormat("yyyy-MM-dd", Locale.US);
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
return dateFormat;
|
||||
}
|
||||
|
||||
public static GregorianCalendar getCalendar(long timestamp)
|
||||
{
|
||||
GregorianCalendar day =
|
||||
@@ -120,34 +96,18 @@ public abstract class DateUtils
|
||||
return day;
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getDateFormat(String skeleton)
|
||||
{
|
||||
String pattern;
|
||||
Locale locale = Locale.getDefault();
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT >=
|
||||
android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
|
||||
pattern = DateFormat.getBestDateTimePattern(locale, skeleton);
|
||||
else pattern = skeleton;
|
||||
|
||||
SimpleDateFormat format = new SimpleDateFormat(pattern, locale);
|
||||
format.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
return format;
|
||||
}
|
||||
|
||||
public static String[] getDayNames(int format)
|
||||
{
|
||||
String[] wdays = new String[7];
|
||||
|
||||
Calendar day = new GregorianCalendar();
|
||||
day.set(GregorianCalendar.DAY_OF_WEEK, Calendar.SATURDAY);
|
||||
day.set(DAY_OF_WEEK, Calendar.SATURDAY);
|
||||
|
||||
for (int i = 0; i < wdays.length; i++)
|
||||
{
|
||||
wdays[i] = day.getDisplayName(GregorianCalendar.DAY_OF_WEEK, format,
|
||||
Locale.getDefault());
|
||||
day.add(GregorianCalendar.DAY_OF_MONTH, 1);
|
||||
wdays[i] =
|
||||
day.getDisplayName(DAY_OF_WEEK, format, Locale.getDefault());
|
||||
day.add(DAY_OF_MONTH, 1);
|
||||
}
|
||||
|
||||
return wdays;
|
||||
@@ -171,14 +131,12 @@ public abstract class DateUtils
|
||||
String[] days = new String[7];
|
||||
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.set(GregorianCalendar.DAY_OF_WEEK,
|
||||
calendar.getFirstDayOfWeek());
|
||||
calendar.set(DAY_OF_WEEK, calendar.getFirstDayOfWeek());
|
||||
for (int i = 0; i < days.length; i++)
|
||||
{
|
||||
days[i] =
|
||||
calendar.getDisplayName(GregorianCalendar.DAY_OF_WEEK, format,
|
||||
Locale.getDefault());
|
||||
calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
|
||||
days[i] = calendar.getDisplayName(DAY_OF_WEEK, format,
|
||||
Locale.getDefault());
|
||||
calendar.add(DAY_OF_MONTH, 1);
|
||||
}
|
||||
|
||||
return days;
|
||||
@@ -192,12 +150,11 @@ public abstract class DateUtils
|
||||
{
|
||||
Integer[] dayNumbers = new Integer[7];
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.set(GregorianCalendar.DAY_OF_WEEK,
|
||||
calendar.getFirstDayOfWeek());
|
||||
calendar.set(DAY_OF_WEEK, calendar.getFirstDayOfWeek());
|
||||
for (int i = 0; i < dayNumbers.length; i++)
|
||||
{
|
||||
dayNumbers[i] = calendar.get(GregorianCalendar.DAY_OF_WEEK);
|
||||
calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
|
||||
dayNumbers[i] = calendar.get(DAY_OF_WEEK);
|
||||
calendar.add(DAY_OF_MONTH, 1);
|
||||
}
|
||||
return dayNumbers;
|
||||
}
|
||||
@@ -209,7 +166,7 @@ public abstract class DateUtils
|
||||
|
||||
public static String[] getShortDayNames()
|
||||
{
|
||||
return getDayNames(GregorianCalendar.SHORT);
|
||||
return getDayNames(SHORT);
|
||||
}
|
||||
|
||||
public static long getStartOfDay(long timestamp)
|
||||
@@ -230,7 +187,7 @@ public abstract class DateUtils
|
||||
public static int getWeekday(long timestamp)
|
||||
{
|
||||
GregorianCalendar day = getCalendar(timestamp);
|
||||
return day.get(GregorianCalendar.DAY_OF_WEEK) % 7;
|
||||
return day.get(DAY_OF_WEEK) % 7;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,20 +203,6 @@ public abstract class DateUtils
|
||||
return number % 7;
|
||||
}
|
||||
|
||||
public static Integer packWeekdayList(boolean weekday[])
|
||||
{
|
||||
int list = 0;
|
||||
int current = 1;
|
||||
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if (weekday[i]) list |= current;
|
||||
current = current << 1;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void setFixedLocalTime(Long timestamp)
|
||||
{
|
||||
fixedLocalTime = timestamp;
|
||||
@@ -279,12 +222,12 @@ public abstract class DateUtils
|
||||
switch (field)
|
||||
{
|
||||
case MONTH:
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.set(DAY_OF_MONTH, 1);
|
||||
return cal.getTimeInMillis();
|
||||
|
||||
case WEEK_NUMBER:
|
||||
int firstWeekday = cal.getFirstDayOfWeek();
|
||||
int weekday = cal.get(Calendar.DAY_OF_WEEK);
|
||||
int weekday = cal.get(DAY_OF_WEEK);
|
||||
int delta = weekday - firstWeekday;
|
||||
if (delta < 0) delta += 7;
|
||||
cal.add(Calendar.DAY_OF_YEAR, -delta);
|
||||
@@ -292,13 +235,13 @@ public abstract class DateUtils
|
||||
|
||||
case QUARTER:
|
||||
int quarter = cal.get(Calendar.MONTH) / 3;
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.set(DAY_OF_MONTH, 1);
|
||||
cal.set(Calendar.MONTH, quarter * 3);
|
||||
return cal.getTimeInMillis();
|
||||
|
||||
case YEAR:
|
||||
cal.set(Calendar.MONTH, Calendar.JANUARY);
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.set(DAY_OF_MONTH, 1);
|
||||
return cal.getTimeInMillis();
|
||||
|
||||
default:
|
||||
@@ -306,20 +249,6 @@ public abstract class DateUtils
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean[] unpackWeekdayList(int list)
|
||||
{
|
||||
boolean[] weekday = new boolean[7];
|
||||
int current = 1;
|
||||
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if ((list & current) != 0) weekday[i] = true;
|
||||
current = current << 1;
|
||||
}
|
||||
|
||||
return weekday;
|
||||
}
|
||||
|
||||
public enum TruncateField
|
||||
{
|
||||
MONTH, WEEK_NUMBER, YEAR, QUARTER
|
||||
|
||||
Reference in New Issue
Block a user