package co.yiiu.core.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by tomoya.
 * Copyright (c) 2016, All Rights Reserved.
 * https://yiiu.co
 */
public class DateUtil {

  public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
  public static final String FORMAT_DATE = "yyyy-MM-dd";

  public static String formatDateTime(Date date) {
    if (date == null) return null;
    SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATETIME);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    return sdf.format(date);
  }

  public static String formatDate(Date date) {
    if (date == null) return null;
    SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    return sdf.format(date);
  }

  public static String formatDateTime(Date date, String style) {
    if (date == null) return null;
    SimpleDateFormat sdf = new SimpleDateFormat(style);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    return sdf.format(date);
  }

  /**
   * 字符串转时间
   *
   * @param dateString
   * @param style
   * @return
   */
  public static Date string2Date(String dateString, String style) {
    if (StringUtils.isEmpty(dateString)) return null;
    Date date = new Date();
    SimpleDateFormat strToDate = new SimpleDateFormat(style);
    try {
      date = strToDate.parse(dateString);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  /**
   * 判断传入的时间是否在当前时间之后，返回boolean值
   * true: 过期
   * false: 还没过期
   *
   * @param date
   * @return
   */
  public static boolean isExpire(Date date) {
    if (date.before(new Date())) return true;
    return false;
  }

  public static Date getHourAfter(Date date, int hour) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, hour + 1);
    return calendar.getTime();
  }

  public static Date getHourBefore(Date date, int hour) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, -(hour - 1));
    return calendar.getTime();
  }

  public static Date getDateBefore(Date date, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, -day);
    return calendar.getTime();
  }

  public static Date getDateAfter(Date date, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, day);
    return calendar.getTime();
  }

  public static Date getMinuteAfter(Date date, int minute) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MINUTE, minute);
    return calendar.getTime();
  }

  public static Date getMinuteBefore(Date date, int minute) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MINUTE, -minute);
    return calendar.getTime();
  }

}
@@ -1826,24 +1826,42 @@
 false;%0A  %7D%0A%0A
+  // %E8%8E%B7%E5%8F%96 hour %E5%90%8E%E7%9A%84%E6%97%B6%E9%97%B4%0A
   public sta
@@ -1897,32 +1897,32 @@
 te, int hour) %7B%0A
-
     Calendar cal
@@ -2015,18 +2015,58 @@
 HOUR
-, hour + 1
+_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour
 );%0A 
@@ -2092,32 +2092,50 @@
 getTime();%0A  %7D%0A%0A
+  // %E8%8E%B7%E5%8F%96 hour %E5%89%8D%E7%9A%84%E6%97%B6%E9%97%B4%0A
   public static 
@@ -2286,21 +2286,58 @@
 HOUR
-, -(hour - 1)
+_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour
 );%0A 

修改获取时间之前，之后bug