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;
|
|
|
|
/**
|
|
|
|
* Created by jacques on 05/05/16.
|
|
|
|
*/
|
|
|
|
public class UtilDate {
|
|
|
|
|
2016-08-10 21:03:28 +02:00
|
|
|
private static final String TAG = UtilDate.class.getName();
|
2016-05-19 07:37:28 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param aDate
|
|
|
|
* @return date
|
|
|
|
*
|
|
|
|
* set date time at Noon
|
|
|
|
*/
|
|
|
|
public static Date dateAtNoon(Date aDate) {
|
|
|
|
|
2016-08-10 21:03:28 +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);
|
|
|
|
|
|
|
|
return calendar.getTime();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param days
|
|
|
|
* @param date
|
|
|
|
* @return date
|
|
|
|
*
|
|
|
|
* Substract days to date and return a new date
|
|
|
|
*/
|
|
|
|
public static Date removeDaysToDate(int days, Date date) {
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.setTime(date);
|
|
|
|
calendar.add(Calendar.DAY_OF_YEAR, -days);
|
|
|
|
|
|
|
|
return calendar.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param date
|
|
|
|
* @return String
|
|
|
|
*
|
|
|
|
* Convert a date to a String using a SimpleDateFormat
|
|
|
|
*/
|
|
|
|
public static String date2String(Date date, DateFormat dateFormat) {
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param dateString
|
|
|
|
* @return date
|
|
|
|
*
|
|
|
|
* Convert String date into Date
|
|
|
|
*/
|
|
|
|
public static Date string2Date(String dateString) {
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
ParsePosition pos = new ParsePosition(0);
|
|
|
|
return dateFormat.parse(dateString,pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param date
|
|
|
|
* @return int
|
|
|
|
*
|
|
|
|
* Number of days between date (older than today) and today
|
|
|
|
*/
|
|
|
|
public 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-08-30 17:58:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param: none
|
|
|
|
* return int
|
|
|
|
*/
|
|
|
|
|
2016-08-31 17:57:12 +02:00
|
|
|
public static long tomorrowAtNoonInMillis() {
|
2016-08-30 17:58:37 +02:00
|
|
|
Date now = new Date();
|
2016-08-30 23:54:23 +02:00
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.setTime(now);
|
|
|
|
calendar.add(Calendar.DAY_OF_YEAR,1);
|
|
|
|
|
|
|
|
Date tomorrowAtNoon = dateAtNoon(calendar.getTime());
|
|
|
|
|
|
|
|
return (tomorrowAtNoon.getTime() - now.getTime());
|
|
|
|
}
|
2016-08-30 17:58:37 +02:00
|
|
|
|
2016-08-11 17:41:14 +02:00
|
|
|
public static String convertDate(long dateInMilliseconds) {
|
2016-08-14 11:11:39 +02:00
|
|
|
DateFormat formatter = new SimpleDateFormat("dd/MM/yy hh:mm:ss");
|
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
|
|
|
}
|