content: /*
   * {EasyGank}  Copyright (C) {2015}  {CaMnter}
   *
   * This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
   * This is free software, and you are welcome to redistribute it
   * under certain conditions; type `show c' for details.
   *
   * The hypothetical commands `show w' and `show c' should show the appropriate
   * parts of the General Public License.  Of course, your program's commands
   * might be different; for a GUI interface, you would use an "about box".
   *
   * You should also get your employer (if you work as a programmer) or school,
   * if any, to sign a "copyright disclaimer" for the program, if necessary.
   * For more information on this, and how to apply and follow the GNU GPL, see
   * <http://www.gnu.org/licenses/>.
   *
   * The GNU General Public License does not permit incorporating your program
   * into proprietary programs.  If your program is a subroutine library, you
   * may consider it more useful to permit linking proprietary applications with
   * the library.  If this is what you want to do, use the GNU Lesser General
   * Public License instead of this License.  But first, please read
   * <http://www.gnu.org/philosophy/why-not-lgpl.html>.
   */
  
  package com.common.utils;
  
  import android.annotation.SuppressLint;
  
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.Locale;
  
  
  @SuppressLint("SimpleDateFormat")
  /**
   * Description：DateUtils
   * Created by：zjl
   * Time：2015-09-24 15:23
   */
  public class DateUtils {
  
      public static String[] WEEK = new String[]{"天", "一", "二", "三", "四", "五", "六"};
  
      public static final long ONE_SECOND = 1000;
      public static final long ONE_MINUTE = ONE_SECOND * 60;
      public static final long ONE_HOUR = ONE_MINUTE * 60;
      public static final long ONE_DAY = ONE_HOUR * 24;
  
      /**
       * String 转换 Date
       *
       * @param str    str
       * @param format format
       * @return Date
       */
      public static Date string2Date(String str, String format) {
          try {
              return new SimpleDateFormat(format).parse(str);
          } catch (ParseException e) {
              e.printStackTrace();
          }
          return new Date();
      }
  
      /**
       * String 转 String
       *
       * @param str          str
       * @param format       format
       * @param secondFormat secondFormat
       * @return String
       */
      public static String string2String(String str, String format, String secondFormat) {
          try {
              return date2String(new SimpleDateFormat(format).parse(str).getTime(), secondFormat);
          } catch (ParseException e) {
              e.printStackTrace();
          }
          return new Date().toString();
      }
  
      /**
       * Date（long） 转换 String
       *
       * @param time   time
       * @param format format
       * @return String
       */
      public static String date2String(long time, String format) {
          SimpleDateFormat sdf = new SimpleDateFormat(format);
          return sdf.format(time);
      }
  
      /**
       * long 去除 时分秒
       * 时分秒全部为0
       *
       * @param date date
       * @return long
       */
      public static long getYearMonthDay(long date) {
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(date);
          calendar.set(Calendar.HOUR_OF_DAY, 0);
          calendar.set(Calendar.SECOND, 0);
          calendar.set(Calendar.MINUTE, 0);
          calendar.set(Calendar.MILLISECOND, 0);
          return calendar.getTimeInMillis();
      }
  
      /**
       * 获取目标时间和当前时间之间的差距
       *
       * @param date date
       * @return String
       */
      public static String getTimestampString(Date date) {
          Date curDate = new Date();
          long splitTime = curDate.getTime() - date.getTime();
          if (splitTime < (30 * ONE_DAY)) {
              if (splitTime < ONE_MINUTE) {
                  return "刚刚";
              }
              if (splitTime < ONE_HOUR) {
                  return String.format("%d分钟前", splitTime / ONE_MINUTE);
              }
  
              if (splitTime < ONE_DAY) {
                  return String.format("%d小时前", splitTime / ONE_HOUR);
              }
  
              return String.format("%d天前", splitTime / ONE_DAY);
          }
          String result;
          result = "M月d日 HH:mm";
          return (new SimpleDateFormat(result, Locale.CHINA)).format(date);
      }
  
      /**
       * 24小时制 转换 12小时制
       *
       * @param time time
       * @return String
       */
      public static String time24To12(String time) {
          String str[] = time.split(":");
          int h = Integer.valueOf(str[0]);
          int m = Integer.valueOf(str[1]);
          String sx;
          if (h < 1) {
              h = 12;
              sx = "上午";
          } else if (h < 12) {
              sx = "上午";
          } else if (h < 13) {
              sx = "下午";
          } else {
              sx = "下午";
              h -= 12;
          }
          return String.format("%d:%02d%s", h, m, sx);
      }
  
      /**
       * Date 转换 HH
       *
       * @param date date
       * @return String
       */
      public static String date2HH(Date date) {
          return new SimpleDateFormat("HH").format(date);
      }
  
      /**
       * Date 转换 HH:mm:ss
       *
       * @param date date
       * @return String
       */
      public static String date2HHmm(Date date) {
          return new SimpleDateFormat("HH:mm").format(date);
      }
  
      /**
       * Date 转换 HH:mm:ss
       *
       * @param date date
       * @return String
       */
      public static String date2HHmmss(Date date) {
          return new SimpleDateFormat("HH:mm:ss").format(date);
      }
  
      /**
       * Date 转换 MM.dd
       *
       * @param date date
       * @return String
       */
      public static String date2MMdd(Date date) {
          return new SimpleDateFormat("MM.dd").format(date);
      }
  
      /**
       * Date 转换 yyyy.MM.dd
       *
       * @param date date
       * @return String
       */
      public static String date2yyyyMMdd(Date date) {
          return new SimpleDateFormat("yyyy.MM.dd").format(date);
      }
  
      /**
       * Date 转换 MM月dd日 星期
       *
       * @param date date
       * @return String
       */
      public static String date2MMddWeek(Date date) {
          Calendar calendar = Calendar.getInstance();
          calendar.setTime(date);
          int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
          return new SimpleDateFormat("MM月dd日 星期").format(date) + WEEK[dayOfWeek - 1];
      }
  
      /**
       * Date 转换 yyyy年MM月dd日 星期
       *
       * @param date date
       * @return String
       */
      public static String date2yyyyMMddWeek(Date date) {
          Calendar calendar = Calendar.getInstance();
          calendar.setTime(date);
          int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
          return new SimpleDateFormat("yyyy年MM月dd日 星期").format(date) + WEEK[dayOfWeek - 1];
      }
  
  }