/**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) < 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) > 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 < 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 < 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 < 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 < 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) < 10)
month = "0" + (cal.get(Calendar.MONTH) + 1);
else
month = "" + (cal.get(Calendar.MONTH) + 1);
if (cal.get(Calendar.DAY_OF_MONTH) < 10)
day = "0" + cal.get(Calendar.DAY_OF_MONTH);
else
day = "" + cal.get(Calendar.DAY_OF_MONTH);
if (cal.get(Calendar.HOUR_OF_DAY) < 10)
hour = "0" + cal.get(Calendar.HOUR_OF_DAY);
else
hour = "" + cal.get(Calendar.HOUR_OF_DAY);
if (cal.get(Calendar.MINUTE) < 10)
minute = "0" + cal.get(Calendar.MINUTE);
else
minute = "" + cal.get(Calendar.MINUTE);
if (cal.get(Calendar.MINUTE) < 10)
minute = "0" + cal.get(Calendar.MINUTE);
else
minute = "" + cal.get(Calendar.MINUTE);
if (cal.get(Calendar.SECOND) < 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) < 10)
month = "0" + (cal.get(Calendar.MONTH) + 1);
else
month = "" + (cal.get(Calendar.MONTH) + 1);
if (cal.get(Calendar.DAY_OF_MONTH) < 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()<=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 > 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 > 0) {
str = str.substring(pos + 1);
}
pos = str.lastIndexOf(':');
if (pos > 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 < 0 || Month > 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;
}
}
JAVA中不错的处理日期工具单元
最新推荐文章于 2022-11-04 17:40:42 发布