首页 > 代码库 > 日期格式数位工具类:日期格式互转

日期格式数位工具类:日期格式互转

public final static String PATTERN = "yyyy-MM-dd";

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

 

public static Date parseString(String date, String format)

     throws ParseException {

     SimpleDateFormat dateformat =

        new SimpleDateFormat(format);   

     Date timestamp = dateformat.parse(date);

        return timestamp;  

}

public static String parseDate(Date date, String format) {
  if (date == null) {
   return null;
  }
  SimpleDateFormat dateformat = new SimpleDateFormat(format);
  String timestamp = dateformat.format(date);
  return timestamp;
 }

public static String payPremiumToYuan(String payPremium) {
  BigDecimal bdl = BigDecimal.valueOf(Integer.valueOf(payPremium));
  String result = String.valueOf(bdl.multiply(BigDecimal.valueOf(0.01)));
  return result;
 }

/**
  * 获取本周第一天
  *
  * @return
  */
 public static String getFirstDayOfWeek() {
  Calendar c = Calendar.getInstance(Locale.CHINA);
  c.setFirstDayOfWeek(Calendar.MONDAY);
  c.setTimeInMillis(System.currentTimeMillis());
  c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  return new SimpleDateFormat(PATTERN).format(c.getTime());
 }

 

/**
  * 获取本周最后一天
  *
  * @return
  */
 public static String getLastDayOfWeek() {
  String firstDayOfWeek = getFirstDayOfWeek();
  try {
   Date date = parseString(firstDayOfWeek, PATTERN);
   Calendar c = Calendar.getInstance();
   c.setTime(date);
   c.add(Calendar.DAY_OF_MONTH, 6);
   return new SimpleDateFormat(PATTERN).format(c.getTime());
  } catch (ParseException e) {
   e.printStackTrace();
  }
  return "";
 }

 

/**
  * 获取本月第一天
  *
  * @return
  */
 public static String getFirstDayOfMonth() {
  Calendar c = Calendar.getInstance(Locale.CHINA);
  c.set(Calendar.DAY_OF_MONTH, 1);
  return new SimpleDateFormat(PATTERN).format(c.getTime());
 }

 

/**
  * 获取本月最后一天
  *
  * @return
  */
 public static String getLastDayOfMonth() {
  Calendar c = Calendar.getInstance(Locale.CHINA);
  c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
  return new SimpleDateFormat(PATTERN).format(c.getTime());
 }

测试以上方法:

 public static void main(String[] args) {  

 Calendar c1 = Calendar.getInstance();  

 c1.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH), 0, 0, 0);   Calendar c2 = Calendar.getInstance();   c2.set(c2.get(Calendar.YEAR), c2.get(Calendar.MONTH), c2.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

  try {    Date beginDate = DateUtil.parseString("2016-04-10", DateUtil.PATTERN);    Date endDate = DateUtil.parseString("2016-04-14", DateUtil.PATTERN);    Calendar c = Calendar.getInstance();    c.setTime(beginDate);    long stepCount = 0L;    while (beginDate.before(endDate)) {     String userStepCount = "200";     if (!StringUtils.isEmpty(userStepCount)) {      stepCount += Long.parseLong(userStepCount);     }     c.add(Calendar.DAY_OF_MONTH, 1);     beginDate = c.getTime();    }   } catch (NumberFormatException e) {    e.printStackTrace();   } catch (ParseException e) {    e.printStackTrace();   }  }

 

========================================================

 

/**
  * 将日期字符串转为日期
  * @param date
  * @param format
  * @return
  */
 public static Date parseDate(String date,String format){
  if(date==null){
   return null;
  }
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  try{
   return sdf.parse(date);
  }catch (ParseException e) {
   return null;
  }
 }

 /**
  * 将日期字符串转为日期
     * @param date
  * @return
  */
 public static Date parseDate(String date){
  if(date!=null){
   date = date.replaceAll("\\D", "");
   return parseDate(date,"yyyyMMdd");
  }
  return null;
 }
 
  /**
  * 将日期转换为指定格式字符串
  * @param date
  * @param format
  * @return
  */
 public static String formatDate(Date date,String format){
  if(date==null){
   return "";
  }
  try{
   SimpleDateFormat sdf = new SimpleDateFormat(format);
   return sdf.format(date);
  }catch(IllegalArgumentException e){
   return "";
  }
 }
 

/**
  * 将日期字符串转换为指定的格式
  * @param date
  * @param srcFormat
  * @param destFormat
  * @return
  */
 public static String formatDate(String date,String srcFormat,String destFormat){
  if(date==null){
   return "";
  }
  Date time = parseDate(date,srcFormat);
  return formatDate(time,destFormat);
 }

 

/**
  * 将日期字符串转换为指定的格式
  * @param date
  * @param format
  * @return
  */
 public static String formatDate(String date,String format){
  if(date==null){
   return "";
  }
  date = date.replaceAll("\\D", "");
  return formatDate(date,"yyyyMMdd",format);
 }

