---
title: "JAVA中不错的处理日期工具单元"
author: "yeminping"
date: "2009-02-10T10:54:22Z"
tags: []
slug: "java"
draft: false
lastmod: "2009-02-10T10:54:22Z"
id: 326047
url: "https://yeminping.iteye.com/blog/326047"
---

/\*\*Revision Information:  
\*@version 1.0 2006-7-31 release(fanrui)  
\*/  
import java.util.Calendar;  
import java.util.Date;  
import java.text.ParseException;  
import java.text.SimpleDateFormat;

public class DateUtilTools {

     /\**  
          * 方法名: 功能描述: 两个参数格式必须为20050827，而且不能为空。 参数说明： 第一个参数小于第二个参数返回true 返回值: //  
          * 函数返回值的说明 其他: // 其它说明  
          \*/  
     public static boolean lessThan(String preDate, String date) {  
if (preDate == null && date == null) {  
      return false;  
} else if (preDate == null) {  
      return true;  
} else if (date == null) {  
      return false;  
}  
preDate = preDate.trim();  
date = date.trim();  
if (preDate.length() == 8 && date.length() == 8) {  
      Integer date1 = new Integer(preDate);  
      Integer date2 = new Integer(date);  
      if (date1.compareTo(date2) &lt; 0) {  
   return true;  
      }  
}  
return false;  
     }

     /\**  
          * 方法名: 功能描述: 两个参数格式必须为20050827，而且不能为空。 参数说明： 第一个参数大于第二个参数返回true 返回值: //  
          * 函数返回值的说明 其他: // 其它说明  
          \*/  
     public static boolean greaterThan(String preDate, String date) {  
if (preDate == null && date == null) {  
      return false;  
} else if (preDate == null) {  
      return false;  
} else if (date == null) {  
      return true;  
}  
preDate = preDate.trim();  
date = date.trim();  
if (preDate.length() == 8 && date.length() == 8) {  
      Integer date1 = new Integer(preDate);  
      Integer date2 = new Integer(date);  
      if (date1.compareTo(date2) &gt; 0) {  
   return true;  
      }  
}  
return false;  
     }

     /\**  
          * 方法名: 功能描述: 两个参数格式必须为20050827，而且不能为空。 参数说明： 返回值: // 函数返回值的说明 其他: //  
          * 其它说明  
          \*/  
     public static boolean equal(String preDate, String date) {  
if (preDate == null && date == null) {  
      return false;  
} else if (preDate == null) {  
      return false;  
} else if (date == null) {  
      return true;  
}  
preDate = preDate.trim();  
date = date.trim();  
if (preDate.length() == 8 && date.length() == 8) {  
      Integer date1 = new Integer(preDate);  
      Integer date2 = new Integer(date);  
      if (date1.compareTo(date2) == 0) {  
   return true;  
      }  
}  
return false;  
     }

     /\**  
          * @param 19位的时间  
          *                 yyyy-MM-dd HH:mm:ss  
          * @return 15位的时间 yyyyMMdd HHmmss  
          \*/  
     public static String \_time19To15(String time\_19) {  
String time\_15 = "";  
if (time\_19 == null || "".equals(time\_19) || time\_19.length() != 19) {  
      time\_15 = "";  
} else {  
      String\[] r = time\_19.replace('-', '#').replace(':', '#').split("#");  
      for (int i = 0; i &lt; r.length; i++) {  
   time\_15 += r\[i];  
      }  
}  
return time\_15;  
     }

     /\**  
          * @param 15位的时间  
          *                 yyyyMMdd HHmmss  
          * @return 19位的时间 yyyy-MM-dd HH:mm:ss  
          \*/  
     public static String \_time15To19(String time\_15) {  
String time\_19 = "";  
if (time\_15 == null || "".equals(time\_15) || time\_15.length() != 15) {  
      time\_19 = "";  
} else {  
      String y = time\_15.substring(0, 4);  
      String m = time\_15.substring(4, 6);  
      String d = time\_15.substring(6, 8);  
      String h = time\_15.substring(9, 11);  
      String mi = time\_15.substring(11, 13);  
      String s = time\_15.substring(13, 15);  
      time\_19 = y + "-" + m + "-" + d + " " + h + ":" + mi + ":" + s;  
}  
return time\_19;  
     }

