package hf.lskj.com.carstockcount.utils;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by yaoju on 2017/4/11.
 */

public class DateUtils {

    public static String format(Date date) {
        return format(date.getTime());
    }

    public static String format(Date date, String formatter) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        return sdf.format(date);
    }

    public static String format(String timeInMills) {
        if (TextUtils.isEmpty(timeInMills)) {
            return "";
        }
        return format(Long.valueOf(timeInMills));
    }
    public static String format(Calendar calendar){
        return format(calendar.getTimeInMillis());
    }
    public static String format(long timeInMills) {
        Date date = new Date(timeInMills);
        return format(date, "yyyy-M-d H:m");
    }

    public static String formatDate(Date date) {
        if (date == null) {
            return "";
        }
        String formatter = "yyyy-M-d";
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        return sdf.format(date);
    }

    public static String addDays(String dateStr, int days) {
        return addDays(dateStr, null, days);
    }

    public static String addDays(String dateStr, String formatter, int days) {
        if (TextUtils.isEmpty(dateStr)) {
            return "";
        }
        if (TextUtils.isEmpty(formatter)) {
            formatter = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        try {
            Date date = sdf.parse(dateStr);
            return addDays(date, formatter, days);
        } catch (Exception ex) {
            return "";
        }

    }

    public static String addDays(Date date, String formatter, int days) {
        if (TextUtils.isEmpty(formatter)) {
            return addDays(date, days);
        } else {
            return format(addDaysToDate(date, days), formatter);
        }
    }

    public static String addDays(Date date, int days) {
        return formatDate(addDaysToDate(date, days));
    }

    public static Date addDaysToDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }
}
