/*
* The MIT License
*
* Copyright 2015 Apothesource, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.apothesource.pillfill.utilites;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by Michael Ramirez on 5/29/15. Copyright 2015, Apothesource, Inc. All Rights Reserved.
*/
public class DateAdapter {
public static Date parseDate(String s) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static String printDate(Date dt) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(dt);
}
public static Date addYearsToDate(Date baseDate, int years) {
Calendar c = Calendar.getInstance();
c.setTime(baseDate);
c.add(Calendar.YEAR, years);
return c.getTime();
}
public static Date addMonthsToDate(Date baseDate, int months) {
Calendar c = Calendar.getInstance();
c.setTime(baseDate);
c.add(Calendar.MONTH, months);
return c.getTime();
}
public static int getDaysBetween(Date beginDate, Date endDate) {
long day1 = beginDate.getTime(); // in milliseconds.
long day2 = endDate.getTime(); // in milliseconds.
long days = (day2 - day1) / 86400000;
return (int) Math.floor(days);
}
public static Date addDaysToDate(Date date, int days) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DAY_OF_MONTH, days);
return c.getTime();
}
}