     /\**  
          * @param 16位的时间  
          *                 yyyy-MM-dd HH:mm  
          * @return 13位的时间 yyyyMMdd HHmm  
          \*/  
     public static String \_time16To13(String time\_16) {  
String time\_13 = "";  
if (time\_16 == null || "".equals(time\_16) || time\_16.length() != 16) {  
      time\_13 = "";  
} else {  
      String\[] r = time\_16.replace('-', '#').replace(':', '#').split("#");  
      for (int i = 0; i &lt; r.length; i++) {  
   time\_13 += r\[i];  
      }  
}  
return time\_13;  
     }

     /\**  
          * @param 13位的时间  
          *                 yyyyMMdd HHmm  
          * @return 16位的时间 yyyy-MM-dd HH:mm  
          \*/  
     public static String \_time13To16(String time\_13) {  
String time\_16 = "";  
if (time\_13 == null || "".equals(time\_13) || time\_13.length() != 13) {  
      time\_16 = "";  
} else {  
      String y = time\_13.substring(0, 4);  
      String m = time\_13.substring(4, 6);  
      String d = time\_13.substring(6, 8);  
      String h = time\_13.substring(9, 11);  
      String mi = time\_13.substring(11, 13);  
      time\_16 = y + "-" + m + "-" + d + " " + h + ":" + mi;  
}  
return time\_16;  
     }

     /\**  
          * @param 10位的日期  
          *                 yyyy-MM-dd  
          * @return 8位的日期 yyyyMMdd  
          \*/  
     public static String \_date10To8(String date\_10) {  
String date\_8 = "";  
if (date\_10 == null || "".equals(date\_10) || date\_10.length() != 10) {  
      date\_8 = "";  
} else {  
      String\[] r = date\_10.split("-");  
      for (int i = 0; i &lt; r.length; i++) {  
   date\_8 += r\[i];  
      }  
}  
return date\_8;  
     }

     /\**  
          * @param 8位的日期  
          *                 yyyyMMdd  
          * @return 10位的日期 yyyy-MM-dd  
          \*/  
     public static String \_date8To10(String date\_8) {  
String date\_10 = "";  
if (date\_8 == null || "".equals(date\_8) || date\_8.length() != 8) {  
      date\_10 = "";  
} else {  
      String y = date\_8.substring(0, 4);  
      String m = date\_8.substring(4, 6);  
      String d = date\_8.substring(6, 8);  
      date\_10 = y + "-" + m + "-" + d;  
}  
return date\_10;  
     }

     /\**  
          * @param 7位的日期  
          *                 yyyy-MM  
          * @return 6位的日期 yyyyMM  
          \*/  
     public static String \_date7To6(String date\_7) {  
String date\_6 = "";  
if (date\_7 == null || "".equals(date\_7) || date\_7.length() != 7) {  
      date\_6 = "";  
} else {  
      String\[] r = date\_7.split("-");  
      for (int i = 0; i &lt; r.length; i++) {  
   date\_6 += r\[i];  
      }  
}  
return date\_6;  
     }

     /\**  
          * @param 6位的日期  
          *                 yyyyMM  
          * @return 7位的日期 yyyy-MM  
          \*/  
     public static String \_date6To7(String date\_6) {  
String date\_7 = "";  
if (date\_6 == null || "".equals(date\_6) || date\_6.length() != 6) {  
      date\_7 = "";  
} else {  
      String y = date\_6.substring(0, 4);  
      String m = date\_6.substring(4, 6);  
      date\_7 = y + "-" + m;  
}  
return date\_7;  
     }

