2016-05-19 14:20:52 +02:00
|
|
|
package net.foucry.pilldroid;
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.ParsePosition;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2016-11-08 12:10:27 +01:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
/**
|
|
|
|
* Created by jacques on 05/05/16.
|
|
|
|
*/
|
2022-03-11 21:45:43 +01:00
|
|
|
public class UtilDate {
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2016-08-10 21:03:28 +02:00
|
|
|
private static final String TAG = UtilDate.class.getName();
|
2020-09-11 11:34:05 +02:00
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
/**
|
2020-12-07 11:34:29 +01:00
|
|
|
* @param aDate any date
|
2020-09-19 18:05:00 +02:00
|
|
|
* @return date the same date as input but at noon (12:00:00 PM)
|
2016-05-19 07:37:28 +02:00
|
|
|
* set date time at Noon
|
|
|
|
*/
|
2022-03-11 21:45:43 +01:00
|
|
|
public static Date dateAtNoon(Date aDate) {
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2020-09-19 18:05:00 +02:00
|
|
|
Log.d(TAG, "dateAtNoon " + aDate);
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.setTime(aDate);
|
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 12);
|
|
|
|
calendar.set(Calendar.MINUTE, 0);
|
|
|
|
calendar.set(Calendar.SECOND, 0);
|
2020-12-07 11:34:29 +01:00
|
|
|
calendar.set(Calendar.MILLISECOND, 0);
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2021-04-01 21:29:08 +02:00
|
|
|
Log.d(TAG, "dateAtNoon " + calendar.getTime());
|
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
return calendar.getTime();
|
|
|
|
}
|
2020-09-11 11:34:05 +02:00
|
|
|
|
2016-05-19 07:37:28 +02:00
|
|
|
/**
|
2016-11-08 12:10:27 +01:00
|
|
|
* @param days number of days to remove to the ate
|
|
|
|
* @param date date before day removing
|
2016-05-19 07:37:28 +02:00
|
|
|
* @return date
|
|
|
|
* Substract days to date and return a new date
|
|
|
|
*/
|
2016-11-08 12:10:27 +01:00
|
|
|
static Date removeDaysToDate(int days, Date date) {
|
2016-05-19 07:37:28 +02:00
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.setTime(date);
|
|
|
|
calendar.add(Calendar.DAY_OF_YEAR, -days);
|
|
|
|
|
|
|
|
return calendar.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-08 12:10:27 +01:00
|
|
|
* @param date Date to be converted
|
|
|
|
* @return String of the converted date
|
2016-05-19 07:37:28 +02:00
|
|
|
* Convert a date to a String using a SimpleDateFormat
|
|
|
|
*/
|
2016-11-08 12:10:27 +01:00
|
|
|
static String date2String(Date date, DateFormat dateFormat) {
|
2016-05-19 07:37:28 +02:00
|
|
|
|
2016-08-10 21:03:28 +02:00
|
|
|
Log.d(TAG, "date == " + date);
|
2016-05-19 07:37:28 +02:00
|
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.setTime(date);
|
|
|
|
|
|
|
|
return dateFormat.format(calendar.getTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-11 11:34:05 +02:00
|
|
|
* @param dateString string representing a Date to be converted
|
|
|
|
* @return date Date after conversion
|
2016-05-19 07:37:28 +02:00
|
|
|
* Convert String date into Date
|
|
|
|
*/
|
2016-11-08 12:10:27 +01:00
|
|
|
static Date string2Date(String dateString) {
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE);
|
2016-05-19 07:37:28 +02:00
|
|
|
ParsePosition pos = new ParsePosition(0);
|
2020-09-11 11:34:05 +02:00
|
|
|
return dateFormat.parse(dateString, pos);
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-08 12:10:27 +01:00
|
|
|
* @param date start date
|
|
|
|
* @return int numbers of days between date and today
|
2016-05-19 07:37:28 +02:00
|
|
|
* Number of days between date (older than today) and today
|
|
|
|
*/
|
2020-12-23 09:39:21 +01:00
|
|
|
static int nbOfDaysBetweenDateAndToday(Date date) {
|
2016-05-19 07:37:28 +02:00
|
|
|
Date oldDate = dateAtNoon(date); // Be sure that the old date is at Noon
|
|
|
|
Date todayDate = dateAtNoon(new Date()); // Be sure that we use today at Noon
|
|
|
|
|
2020-12-23 09:39:21 +01:00
|
|
|
return (int) (todayDate.getTime() - oldDate.getTime())/(86400*1000);
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|
2016-08-30 17:58:37 +02:00
|
|
|
|
2020-09-19 18:05:00 +02:00
|
|
|
/**
|
|
|
|
* Convert dateInMilliseconds into string formated date
|
|
|
|
* @param dateInMilliseconds long
|
|
|
|
* @return formatted Date String
|
|
|
|
*/
|
2016-11-08 12:10:27 +01:00
|
|
|
static String convertDate(long dateInMilliseconds) {
|
2021-12-28 15:10:36 +01:00
|
|
|
DateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss", Locale.getDefault());
|
2016-08-30 17:58:37 +02:00
|
|
|
Calendar calendar = Calendar.getInstance();
|
2016-08-11 17:41:14 +02:00
|
|
|
calendar.setTimeInMillis(dateInMilliseconds);
|
|
|
|
return formatter.format(calendar.getTime());
|
2016-08-30 17:58:37 +02:00
|
|
|
}
|
2016-05-19 07:37:28 +02:00
|
|
|
}
|