package com.fshark.overseas.advert.util;


import org.gradle.internal.impldep.org.joda.time.DateTime;
import org.gradle.internal.impldep.org.joda.time.Days;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zhouml
 * @date 2018-11-29
 */
public class DateUtils {

    static DateTimeFormatter YMDHMS_FORMATTER = DateTimeFormatter.ofPattern(Constants.YMDHMS);
    static DateTimeFormatter YMDHMS_MILLIS_FORMATTER = DateTimeFormatter.ofPattern(Constants.YMDHMS_MILLIS);
    static DateTimeFormatter HH_FORMATTER = DateTimeFormatter.ofPattern(Constants.HH);
    static DateTimeFormatter YMD_FORMATTER = DateTimeFormatter.ofPattern(Constants.YMD);



    public static String formatAsYMDHMS(LocalDateTime ts) {
        return YMDHMS_FORMATTER.format(ts);
    }


    /**
     * 将 yyyy-MM-dd HH:mm:ss数据转换为yyyyMMdd
     *
     * @param ts
     * @return
     */
    public static String formatAsYMD(String ts) {
        LocalDateTime parse = LocalDateTime.parse(ts, YMDHMS_FORMATTER);
        return parse.format(YMD_FORMATTER);
    }

    public static String formatAsYMD(LocalDateTime ts) {
        return ts.format(YMD_FORMATTER);
    }

    public static String formatAsHH(LocalDateTime ts) {
        return ts.format(HH_FORMATTER);
    }

    public static String formatAsHH(String ts) {
        LocalDateTime parse = LocalDateTime.parse(ts, YMDHMS_FORMATTER);
        return parse.format(HH_FORMATTER);
    }

    public static LocalDateTime formatAsDate(String ts) {
        return LocalDateTime.parse(ts, YMDHMS_FORMATTER);
    }

    public static LocalDateTime formatHMSSAsDate(String ts) {
        return LocalDateTime.parse(ts, YMDHMS_MILLIS_FORMATTER);
    }


    /**
     * @return 相差天数
     */

    public static long daysBetween(LocalDateTime smdate, LocalDateTime bdate) {
        Duration between = Duration.between(smdate.toLocalDate().atStartOfDay(), bdate.toLocalDate().atStartOfDay());
        return between.toDays();
    }




}