     /\**  
          * Calendar 转 String  
          * @param Calendar  
          * @return String YYYY-MM-DD HH:mm:ss  
          \*/  
     public static String getString(Calendar cal) {  
String month = "";  
String day = "";  
String hour = "";  
String minute = "";  
String second = "";  
if ((cal.get(Calendar.MONTH) + 1) &lt; 10)  
      month = "0" + (cal.get(Calendar.MONTH) + 1);  
else  
      month = "" + (cal.get(Calendar.MONTH) + 1);

if (cal.get(Calendar.DAY\_OF\_MONTH) &lt; 10)  
      day = "0" + cal.get(Calendar.DAY\_OF\_MONTH);  
else  
      day = "" + cal.get(Calendar.DAY\_OF\_MONTH);

if (cal.get(Calendar.HOUR\_OF\_DAY) &lt; 10)  
      hour = "0" + cal.get(Calendar.HOUR\_OF\_DAY);  
else  
      hour = "" + cal.get(Calendar.HOUR\_OF\_DAY);

if (cal.get(Calendar.MINUTE) &lt; 10)  
      minute = "0" + cal.get(Calendar.MINUTE);  
else  
      minute = "" + cal.get(Calendar.MINUTE);

if (cal.get(Calendar.MINUTE) &lt; 10)  
      minute = "0" + cal.get(Calendar.MINUTE);  
else  
      minute = "" + cal.get(Calendar.MINUTE);

if (cal.get(Calendar.SECOND) &lt; 10)  
      second = "0" + cal.get(Calendar.SECOND);  
else  
      second = "" + cal.get(Calendar.SECOND);

return cal.get(Calendar.YEAR) + "-" + month + "-" + day + " " + hour  
   + ":" + minute + ":" + second;  
     }

     /\**  
          * @param Calendar  
          * @return String YYYY-MM-DD  
          \*/  
     public static String getShortString(Calendar cal) {  
String month = "";  
String day = "";  
if ((cal.get(Calendar.MONTH) + 1) &lt; 10)  
      month = "0" + (cal.get(Calendar.MONTH) + 1);  
else  
      month = "" + (cal.get(Calendar.MONTH) + 1);

if (cal.get(Calendar.DAY\_OF\_MONTH) &lt; 10)  
      day = "0" + cal.get(Calendar.DAY\_OF\_MONTH);  
else  
      day = "" + cal.get(Calendar.DAY\_OF\_MONTH);  
return cal.get(Calendar.YEAR) + "-" + month + "-" + day;// +"  
// "+hour+":"+minute+":"+second;  
     }

     /\**  
          * @param String  
          *                 YYYY-MM-DD HH:mm:ss  
          * @return Calendar YYYY-MM-DD HH:mm:ss  
          \*/  
     public static Calendar toCalender(String time) {

Calendar calendar = Calendar.getInstance();  
int year = Integer.parseInt(time.substring(0, 4));  
int month = Integer.parseInt(time.substring(5, 7));  
int day = Integer.parseInt(time.substring(8, 10));  
int hour = Integer.parseInt(time.substring(11, 13));  
int munite = Integer.parseInt(time.substring(14, 16));  
int second = Integer.parseInt(time.substring(17, 19));  
calendar.set(year, month - 1, day, hour, munite, second);  
return calendar;  
     }

     /\**  
          * @param String  
          *                 YYYY-MM-DD  
          * @return Calendar YYYY-MM-DD  
          \*/  
     public static Calendar toDateCalender(String time) {  
Calendar calendar = Calendar.getInstance();  
int year = Integer.parseInt(time.substring(0, 4));  
int month = Integer.parseInt(time.substring(5, 7));  
int day = Integer.parseInt(time.substring(8, 10));  
int hour = Integer.parseInt("00");  
int munite = Integer.parseInt("00");  
int second = Integer.parseInt("00");  
calendar.set(year, month - 1, day, hour, munite, second);  
return calendar;  
     }

