/**
 * ClassName:DateUtil
 * Description TODO DateUtil
 * created by BAI
 * Data 2016/12/29 14:32
 */
public class DateUtil {
   public final static String TIMEFORMAT1 = "MM-dd";
   public final static String TIMEFORMAT2 = "yyyy-MM-dd HH:mm:ss";
   public final static String TIMEFORMAT3 = "HH:mm";
   public final static String TIMEFORMAT4 = "yyyy-MM-dd";
   public final static String TIMEFORMAT5 = "yyyy年MM月";
   public final static String TIMEFORMAT6 = "MM";
   public final static String TIMEFORMAT7 = "yyyy";
   public final static String TIMEFORMAT8 = "yyyy年MM月dd日";
   public static String getCurrentTimeString() {
        SimpleDateFormat fn = new SimpleDateFormat(TIMEFORMAT2);
        String str = fn.format(new Date(System.currentTimeMillis()));
        return str;
    }

   public static String getCurrentTimeHMString() {
      SimpleDateFormat fn = new SimpleDateFormat(TIMEFORMAT3);
      String str = fn.format(new Date(System.currentTimeMillis()));
      return str;
   }

   public static String getCurrentYearString() {
      SimpleDateFormat fn = new SimpleDateFormat(TIMEFORMAT7);
      String str = fn.format(new Date(System.currentTimeMillis()));
      return str;
   }

   public static String getCurrentTimeYYHMString() {
      SimpleDateFormat fn = new SimpleDateFormat(TIMEFORMAT4);
      String str = fn.format(new Date(System.currentTimeMillis()));
      return str;
   }

   public static String getLongTimeString(long time) {
      SimpleDateFormat fn = new SimpleDateFormat(TIMEFORMAT2);
      String str = fn.format(new Date(time));
      return str;
   }

   public static String getMMDDDate(String time) {
      return StringToDate(time, TIMEFORMAT2, TIMEFORMAT1);
   }

   public static String getHHMMDate(String str) {
      return StringToDate(str, TIMEFORMAT2, TIMEFORMAT3);
   }

   public static String getYYMMDate(String str) {
      return StringToDate(str, TIMEFORMAT2, TIMEFORMAT5);
   }

   public static String getMMDate(String str) {
      return StringToDate(str, TIMEFORMAT5, TIMEFORMAT6);
   }

   public static String getYYYYDate(String str) {
      return StringToDate(str, TIMEFORMAT5, TIMEFORMAT7);
   }

   public static String getYMDHMSDate(String str) {
      return StringToDate(str, TIMEFORMAT8, TIMEFORMAT2);
   }

   public static String getYYMMDDDate(String str) {
      return StringToDate(str, TIMEFORMAT2, TIMEFORMAT4);
   }

   public static String getYYMMDD42Date(String str) {
      return StringToDate(str, TIMEFORMAT4, TIMEFORMAT2);
   }

   public static String StringToDate(String dateStr, String dateFormatStr, String formatStr) {
      DateFormat sdf = new SimpleDateFormat(dateFormatStr);
      Date date = null;
      try{
         date = sdf.parse(dateStr);
      } catch (ParseException e){
         e.printStackTrace();
      }
      SimpleDateFormat s = new SimpleDateFormat(formatStr);

      return s.format(date);
   }

   public static String getLastDayDate(String time) {
      Date date = null;
      try {
         date = ConvertToDate(time,TIMEFORMAT4);
      } catch (Exception e) {
         e.printStackTrace();
      }
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.DAY_OF_MONTH, -1); //今天的时间加-天
      date = calendar.getTime();
      return ConvertToString(date,TIMEFORMAT4);
   }

   public static String getNextDayDate(String time) {
      Date date = null;
      try {
         date = ConvertToDate(time,TIMEFORMAT4);
      } catch (Exception e) {
         e.printStackTrace();
      }
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.DAY_OF_MONTH, +1); //今天的时间加一天
      date = calendar.getTime();
      return ConvertToString(date,TIMEFORMAT4);
   }

   public static String getLastMonthDate(String time) {
      Date date = null;
      try {
         date = ConvertToDate(time,TIMEFORMAT5);
      } catch (Exception e) {
         e.printStackTrace();
      }
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.MONTH, -1);
      date = calendar.getTime();
      return ConvertToString(date,TIMEFORMAT5);
   }

   public static String getNextMonthDate(String time) {
      Date date = null;
      try {
         date = ConvertToDate(time,TIMEFORMAT5);
      } catch (Exception e) {
         e.printStackTrace();
      }
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.MONTH, +1);
      date = calendar.getTime();
      return ConvertToString(date,TIMEFORMAT5);
   }

   //把日期转为字符串
   public static String ConvertToString(Date date,String dateFormat) {
      DateFormat df = new SimpleDateFormat(dateFormat);

      return df.format(date);
   }
   //把字符串转为日期
   public static Date ConvertToDate(String strDate,String dateFormat) throws Exception {
      DateFormat df = new SimpleDateFormat(dateFormat);
      return df.parse(strDate);
   }
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