/**
*
*/
package com.cn.elc.system.tools;
/**
* @author
* @日期工具
*/
import java.text.*;
import java.util.*;
public class DateTool {
public DateTool() {
}
public static Date stringToDate(String date) {
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(date);
}
catch(Exception err) {
return null;
}
}
public static Date dateToDate(Date date) {
try {
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
return simpledate.parse(simpledate.format(date));
}
catch(Exception err) {
return null;
}
}
public static Date stringToDateTime(String date) {
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
return sdf.parse(date);
}
catch(Exception err) {
return null;
}
}
public static Date stringToDateTimeSec(String date) {
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(date);
}
catch(Exception err) {
return null;
}
}
public static Date stringToDateTimeOne(String date) {
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.parse(date);
}
catch(Exception err) {
return null;
}
}
public static String dateToString(Date date) {
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
return simpledate.format(date);
}
public static String dateToStringTime(Date date) {
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return simpledate.format(date);
}
public static String dateToStringTimeSec(Date date) {
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpledate.format(date);
}
public static int getYear() {
Calendar ca = Calendar.getInstance();
ca.setTime(new java.util.Date());
int year = ca.get(Calendar.YEAR);
return year;
}
public static int getMonth() {
Calendar ca = Calendar.getInstance();
ca.setTime(new java.util.Date());
int month = ca.get(Calendar.MONTH);
return month;
}
public static int getDay() {
Calendar ca = Calendar.getInstance();
ca.setTime(new java.util.Date());
int day = ca.get(Calendar.DAY_OF_MONTH);
return day;
}
public static int getYear(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
int year = ca.get(Calendar.YEAR);
return year;
}
public static int getMonth(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
int month = ca.get(Calendar.MONTH);
return month;
}
public static int getDay(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
int day = ca.get(Calendar.DAY_OF_MONTH);
return day;
}
public static int getHourOfDay() {
Calendar ca = Calendar.getInstance();
ca.setTime(new java.util.Date());
int hour = ca.get(Calendar.HOUR_OF_DAY);
return hour;
}
public static String dateToTimeString() {
SimpleDateFormat simpledate = new SimpleDateFormat("yyyyMMddHHmmss");
return simpledate.format(new java.util.Date());
}
public static String dateToTimeString(Date date) {
SimpleDateFormat simpledate = new SimpleDateFormat("yyyyMMddHHmmss");
return simpledate.format(date);
}
public static Date timeStringToDate(String dateTime) {
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(dateTime);
}
catch(Exception err) {
return null;
}
}
public static String getYearMonthDay(String dateFormateArg) {
String dateFormate=null;
Calendar ca = Calendar.getInstance();
if(dateFormateArg!=null && !"".equals(dateFormateArg)) {
dateFormate=dateFormateArg;
}
else {
dateFormate="yyyyMMdd";
}
ca.setTime(new java.util.Date());
SimpleDateFormat simpledate = new SimpleDateFormat(dateFormate);
String date = simpledate.format(ca.getTime());
return date;
}
/**
* 计算两个时间间隔多少天,date1为先前时间,date2为现在时间。
*/
public static int getDistanceDay(Date date1,Date date2) {
int distanceDay=0;
if(date1!=null && date2!=null) {
Calendar ca1 = Calendar.getInstance();
Calendar ca2 = Calendar.getInstance();
ca1.setTime(date1);
ca2.setTime(date2);
int year1=ca1.get(Calendar.YEAR);
int year2=ca2.get(Calendar.YEAR);
if(year1!=year2) {
if((year2-year1)>=2) {
for(int i=year1+1;i<year2;i++) {
if(isLeapYear(i)) {
distanceDay=distanceDay+366;
}
else {
distanceDay=distanceDay+365;
}
}
}
if(isLeapYear(year1)) {
distanceDay=distanceDay+366-ca1.get(Calendar.DAY_OF_YEAR)+ca2.get(Calendar.DAY_OF_YEAR);
}
else {
distanceDay=distanceDay+365-ca1.get(Calendar.DAY_OF_YEAR)+ca2.get(Calendar.DAY_OF_YEAR);
}
}
else {
distanceDay = ca2.get(Calendar.DAY_OF_YEAR) - ca1.get(Calendar.DAY_OF_YEAR);
}
}
return distanceDay;
}
/**
* 计算两个时间间隔多少小时,date1为先前时间,date2为现在时间。
*/
public static int getDistanceHour(Date date1,Date date2) {
int distanceHour=0;
if(date1!=null && date2!=null) {
Calendar ca1 = Calendar.getInstance();
Calendar ca2 = Calendar.getInstance();
ca1.setTime(date1);
ca2.setTime(date2);
distanceHour = ca2.get(Calendar.HOUR_OF_DAY) - ca1.get(Calendar.HOUR_OF_DAY);
int year1=ca1.get(Calendar.YEAR);
int year2=ca2.get(Calendar.YEAR);
int distanceDay=0;
if(year1!=year2) {
if((year2-year1)>=2) {
for(int i=year1+1;i<year2;i++) {
if(isLeapYear(i)) {
distanceDay=distanceDay+366;
}
else {
distanceDay=distanceDay+365;
}
}
}
if(isLeapYear(year1)) {
distanceDay=distanceDay+366-ca1.get(Calendar.DAY_OF_YEAR)+ca2.get(Calendar.DAY_OF_YEAR);
}
else {
distanceDay=distanceDay+365-ca1.get(Calendar.DAY_OF_YEAR)+ca2.get(Calendar.DAY_OF_YEAR);
}
}
else {
distanceDay = ca2.get(Calendar.DAY_OF_YEAR) - ca1.get(Calendar.DAY_OF_YEAR);
}
distanceHour=distanceHour+distanceDay*24;
}
return distanceHour;
}
/**
* 计算两个时间间隔多少分钟,date1为先前时间,date2为现在时间。
*/
public static long getDistanceMinute(Date date1,Date date2) throws Exception {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date begin = dfs.parse(dateToStringTime(date1));
Date end = dfs.parse(dateToStringTime(date2));
long between = end.getTime() - begin.getTime(); /* 得到两者的毫秒数 */
between=(between/1000)/60;
return between;
}
/**
* 计算两个时间间隔多少秒,date1为先前时间,date2为现在时间。
*/
public static long getDistanceSecond(Date date1,Date date2) throws Exception {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date begin = dfs.parse(dateToStringTime(date1));
Date end = dfs.parse(dateToStringTime(date2));
long between = end.getTime() - begin.getTime(); /* 得到两者的毫秒数 */
between=between/1000;
return between;
}
/**
* 判断是否是闰年,闰年366天,非闰年365天
*/
public static boolean isLeapYear(int year) {
boolean isLeapYear = true;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
isLeapYear = true;
}
else {
isLeapYear = false;
}
return isLeapYear;
}
/**
* 得到指定月的天数
*/
public static int getMonthLastDay(int year,int month) {
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* 重新设置当天日期
*/
public static Date getHourOfDay(Date date,int hour) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.set(Calendar.HOUR_OF_DAY, hour);
return ca.getTime();
}
/**
* 计算几天后的日期
*/
public static Date getComputeDate(int days) {
Calendar ca = Calendar.getInstance();
ca.add(Calendar.DAY_OF_YEAR, days);
return ca.getTime();
}
/**
* 计算几天后的日期
*/
public static Date getComputeDate(Date date,int days) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.DAY_OF_YEAR, days);
return ca.getTime();
}
/**
* 计算几年后的日期
*/
public static Date getComputeYear(int years) {
Calendar ca = Calendar.getInstance();
ca.add(Calendar.YEAR, years);
return ca.getTime();
}
/**
* 计算几年后的日期
*/
public static Date getComputeMonth(int months) {
Calendar ca = Calendar.getInstance();
ca.add(Calendar.MONTH, months);
return ca.getTime();
}
/**
* 计算几年后的日期
*/
public static Date getComputeMonth(Date date,int months) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(Calendar.MONTH, months);
return ca.getTime();
}
/**
* 计算当天是星期几
*/
public static int getDayOfWeek(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
return ca.get(Calendar.DAY_OF_WEEK);
}
public static void main(String[] args) throws Exception {
System.out.println(DateTool.dateToString(DateTool.getComputeDate(new Date(),-1)));
String time="14:00-15:00";
System.out.println(time.substring(0, 2));
System.out.println(getComputeMonth(1));
String value="qazwsx123";
System.out.println(value.substring(0, 4));
/*Date date1=DateTool.stringToDateTime("2014-08-19 10:20");
Date date2=DateTool.stringToDateTime("2014-08-20 10:51");
System.out.println(DateTool.getDistanceMinute(date1, date2));*/
/*Calendar aCalendar=Calendar.getInstance();
aCalendar.set(Calendar.YEAR, 2012);
aCalendar.set(Calendar.MONTH, 2);
aCalendar.set(Calendar.DAY_OF_MONTH, 25);
aCalendar.set(Calendar.HOUR_OF_DAY, 14);
Calendar aCalendar1=Calendar.getInstance();
aCalendar1.set(Calendar.YEAR, 2012);
aCalendar1.set(Calendar.MONTH, 2);
aCalendar1.set(Calendar.DAY_OF_MONTH, 26);
aCalendar1.set(Calendar.HOUR_OF_DAY, 14);*/
//System.out.println(aCalendar.get(Calendar.DAY_OF_MONTH));
//aCalendar.set(Calendar.DAY_OF_MONTH, aCalendar.get(Calendar.DAY_OF_MONTH)+1);
/*Calendar test=Calendar.getInstance();
test.setTime(new Date());
System.out.println(test.get(Calendar.MONTH)+" "+test.get(Calendar.DAY_OF_MONTH));*/
//System.out.println((aCalendar1.getTime().getTime()-aCalendar.getTime().getTime())/60000);
//System.out.println(getDistanceMinute(aCalendar.getTime(),aCalendar1.getTime()));
}
}