     /\**  
          * @param String  
          * @return Date  
          \*/  
     public static java.util.Date stringToDate(String dateStr) {  
return java.sql.Date.valueOf(dateStr);  
     }

     /\**  
          * @return 系统时间String  
          \*/  
     public static String getSystemTime\_String() {  
return DateUtilTools.getString(Calendar.getInstance());  
     }

     /\**  
          * @return 系统时间 Date  
          \*/  
     public static Date getSystemTimeDate() {  
String nowdate = DateUtilTools.getShortString(Calendar.getInstance());  
return DateUtilTools.stringToDate(nowdate);  
     }

     /\**  
          * @return 系统时间 String  
          \*/  
     public static String getNowTime\_String() {  
java.util.Date now = new java.util.Date();  
return DateUtilTools.getLongTimeStr(now);  
     }

     /\**  
          * 将字符串日期转换为Date对象  
          * @param str  
          * @return  
          * @throws ParseException  
          \*/  
     public static Date getDateFromstr(String str) {  
Date theDate = null;  
if(str.length()&lt;=10){  
      str+=" 00:00:00";  
}  
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
try{  
      theDate = sdf.parse(str);  
}catch(Exception ex){ex.printStackTrace();}  
return theDate;  
     }

     /\**  
          * 将Date时间转换为长日期字符串('yyyy-MM-dd HH:mm:ss'格式)  
          * @param t  
          * @return  
          \*/  
     public static String getLongTimeStr(Date t) {  
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
return formatter.format(t);  
     }

     /\**  
          * 将Date时间转换为短日期字符串('yyyy-MM-dd'格式)  
          * @param t  
          * @return  
          \*/  
     public static String getShortTimeStr(Date t) {  
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
return formatter.format(t);  
     }

/\**  
   * 将长日期字符串('yyyy-MM-dd HH:mm:ss'格式)转换为短日期字符串('yyyy-MM-dd'格式)  
   * @param calendar  
   * @return  
   \*/  
public static String getShortDateStr(String str) {  
int pos = str.indexOf(' ');  
if (pos &gt; 0) {  
      return str.substring(0, pos);  
}  
return str;  
     }

/\**  
   * 将长日期字符串('yyyy-MM-dd HH:mm:ss'格式)转换为短时间字符串('HH:mm'格式)  
   * @param calendar  
   * @return  
   \*/  
public static String getShortTimeStr(String str) {  
int pos = str.indexOf(' ');  
if (pos &gt; 0) {  
      str = str.substring(pos + 1);  
}  
pos = str.lastIndexOf(':');  
if (pos &gt; 0) {  
      return str.substring(0, pos);  
}  
return str;  
     }

     /\**  
          * 返回当前的年月 yyyy-mm  
          * @return String  
          \*/  
     public static String getSysMonth7() {  
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
String str = formatter.format(new Date());  
if (str == null || "".equals(str)) {  
      str = "";  
} else {  
      str = str.substring(0, 7);  
}  
return str;  
     }  
     
     /\**  
      * 返回当前的年月 yyyymm  
      * @return String  
      \*/  
public static String getSysMonth6() {  
   return getSysMonth7().replaceFirst("-","");  
}

