package com.tzld.longarticle.recommend.server.util;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
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 boolean ifTimeRangeInNow(String timeRange) {
        try {

            String[] split = timeRange.split("-");
            if (split.length != 2) {
                return false;
            }
            String startTime = split[0];
            String endTime = split[1];
            if (startTime.length() != 10 || endTime.length() != 10) {
                return false;
            }
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyyMMddHH"));
            LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyyMMddHH"));
            return now.equals(start) || (now.isAfter(start) && now.isBefore(end));
        } catch (Exception e) {
//            e.printStackTrace();
            return false;
        }
    }


    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        System.out.println((calendar.get(Calendar.DAY_OF_WEEK) + 6) % 7);
        System.out.println(new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()));
        System.out.println(new SimpleDateFormat("HH").format(calendar.getTime()));

    }
}
