package com.sheep.gamegroup.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Created by ljy on 2018/3/8.
 */
public class TimeUtil {

    public static String FORMAT = "yyyy/MM/dd HH:mm";
    public static String FORMAT_DD = "yyyy/MM/dd";
    public static String FORMAT_DD_LINE = "yyyy-MM-dd";

    private static TimeUtil timeUtil;

    public static TimeUtil newInstance(){
        if (timeUtil == null)
            timeUtil = new TimeUtil();
        return timeUtil;
    }

    public static long getCurrentSeconds(){
        long ls = System.currentTimeMillis()/1000;
        return ls;
    }
    public static String TimeStamp2Date(long time, String formats) {
        time = time < 10_000_000_000L ? time * 1000 : time;//秒的时间戳为10位数,要x1000  10_000_000_000L对应时间为2286-11-21 01:46:40
        String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(time));
        return date;
    }
    public static String[] getCalendarShowTime(long paramLong)
    {
        String[] localObject;
        String str = new SimpleDateFormat("yyyy:MM:dd", Locale.CHINA).format(new Date(paramLong));
        try
        {
            String[] arrayOfString = str.split(":");
            localObject = arrayOfString;
            if ((localObject != null) && (localObject.length == 3));
            return localObject;
        }
        catch (Exception localException)
        {
            while (true)
                localException.printStackTrace();
        }
    }

    public static String[] getCalendarShowTime(String paramString)
    {
        try {
            long l = Long.valueOf(paramString);
            Calendar localCalendar = Calendar.getInstance();
            localCalendar.setTimeInMillis(1000L * l);
            return getCalendarShowTime(localCalendar.getTimeInMillis());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String getDate(String format){
        return new SimpleDateFormat(format, Locale.CHINA).format(new Date());
    }
    public static String getDate(String formate, long paramLong){
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(paramLong*1000);
        Date date = c.getTime();
        String str = new SimpleDateFormat(formate, Locale.CHINA).format(date);
        return str;
    }

    /**
     * 毫秒值
     * @param time
     * @return 时:分:秒
     */
    public static String getHours(long time) {
        long second = time / 1000;
        long hour = second / 60 / 60;
        long minute = (second - hour * 60 * 60) / 60;
        long sec = (second - hour * 60 * 60) - minute * 60;

        String rHour;
        String rMin;
        String rSs;
        // 时
        if (hour < 10) {
            rHour = "0" + hour;
        } else {
            rHour = hour + "";
        }
        // 分
        if (minute < 10) {
            rMin = "0" + minute;
        } else {
            rMin = minute + "";
        }
        // 秒
        if (sec < 10) {
            rSs = "0" + sec;
        } else {
            rSs = sec + "";
        }

        // return hour + "小时" + minute + "分钟" + sec + "秒";
        return rHour + ":" + rMin + ":" + rSs;

    }

    /**
     * 日期时间字符串转换成毫秒
     * @return
     */
    public static long string2Long(String dateStr, String dateFormat){
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat(dateFormat).parse(dateStr));
            return calendar.getTimeInMillis();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }

    /**
     * 是否大于今天
     */
    public boolean big2Today(String dateStr, String dateFormat){
        if(getDayBegin() == string2Long(dateStr, dateFormat)){
            return true;
        }
        return false;
    }
    //获取当天的开始时间
      public long getDayBegin() {
                Calendar cal = new GregorianCalendar();
                 cal.set(Calendar.HOUR_OF_DAY, 0);
                 cal.set(Calendar.MINUTE, 0);
                 cal.set(Calendar.SECOND, 0);
                 cal.set(Calendar.MILLISECOND, 0);
                 return (cal.getTime()).getTime();
             }

}