/**
  * 加减日期字段
  * @param date
  * @param addNum
  * @param field  同Calendar add 参数
  * @return
  */
 public static Date addDateField(Date date,int addNum, int field) { 
  Calendar calendar =  Calendar.getInstance();
  calendar.setTime(date);
  calendar.add(field, addNum);
  return calendar.getTime();
 }
 

 /**
  * 设置日期字段
  * @param date
  * @param value
  * @param field  同Calendar set 参数
  * @return
  */
 public static Date setDateField(Date date,int value, int field) { 
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(date);
  calendar.set(field, value);
  return calendar.getTime();
 }

 

/**
  * 获取给定日期所在月的最后一天
  * @param date
  * @return
  */
 public static Date getLastDayOfMonth(Date date){
  date = addDateField(date,1, Calendar.MONTH);
  date = setDateField(date,1, Calendar.DAY_OF_MONTH);
  date = addDateField(date,-1, Calendar.DAY_OF_YEAR);
  return date;
 }

/**
  * 返回以指定元素格式截去一部分的日期值
  * @param date
  * @param field
  * @return
  */
 public static Date truncDate(Date date,int field){
  Calendar calendar =  Calendar.getInstance();
  calendar.setTime(date);
  if(Calendar.MILLISECOND>=field)
   date = setDateField(date,0, Calendar.MILLISECOND);
  if(Calendar.SECOND>=field)
   date = setDateField(date,0, Calendar.SECOND);
  if(Calendar.MINUTE>=field)
   date = setDateField(date,0, Calendar.MINUTE);
  if(Calendar.HOUR_OF_DAY>=field)
   date = setDateField(date,0, Calendar.HOUR_OF_DAY);
  if(Calendar.DAY_OF_WEEK==field)
   date = setDateField(date,Calendar.SUNDAY, Calendar.DAY_OF_WEEK);
  if(Calendar.DAY_OF_MONTH==field)
   date = setDateField(date,1, Calendar.DAY_OF_MONTH);
  if(Calendar.DAY_OF_YEAR==field)
   date = setDateField(date,1, Calendar.DAY_OF_YEAR);
  return date;
 }
 

/**
  *
  * @param beginDate
  * @param endDate
  * @return
  */
 public static long compareTowDate(String beginDate,String endDate){
  return compareTowDate(parseDate(beginDate),parseDate(endDate));
 }

/**
  *
  * @param beginDate
  * @param endDate
  * @return
  */
 public static long compareTowDate(Date beginDate,Date endDate){
  long beginTime = beginDate==null?Long.MAX_VALUE:beginDate.getTime();
  long endTime = endDate==null?Long.MAX_VALUE:endDate.getTime();
  return endTime - beginTime;
 }

/**
  * 时间区间是否有重叠
  * @param beginDate1 时间段1的开始时间
  * @param endDate1  时间段1的结束时间
  * @param beginDate2 时间段2的开始时间
  * @param endDate2  时间段2的结束时间
  * @return 没有重叠时返回 true,重叠返回false
  */
 public static boolean checkDateRangesConstraint(Date beginDate1,Date endDate1,Date beginDate2,Date endDate2){
  boolean checked = true;
  long beginDate1Time = beginDate1==null?Long.MIN_VALUE:beginDate1.getTime();
  long beginDate2Time = beginDate2==null?Long.MIN_VALUE:beginDate2.getTime();
  long endDate1Time = endDate1==null?Long.MAX_VALUE:endDate1.getTime();
  long endDate2Time = endDate2==null?Long.MAX_VALUE:endDate2.getTime();
  //交叉的情况
  if(beginDate1Time<=beginDate2Time&&endDate1Time>beginDate2Time){
   checked = false;
  }
  //交叉的情况
  if(beginDate1Time<endDate2Time&&endDate1Time>=endDate2Time){
   checked = false;
  }
  //包含的情况
  if(checkDateRangesInclude(beginDate1,endDate1,beginDate2,endDate2)){
   checked = false;
  }
  //包含的情况
  if(checkDateRangesInclude(beginDate2,endDate2,beginDate1,endDate1)){
   checked = false;
  }
  return checked;
 }

/**
  * 时间区间是否有重叠
  * @param beginDate1 时间段1的开始时间
  * @param endDate1  时间段1的结束时间
  * @param beginDate2 时间段2的开始时间
  * @param endDate2  时间段2的结束时间
  * @return 没有重叠时返回 true,重叠返回false
  */
 public static boolean checkDateRangesConstraint(String beginDate1,String endDate1,String beginDate2,String endDate2){
  return checkDateRangesConstraint(parseDate(beginDate1),parseDate(endDate1),parseDate(beginDate2),parseDate(endDate2));
 }
 

