2019-02-13 20:43:30 +01:00
|
|
|
/* Copyright (C) 2015-2019 Andreas Shimokawa, AndrewH, Carsten Pfeiffer,
|
2018-08-29 21:30:23 +02:00
|
|
|
Daniele Gobbetti, Pavel Elagin
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-08-17 02:22:16 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
|
|
|
import android.text.format.DateUtils;
|
|
|
|
|
|
|
|
import com.github.pfichtner.durationformatter.DurationFormatter;
|
|
|
|
|
2018-04-24 21:00:22 +02:00
|
|
|
import java.text.FieldPosition;
|
2016-05-13 23:47:47 +02:00
|
|
|
import java.text.ParseException;
|
2018-04-24 21:00:22 +02:00
|
|
|
import java.text.ParsePosition;
|
2016-05-13 23:47:47 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
2015-08-17 02:22:16 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
2016-05-13 23:47:47 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.TimeZone;
|
2015-08-17 02:22:16 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
|
|
|
|
public class DateTimeUtils {
|
2016-05-13 23:47:47 +02:00
|
|
|
private static SimpleDateFormat DAY_STORAGE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
|
2018-08-16 16:59:56 +02:00
|
|
|
private static SimpleDateFormat HOURS_MINUTES_FORMAT = new SimpleDateFormat("HH:mm", Locale.US);
|
2018-04-24 21:00:22 +02:00
|
|
|
public static SimpleDateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US){
|
|
|
|
//see https://github.com/Freeyourgadget/Gadgetbridge/issues/1076#issuecomment-383834116 and https://stackoverflow.com/a/30221245
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Date parse(String text, ParsePosition pos) {
|
|
|
|
if (text.length() > 3) {
|
|
|
|
text = text.substring(0, text.length() - 3) + text.substring(text.length() - 2);
|
|
|
|
}
|
|
|
|
return super.parse(text, pos);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {
|
|
|
|
StringBuffer rfcFormat = super.format(date, toAppendTo, pos);
|
2018-07-22 16:11:32 +02:00
|
|
|
if (this.getTimeZone().equals(TimeZone.getTimeZone("UTC"))) {
|
|
|
|
rfcFormat.setLength(rfcFormat.length()-5);
|
|
|
|
return rfcFormat.append("Z");
|
|
|
|
} else {
|
|
|
|
return rfcFormat.insert(rfcFormat.length() - 2, ":");
|
|
|
|
}
|
2018-04-24 21:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}; //no public access, we have to workaround Android bugs
|
2016-05-13 23:47:47 +02:00
|
|
|
|
2015-09-05 00:14:09 +02:00
|
|
|
public static String formatDateTime(Date date) {
|
2017-10-19 21:52:38 +02:00
|
|
|
return DateUtils.formatDateTime(GBApplication.getContext(), date.getTime(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_YEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatIso8601(Date date) {
|
2018-04-16 18:26:23 +02:00
|
|
|
if(GBApplication.isRunningNougatOrLater()){
|
|
|
|
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.US).format(date);
|
2018-04-12 21:47:40 +02:00
|
|
|
}
|
2018-07-22 16:11:32 +02:00
|
|
|
ISO_8601_FORMAT.setTimeZone(TimeZone.getDefault());
|
|
|
|
return ISO_8601_FORMAT.format(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatIso8601UTC(Date date) {
|
|
|
|
if(GBApplication.isRunningNougatOrLater()){
|
|
|
|
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.US);
|
|
|
|
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
return sdf.format(date);
|
|
|
|
}
|
|
|
|
ISO_8601_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
|
2017-10-19 21:52:38 +02:00
|
|
|
return ISO_8601_FORMAT.format(date);
|
2015-09-05 00:14:09 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 02:22:16 +02:00
|
|
|
public static String formatDate(Date date) {
|
|
|
|
return DateUtils.formatDateTime(GBApplication.getContext(), date.getTime(), DateUtils.FORMAT_SHOW_DATE);
|
|
|
|
// long dateMillis = date.getTime();
|
|
|
|
// if (isToday(dateMillis)) {
|
|
|
|
// return "Today";
|
|
|
|
// }
|
|
|
|
// if (isYesterday(dateMillis)) {
|
|
|
|
// return "Yesterday";
|
|
|
|
// }
|
|
|
|
// DateFormat.getDateInstance(DateFormat.SHORT).format(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatDurationHoursMinutes(long duration, TimeUnit unit) {
|
|
|
|
DurationFormatter df = DurationFormatter.Builder.SYMBOLS
|
|
|
|
.maximum(TimeUnit.DAYS)
|
|
|
|
.minimum(TimeUnit.MINUTES)
|
2017-04-08 23:16:33 +02:00
|
|
|
.suppressZeros(DurationFormatter.SuppressZeros.LEADING, DurationFormatter.SuppressZeros.TRAILING)
|
2015-08-17 02:22:16 +02:00
|
|
|
.maximumAmountOfUnitsToShow(2)
|
|
|
|
.build();
|
|
|
|
return df.format(duration, unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatDateRange(Date from, Date to) {
|
|
|
|
return DateUtils.formatDateRange(GBApplication.getContext(), from.getTime(), to.getTime(), DateUtils.FORMAT_SHOW_DATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Date shiftByDays(Date date, int offset) {
|
|
|
|
Calendar cal = GregorianCalendar.getInstance();
|
|
|
|
cal.setTime(date);
|
|
|
|
cal.add(GregorianCalendar.DAY_OF_YEAR, offset);
|
|
|
|
Date newDate = cal.getTime();
|
|
|
|
return newDate;
|
|
|
|
}
|
2016-02-26 00:30:57 +01:00
|
|
|
|
|
|
|
public static Date parseTimeStamp(int timestamp) {
|
|
|
|
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
|
|
|
|
cal.setTimeInMillis(timestamp * 1000L); // make sure it's converted to long
|
|
|
|
return cal.getTime();
|
|
|
|
}
|
2016-05-13 23:47:47 +02:00
|
|
|
|
|
|
|
public static String dayToString(Date date) {
|
|
|
|
return DAY_STORAGE_FORMAT.format(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Date dayFromString(String day) throws ParseException {
|
|
|
|
return DAY_STORAGE_FORMAT.parse(day);
|
|
|
|
}
|
|
|
|
|
2018-08-16 16:59:56 +02:00
|
|
|
public static String timeToString(Date date) {
|
|
|
|
return HOURS_MINUTES_FORMAT.format(date);
|
|
|
|
}
|
|
|
|
|
2019-01-07 01:10:57 +01:00
|
|
|
public static String formatTime(int hours, int minutes) {
|
|
|
|
return String.format(Locale.US, "%02d", hours) + ":" + String.format(Locale.US, "%02d", minutes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-13 23:47:47 +02:00
|
|
|
public static Date todayUTC() {
|
|
|
|
Calendar cal = getCalendarUTC();
|
|
|
|
return cal.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Calendar getCalendarUTC() {
|
|
|
|
return GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
|
|
|
|
}
|
2017-03-03 14:21:59 +01:00
|
|
|
|
|
|
|
public static String minutesToHHMM(int minutes) {
|
|
|
|
return String.format(Locale.US, "%d:%02d", minutes / 60, minutes % 60); // no I do not want to use durationformatter :P
|
|
|
|
}
|
2015-08-17 02:22:16 +02:00
|
|
|
}
|