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

101 lines
2.9 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
/**
2016-11-08 12:10:27 +01:00
* @param aDate anydate
* @return date the same date as input but at noon (12:00:00)
2020-09-11 11:34:05 +02:00
* <p>
* set date time at Noon
*/
2016-11-08 12:10:27 +01:00
static Date dateAtNoon(Date aDate) {
2016-11-01 23:27:50 +01: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);
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
2020-09-11 11:34:05 +02:00
* <p>
* 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
2020-09-11 11:34:05 +02:00
* <p>
* 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
* <p>
* 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
2020-09-11 11:34:05 +02:00
* <p>
* Number of days between date (older than today) and today
*/
2016-11-08 12:10:27 +01:00
static int 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
return (int) (todayDate.getTime() - oldDate.getTime());
}
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());
}
}