/**
  * 时间区间是否在另一时间期间内
  * @param beginInnerDate 小时间段的开始时间
  * @param endInnerDate 小时间段的结束时间
  * @param beginOutDate 大时间段的开始时间
  * @param endOutDate  大时间段的结束时间
  * @return 当是包含关系时返回 true,否则返回false
  */
 public static boolean checkDateRangesInclude(Date beginInnerDate,Date endInnerDate,Date beginOutDate,Date endOutDate){
  boolean checked = true;
  long beginInnerTime = beginInnerDate==null?Long.MIN_VALUE:beginInnerDate.getTime();
  long beginOutTime = beginOutDate==null?Long.MIN_VALUE:beginOutDate.getTime();
  long endInnerTime = endInnerDate==null?Long.MAX_VALUE:endInnerDate.getTime();
  long endOutTime = endOutDate==null?Long.MAX_VALUE:endOutDate.getTime();
  
  if(beginInnerTime<beginOutTime){
   checked = false;
  }
  if(endInnerTime>endOutTime){
   checked = false;
  } 
  return checked;
 }

/**
  * 时间区间是否在另一时间期间内
  * @param beginInnerDate 小时间段的开始时间
  * @param endInnerDate 小时间段的结束时间
  * @param beginOutDate 大时间段的开始时间
  * @param endOutDate  大时间段的结束时间
  * @return 当是包含关系时返回 true,否则返回false
  */
 public static boolean checkDateRangesInclude(String beginInnerDate,String endInnerDate,String beginOutDate,String endOutDate){
  return checkDateRangesInclude(parseDate(beginInnerDate),parseDate(endInnerDate),parseDate(beginOutDate),parseDate(endOutDate));
 }

 

/**
  * 时间区间是否有交集
  * @param beginDate1 时间段1的开始时间
  * @param endDate1 时间段1的结束时间
  * @param beginDate2 时间段2的开始时间
  * @param endDate2  时间段2的结束时间
  * @return 当有交集时返回 true,否则返回false
  */
 public static boolean checkDateRangesMixed(Date beginDate1,Date endDate1,Date beginDate2,Date endDate2){
  boolean checked = true;
  long beginTime1 = beginDate1==null?Long.MIN_VALUE:beginDate1.getTime();
  long beginTime2 = beginDate2==null?Long.MIN_VALUE:beginDate2.getTime();
  long endTime1 = endDate1==null?Long.MAX_VALUE:endDate1.getTime();
  long endTime2 = endDate2==null?Long.MAX_VALUE:endDate2.getTime();
  if(beginTime1>endTime2){
   checked = false;
  } 
  
  if(beginTime2>endTime1){
   checked = false;
  }
  
  if(beginTime1==endTime2){
   if(endTime2!=beginTime2)
   checked = false;
  }
  
  if(beginTime2==endTime1){
   if(endTime1!=beginTime1)
    checked = false;
  }
  
  return checked;
 }
 

 /**
  * 时间区间是否有交集
  * @param beginDate1 时间段1的开始时间
  * @param endDate1 时间段1的结束时间
  * @param beginDate2 时间段2的开始时间
  * @param endDate2  时间段2的结束时间
  * @return 当有交集时返回 true,否则返回false
  */
 public static boolean checkDateRangesMixed(String beginDate1,String endDate1,String beginDate2,String endDate2){
  return checkDateRangesMixed(parseDate(beginDate1),parseDate(endDate1),parseDate(beginDate2),parseDate(endDate2));
 }

 public static int calculateAge(String birth) {
  int age = -1;
  Date birthDay = parseDate(birth,"yyyy-MM-dd");
  Date sysdate = truncDate(new Date(),Calendar.HOUR_OF_DAY);
  while(compareTowDate(birthDay,sysdate)>0){
   age +=1;
   birthDay = addDateField(birthDay,1,Calendar.YEAR);
  }
  return age;
 }
 

public static Date addMonth(Date date,int value) {
  Calendar calendar =  Calendar.getInstance();
  calendar.setTime(date);
  int actualDay = calendar.get(Calendar.DAY_OF_MONTH);
  int actualMaxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  calendar.add(Calendar.MONTH, value);
  if(actualDay ==actualMaxDay ){
   calendar.set(Calendar.DAY_OF_MONTH, actualMaxDay);
  }
  return calendar.getTime();
 }

 

测试以上方法:

public static void main(String[] args){   Calendar newCalendar =  Calendar.getInstance();   newCalendar.set(1999, 1, 28);   System.out.println(newCalendar.getMaximum(Calendar.DAY_OF_MONTH));   System.out.println(newCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));   System.out.println(newCalendar.getActualMinimum(Calendar.DAY_OF_MONTH));   System.out.println(newCalendar.getGreatestMinimum(Calendar.DAY_OF_MONTH));   System.out.println(newCalendar.getLeastMaximum(Calendar.DAY_OF_MONTH));

    }

 

日期格式数位工具类:日期格式互转