     /\**  
          * 返回当前的整点时间 如:2006-08-07 17:00:00  
          * @return  
          \*/  
public static String getCurrentDateAndHour() {  
Calendar cal = Calendar.getInstance();  
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.HOUR\_OF\_DAY), 0, 0);  
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
return formatter.format(new Date(cal.getTimeInMillis()));  
     }

     /\**  
          * 返回当前的时间的小时数  
          * @return  
          \*/  
     public static String getCurrentHour() {  
Calendar cal=Calendar.getInstance();  
return String.valueOf(cal.get(Calendar.HOUR\_OF\_DAY));  
     }

     /\**  
          * 返回输入的整点时间 如:2006-08-07 17:00:00  
          * @param cal  
          * @return  
          \*/  
     public static String getCurrentDateAndHour(Calendar cal) {

cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.HOUR\_OF\_DAY), 0, 0);

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return formatter.format(new Date(cal.getTimeInMillis()));  
     }

     /\**  
          * 返回输入时间的小时数  
          * @param cal  
          * @return  
          \*/  
     public static String getCurrentHour(Calendar cal) {  
return String.valueOf(cal.get(Calendar.HOUR\_OF\_DAY));  
     }

     /\**  
          * 返回年

          * @return int  
          \*/  
     public static int getYear() {  
return Calendar.getInstance().get(1);  
     }

     /\**  
          * 返回月  
          * @return int  
          \*/  
     public static int getMonth() {  
return Calendar.getInstance().get(Calendar.MONTH) + 1;  
     }

     /\**  
          * 返回日  
          * @return int  
          \*/  
     public static int getDay() {  
return Calendar.getInstance().get(Calendar.DAY\_OF\_MONTH);  
     }

     /\**  
          * 判断是否是润年

          * @return boolean  
          \*/  
     public static boolean isLunYear(int year) {  
return (year % 4 == 0 && year % 100 == 0) || year % 400 == 0;  
     }

     /\**  
          * 当前时间，日加两天  
          * @return String  
          \*/  
     public static String getSdate() {  
Calendar cal = Calendar.getInstance();  
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal  
   .get(Calendar.DATE) + 2);  
return DateUtilTools.getShortString(cal);  
     }

     /\**  
          \*当前时间，月加一个月  
          * @return String  
          \*/  
     public static String getEdate() {  
Calendar cal = Calendar.getInstance();  
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal  
   .get(Calendar.DATE) + 2);  
return DateUtilTools.getShortString(cal);  
     }

     /\**  
          * 计算指定年和月的总天数, 要输入年份的原因是要判断二月29天还是28天  
          * @param int  
          *                 Month ,int Year  
          * @return int  
          \*/  
     public static int getCountMonth(int Month, int Year) {  
int Dates = 0;  
if (Month &lt; 0 || Month &gt; 12) {  
      System.out.println("Month Error");  
}  
if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8  
   || Month == 10 || Month == 12) {  
      Dates = 31;  
}  
if (Month == 2 && DateUtilTools.isLunYear(Year)) {  
      Dates = 29;  
}  
if (Month == 2 && !DateUtilTools.isLunYear(Year)) {  
      Dates = 28;  
}  
if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {  
      Dates = 30;  
}  
return Dates;  
     }

     /\**  
          * 计算当月总天数  
          * @return int  
          \*/  
     public static int getCountMonth() {  
   Calendar thisMonth = Calendar.getInstance();     //声明一个当前日期  
   return thisMonth.getActualMaximum(Calendar.DAY\_OF\_MONTH);     //取得当月共有多少天  
     }

    /\**  
          * 将Date时间转换为短日期字符串('yyyyMMdd'格式)  
          * @param t  
          * @return  
          \*/  
     public static String getShortDateStr(Date t) {  
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");  
return formatter.format(t);  
     }

     /\**  
      * 比较两个Calendar 的大小，第一个比第二个小 返回 true ，反之 false  
      * Calendar cal,Calendar cal2  
      * @param cal  
      * @param cal2  
      * @return boolean  
      \*/  
public static boolean compareCalendar(Calendar cal,Calendar cal2)//日期的比较,前者比后者早  
{  
   return cal.before(cal2);  
}

/\**  
   * 返回两个Calendar 之间的天数  
   * @param Calendar cal  
   * @param Calendar cal2  
   * @return long  
   \*/  
     public static long betweenCalendarDays(Calendar cal,Calendar cal2){  
      long days=0;  
      Date d1=cal.getTime();  
      Date d2=cal2.getTime();  
      days=d1.getTime()-d2.getTime();  
      days=days/3600/24/1000;  
      return days;  
     }  
}