package com.tzld.piaoquan.ad.engine.commons.util;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author dyp
 */
public final class DateUtils {
    public static String getCurrentDateStr(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date currentDate = new Date();
        return dateFormat.format(currentDate);
    }

    public static int getCurrentHour() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static String getBeforeDaysDateStr(String format, int d) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -d);
        Date previousDate = calendar.getTime();
        return dateFormat.format(previousDate);
    }

    public static int getHourByTimestamp(long timestamp) {
        return LocalDateTime
                .ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault())
                .getHour();
    }

    public static int getDayOrWeekByTimestamp(long timestamp) {
        return LocalDateTime
                .ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault())
                .getDayOfWeek()
                .getValue();
    }

    public static int getHourQuarter(long timestamp) {
        Instant instant = Instant.ofEpochSecond(timestamp);
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        int hour = zonedDateTime.getHour();
        int minute = zonedDateTime.getMinute();
        return minute / 15 + hour * 4;
    }

    public static String getThatDayDateString() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate today = LocalDate.now();
        return dateFormat.format(today);
    }
}
