package com.jwipc.shopping.util;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;
import java.util.Locale;

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/3/14 0014 11:01
 */
public class DateUtil {

    public static final String YYYYMMDD_DASH_PATTERN = "yyyy-MM-dd";
    public static final String YEAR_PATTERN = "yyyy";
    public static final String YYYYMMDD_PATTERN = "yyyyMMdd";
    public static final String YYYYMMDD_TIME_PATTERN = "yyyyMMdd HH:mm:ss";
    public static final String YYYYMMDD_TIMES_PATTERN = "yyyyMMdd HH:mm:ss.SSS";
    public static final Locale DEFAULT_LOCALE;
    public static final DateTimeZone DEFAULT_TIMEZONE;

    public DateUtil() {
    }

    public static DateTimeFormatter getDateTimeFormatter(String pattern) {
        return DateTimeFormat.forPattern(pattern).withZone(DEFAULT_TIMEZONE).withLocale(DEFAULT_LOCALE);
    }

    public static String year(Date date) {
        return getDateTimeFormatter("yyyy").print(new DateTime(date));
    }

    public static String yyyyMMdd(Date date) {
        return getDateTimeFormatter("yyyyMMdd").print(new DateTime(date));
    }

    public static String yyyyMMddDash(Date date) {
        return getDateTimeFormatter("yyyy-MM-dd").print(new DateTime(date));
    }

    public static String yyyyMMddTime(Date date) {
        return getDateTimeFormatter("yyyyMMdd HH:mm:ss").print(new DateTime(date));
    }

    public static String yyyyMMddTimes(Date date) {
        return getDateTimeFormatter("yyyyMMdd HH:mm:ss.SSS").print(new DateTime(date));
    }

    public static DateTime year(String dateFormat) {
        return getDateTimeFormatter("yyyy").parseDateTime(dateFormat);
    }

    public static DateTime yyyyMMdd(String dateFormat) {
        return getDateTimeFormatter("yyyyMMdd").parseDateTime(dateFormat);
    }

    public static DateTime yyyyMMddDash(String dateFormat) {
        return getDateTimeFormatter("yyyy-MM-dd").parseDateTime(dateFormat);
    }

    public static DateTime yyyyMMddTime(String dateFormat) {
        return getDateTimeFormatter("yyyyMMdd HH:mm:ss").parseDateTime(dateFormat);
    }

    public static DateTime yyyyMMddTimes(String dateFormat) {
        return getDateTimeFormatter("yyyyMMdd HH:mm:ss.SSS").parseDateTime(dateFormat);
    }

    public static int unixTime() {
        return (int)(System.currentTimeMillis() / 1000L);
    }

    public static Date dateTime(int unix) {
        return new Date((long)unix * 1000L);
    }

    public static int unixTime(Date date) {
        return date == null ? 0 : (int)(date.getTime() / 1000L);
    }

    public static long unixTimeLong() {
        return System.currentTimeMillis() / 1000L;
    }

    static {
        DEFAULT_LOCALE = Locale.SIMPLIFIED_CHINESE;
        DEFAULT_TIMEZONE = DateTimeZone.forOffsetHours(8);
    }

}
