Javaは時間の1つのクラスを取ります
16495 ワード
自分の開発過程でよく使われている時間クラスを整理し、ツールクラスとして使っています.
package com.lzb.common;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
*
* :
*
* @author lizhenbin
*
* <p> :( , , / )</p>
*/
public class DateUtil {
public static final Long MILI_SECOND = 1000L;
public static final Long MILI_MINUTE = MILI_SECOND * 60L;
public static final Long MILI_HOUR = MILI_MINUTE * 60L;
public static final Long MILI_DATE = MILI_HOUR * 24L;
public static final Long MILI_MONTH = MILI_DATE * 30L;
public static final Long MILI_YEAR = MILI_MONTH * 365L;
public static DateUtil instance = null;
public DateUtil() {};
public static synchronized DateUtil getInstance() {
if(instance == null)
instance = new DateUtil();
return instance;
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-31 11:11:06</p>
*
* @param date
* @param formatType
* @return
*
* <p> :( , , / )</p>
*/
public String getDateByStr(Date date, String formatType) {
isDateCheck(date, formatType);
DateFormat sdf = new SimpleDateFormat(formatType);
return sdf.format(date);
}
/**
*
* : ,
*
* @author lizhenbin
* <p> :2012-5-9 8:29:53</p>
*
* @param dateStr
* @param formatType
* @return
*
* <p> :( , , / )</p>
*/
public Date createDateByformateStr(String dateStr, String formatType) {
isDateCheck(dateStr, formatType);
DateFormat sdf = new SimpleDateFormat(formatType);
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
*
* : , :(yyyy{?}MM{?}dd{?}HH{?}mm{?}ss)
*
* @author lizhenbin
* <p> :2012-5-10 1:18:06</p>
*
* @param dateStr
* @return
*
* <p> :( , , / )</p>
*/
public Date createDateByformateStr(String dateStr) {
isDateCheck(dateStr);
List<String> symlist = new ArrayList<String>();
for(int i=0; i < dateStr.length(); i++) {
char ch = dateStr.charAt(i);
if(ch < '0' || ch > '9')
symlist.add(String.valueOf(ch));
}
StringBuffer sb = new StringBuffer();
sb.append("yyyy" + symlist.get(0) + "MM" + symlist.get(1) + "dd" + symlist.get(2) +
"HH" + symlist.get(3) + "mm" + symlist.get(4) + "ss");
DateFormat sdf = new SimpleDateFormat(sb.toString());
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:09:44</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getTimeMiliSecond(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.MILLISECOND);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:17:57</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getTimeSecond(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.SECOND);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:18:52</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getTimeMinute(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.MINUTE);
}
/**
*
* : (12 )
*
* @author lizhenbin
* <p> :2012-5-10 12:20:17</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getTime12Hour(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.HOUR);
}
/**
*
* : (24 )
*
* @author lizhenbin
* <p> :2012-5-10 12:23:39</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getTime24Hour(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.HOUR_OF_DAY);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:25:09</p>
*
* @param date
* @return , : 1 , ...7
*
* <p> :( , , / )</p>
*/
public int getTimeDateOfWeek(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
int week = calender.get(Calendar.DAY_OF_WEEK) - 1;
if(week == 0)
week = 7;
return week;
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:26:29</p>
*
* @return
*
* <p> :( , , / )</p>
*/
public int getTimeWeekOfMonth(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.WEEK_OF_MONTH);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:27:42</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int geTimetWeekOfYear(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.WEEK_OF_YEAR);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:29:41</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getTimeDateOfMonth(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.DAY_OF_MONTH);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:30:58</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getTimeDateOfYear(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.DAY_OF_YEAR);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:32:40</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getTimeMonth(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return (calender.get(Calendar.MONTH) + 1);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:33:53</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public int getYear(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender.get(Calendar.YEAR);
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-9 11:50:19</p>
*
* @param basetime
* @param targettime
* @return , :
*
* <p> :( , , / )</p>
*/
public long subTimeMiliSecond(Date basetime, Date targettime) {
isDateCheck(basetime, targettime);
return targettime.getTime() - basetime.getTime();
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:42:09</p>
*
* @param basetime
* @param targettime
* @return
*
* <p> :( , , / )</p>
*/
public long subTimeSecond(Date basetime, Date targettime) {
isDateCheck(basetime, targettime);
return subTimeMiliSecond(basetime, targettime)/MILI_SECOND;
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:46:23</p>
*
* @param basetime
* @param targettime
* @return
*
* <p> :( , , / )</p>
*/
public long subTimeMinute(Date basetime, Date targettime) {
isDateCheck(basetime, targettime);
return subTimeMiliSecond(basetime, targettime)/MILI_MINUTE;
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:47:05</p>
*
* @param basetime
* @param targettime
* @return
*
* <p> :( , , / )</p>
*/
public long subTimeHour(Date basetime, Date targettime) {
isDateCheck(basetime, targettime);
return subTimeMiliSecond(basetime, targettime)/MILI_HOUR;
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:47:50</p>
*
* @param basetime
* @param targettime
* @return
*
* <p> :( , , / )</p>
*/
public long subTimeDate(Date basetime, Date targettime) {
return subTimeMiliSecond(basetime, targettime)/MILI_DATE;
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:50:29</p>
*
* @param basetime
* @param targettime
* @return
*
* <p> :( , , / )</p>
*/
public long subTimeMonth(Date basetime, Date targettime) {
isDateCheck(basetime, targettime);
return subTimeMiliSecond(basetime, targettime)/MILI_MONTH;
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 12:51:17</p>
*
* @param basetime
* @param targettime
* @return
*
* <p> :( , , / )</p>
*/
public long subTimeYear(Date basetime, Date targettime) {
isDateCheck(basetime, targettime);
return subTimeMiliSecond(basetime, targettime)/MILI_YEAR;
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 10:57:18</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public Date getNextDate(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
calender.add(calender.DATE, 1);
return calender.getTime();
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-31 11:09:02</p>
*
* @param date
* @param discount
* @return
*
* <p> :( , , / )</p>
*/
public Date getNextTime(Date date, int discount) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
calender.add(calender.DATE, discount);
return calender.getTime();
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-31 11:25:13</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
public Date getBeforeDate(Date date) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
calender.add(calender.DATE, -1);
return calender.getTime();
}
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-31 11:09:27</p>
*
* @param date
* @param discount
* @return
*
* <p> :( , , / )</p>
*/
public Date getBeforeTime(Date date, int discount) {
isDateCheck(date);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
calender.add(calender.DATE, -discount);
return calender.getTime();
}
// public static void main(String[] args) {
//
// DateUtil d = DateUtil.getInstance();
// System.out.println(d.getBeforeDate(new Date()));
// }
/**
*
* :
*
* @author lizhenbin
* <p> :2012-5-10 1:22:29</p>
*
* @param date
* @return
*
* <p> :( , , / )</p>
*/
private boolean isDateCheck(Date date) {
if(date==null) {
System.out.println("Usage: Input Date is Null.");
return false;
}
return true;
}
private boolean isDateCheck(String str) {
if(str==null) {
System.out.println("Usage: Input Str is Null.");
return false;
}
return true;
}
private boolean isDateCheck(Date base, Date tagert) {
if(base==null) {
System.out.println("Usage: Input Base Date is Null.");
return false;
}
if(tagert==null) {
System.out.println("Usage: Input Tagert Date is Null.");
return false;
}
return true;
}
private boolean isDateCheck(String base, String tagert) {
if(base==null) {
System.out.println("Usage: Input Base Str is Null.");
return false;
}
if(tagert==null) {
System.out.println("Usage: Input Tagert Tagert is Null.");
return false;
}
return true;
}
private boolean isDateCheck(Date date, String type) {
if(date==null) {
System.out.println("Usage: Input Date is Null.");
return false;
}
if(type==null || type.equals("")) {
System.out.println("Usage: Input Str is Null.");
return false;
}
return true;
}
}