Pilldroid/app/src/main/java/net/foucry/pilldroid/UtilDate.java

102 lines
3 KiB
Java
Raw Normal View History

package net.foucry.pilldroid;
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;
/**
* Created by jacques on 05/05/16.
*/
2016-11-08 12:10:27 +01:00
class UtilDate {
private static final String TAG = UtilDate.class.getName();
2020-09-11 11:34:05 +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)
* set date time at Noon
*/
2016-11-08 12:10:27 +01:00
static Date dateAtNoon(Date aDate) {
2020-09-19 18:05:00 +02:00
Log.d(TAG, "dateAtNoon " + aDate);
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);
return calendar.getTime();
}
2020-09-11 11:34:05 +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
* @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) {
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
* Convert a date to a String using a SimpleDateFormat
*/
2016-11-08 12:10:27 +01:00
static String date2String(Date date, DateFormat dateFormat) {
Log.d(TAG, "date == " + date);
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
* 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);
ParsePosition pos = new ParsePosition(0);
2020-09-11 11:34:05 +02:00
return dateFormat.parse(dateString, pos);
}
/**
2016-11-08 12:10:27 +01:00
* @param date start date
* @return int numbers of days between date and today
* Number of days between date (older than today) and today
*/
2020-12-07 11:34:29 +01:00
static long nbOfDaysBetweenDateAndToday(Date date) {
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-07 11:34:29 +01:00
return (todayDate.getTime() - oldDate.getTime());
}
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) {
DateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss", Locale.FRANCE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(dateInMilliseconds);
return formatter.format(calendar.getTime());
}
}