package net.maxt.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* DATE 繼承於 java.util.Date,多實現了很多方法。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 9:52:51 AM
*/
public class Date extends java.util.Date {
/**
*
*/
private static final long serialVersionUID = 2155545266875552658L;
/**
* 功能:轉換為Calendar。
* @author 沙琪瑪 QQ:862990787
* Aug 21, 2013 8:58:31 AM
* @return Calendar
*/
public Calendar toCalendar() {
Calendar c = Calendar.getInstance();
c.setTime(this);
return c;
}
/**
* 功能:判斷日期是否和當前date對象在同一天。
* @author 沙琪瑪 QQ:862990787
* Aug 21, 2013 7:15:53 AM
* @param date 比較的日期
* @return boolean 如果在返回true,否則返回false。
*/
public boolean isSameDay(Date date) {
if (date == null) {
throw new IllegalArgumentException("日期不能為null");
}
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date);
return this.isSameDay(cal2);
}
/**
* 功能:判斷日期是否和當前date對象在同一天。
* @author 沙琪瑪 QQ:862990787
* Aug 21, 2013 7:15:53 AM
* @param cal 比較的日期
* @return boolean 如果在返回true,否則返回false。
*/
public boolean isSameDay(Calendar cal) {
if (cal == null) {
throw new IllegalArgumentException("日期不能為null");
}
//當前date對象的時間
Calendar cal1 = Calendar.getInstance();
cal1.setTime(this);
return (cal1.get(Calendar.ERA) == cal.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal.get(Calendar.DAY_OF_YEAR));
}
/**
* 功能:將當前日期的秒數進行重新設置。
* @author 沙琪瑪 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param second 秒數
* @return 設置后的日期
*/
public Date setSecondNew(int second){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.SECOND,second);
return new Date(c.getTimeInMillis());
}
/**
* 功能:將當前日期的分鍾進行重新設置。
* @author 沙琪瑪 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param minute 分鍾數
* @return 設置后的日期
*/
public Date setMinuteNew(int minute){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MINUTE,minute);
return new Date(c.getTimeInMillis());
}
/**
* 功能:將當前日期的小時進行重新設置。
* @author 沙琪瑪 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param hours 小時數 (24小時制)
* @return 設置后的日期
*/
public Date setHourNew(int hour){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.HOUR_OF_DAY, hour);
return new Date(c.getTimeInMillis());
}
/**
* 功能:將當前日期的天進行重新設置。
* @author 沙琪瑪 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param days 某一天
* @return 設置后的日期
*/
public Date setDayNew(int day){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.DATE,day);
return new Date(c.getTimeInMillis());
}
/**
* 功能:將當前日期的月進行重新設置。
* @author 沙琪瑪 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param months 某一月
* @return 設置后的日期
*/
public Date setMonthNew(int month){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MONTH, month-1);
return new Date(c.getTimeInMillis());
}
/**
* 功能:將當前日期的年進行重新設置。
* @author 沙琪瑪 QQ:862990787
* Jul 31, 2013 2:42:36 PM
* @param years 某一年
* @return 設置后的日期
*/
public Date setYearNew(int year){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.YEAR, year);
return new Date(c.getTimeInMillis());
}
/**
* 功能:得到當月有多少天。
* @author 沙琪瑪 QQ:862990787
* Jul 2, 2013 4:59:41 PM
* @return int
*/
public int daysNumOfMonth(){
Calendar cal = Calendar.getInstance();
cal.setTime(this);
return cal.getActualMaximum(Calendar.DATE);
}
/**
* 將yyyy-MM-dd HH:mm:ss字符串轉換成日期(net.maxt.util.Date)<br/>
* @param dateStr 時間字符串
* @param 當前時間字符串的格式。
* @return net.maxt.util.Date 日期 ,轉換異常時返回null。
*/
public static Date parseDate(String dateStr,SimpleDateFormat dataFormat){
try {
java.util.Date d = dataFormat.parse(dateStr);
return new Date(d.getTime());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 將yyyy-MM-dd HH:mm:ss字符串轉換成日期(net.maxt.util.Date)<br/>
* @param dateStr yyyy-MM-dd HH:mm:ss字符串
* @return net.maxt.util.Date 日期 ,轉換異常時返回null。
*/
public static Date parseDate(String dateStr){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
java.util.Date d = sdf.parse(dateStr);
return new Date(d.getTime());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 功能:計算兩個時間的時間差。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 2:34:08 PM
* @param time 另一個時間。
* @return Timespan 時間間隔
*/
public Timespan substract(Date time){
return new Timespan(this.getTime()-time.getTime());
}
/**
* 功能:當前時間增加毫秒數。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param milliseconds 正值時時間延后,負值時時間提前。
* @return Date
*/
public Date addMilliseconds(int milliseconds){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MILLISECOND, c.get(Calendar.MILLISECOND)+milliseconds);
return new Date(c.getTimeInMillis());
}
/**
* 功能:當前時間增加秒數。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param seconds 正值時時間延后,負值時時間提前。
* @return Date
*/
public Date addSeconds(int seconds){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.SECOND, c.get(Calendar.SECOND)+seconds);
return new Date(c.getTimeInMillis());
}
/**
* 功能:當前時間增加分鍾數。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param minutes 正值時時間延后,負值時時間提前。
* @return Date
*/
public Date addMinutes(int minutes){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE)+minutes);
return new Date(c.getTimeInMillis());
}
/**
* 功能:當前時間增加小時數。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param hours 正值時時間延后,負值時時間提前。
* @return Date
*/
public Date addHours(int hours){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.HOUR, c.get(Calendar.HOUR)+hours);
return new Date(c.getTimeInMillis());
}
/**
* 功能:當前時間增加天數。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param days 正值時時間延后,負值時時間提前。
* @return Date
*/
public Date addDays(int days){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.DATE, c.get(Calendar.DATE)+days);
return new Date(c.getTimeInMillis());
}
/**
* 功能:當前時間增加月數。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param months 正值時時間延后,負值時時間提前。
* @return Date
*/
public Date addMonths(int months){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.MONTH, c.get(Calendar.MONTH)+months);
return new Date(c.getTimeInMillis());
}
/**
* 功能:當前時間增加年數。注意遇到2月29日情況,系統會自動延后或者減少一天。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 11:26:27 AM
* @param years 正值時時間延后,負值時時間提前。
* @return Date
*/
public Date addYears(int years){
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.YEAR, c.get(Calendar.YEAR)+years);
return new Date(c.getTimeInMillis());
}
/**
* 得到秒。格式:56<br/>
* @return int
*/
public int secondInt() {
return Integer.parseInt(toString("ss"));
}
/**
* 得到分鍾。格式:56<br/>
* @return int
*/
public int minuteInt() {
return Integer.parseInt(toString("mm"));
}
/**
* 得到小時。格式:23<br/>
* @return int
*/
public int hourInt() {
return Integer.parseInt(toString("HH"));
}
/**
* 得到日。格式:26<br/>
* 注意:這里1日返回1,2日返回2。
* @return int
*/
public int dayInt() {
return Integer.parseInt(toString("dd"));
}
/**
* 得到月。格式:5<br/>
* 注意:這里1月返回1,2月返回2。
* @return int
*/
public int monthInt() {
return Integer.parseInt(toString("MM"));
}
/**
* 得到年。格式:2013
* @return int
*/
public int yearInt() {
return Integer.parseInt(toString("yyyy"));
}
/**
* 得到短時間。格式:12:01
* @return String
*/
public String shortTime() {
return toString("HH:mm");
}
/**
* 得到長時間。格式:12:01:01
* @return String
*/
public String longTime() {
return toString("HH:mm:ss");
}
/**
* 得到今天的第一秒的時間。
* @return Date
*/
public Date dayStart() {
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return new Date(c.getTimeInMillis());
}
/**
* 得到當前所在自然月的第一天的開始,格式為長日期格式。例如:2012-03-01 00:00:00。
* @return Date
*/
public Date monthStart(){
Calendar c=Calendar.getInstance();
String startStr= toString("yyyy-M-")+c.getActualMinimum(Calendar.DATE)+" 00:00:00";
return Date.parseDate(startStr);
}
/**
* 得到今天的最后一秒的時間。
* @return Date
*/
public Date dayEnd() {
Calendar c = Calendar.getInstance();
c.setTime(this);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return new Date(c.getTimeInMillis());
}
/**
* 根據日期得到星期幾,得到數字。<br/>
* 7, 1, 2, 3, 4, 5, 6
* @return Integer 如:6
*/
public int dayOfWeekInt() {
Integer dayNames[] = { 7, 1, 2, 3, 4, 5, 6 };
Calendar calendar = Calendar.getInstance();
calendar.setTime(this);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0)
dayOfWeek = 0;
return dayNames[dayOfWeek];
}
/**
* 將日期轉換成長日期字符串 例如:2009-09-09 01:01:01
* @return String
*/
public String toLongDate() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return (null == this) ? null : df.format(this);
}
/**
* 將日期按照一定的格式進行格式化為字符串。<br/>
* 例如想將時間格式化為2012-03-05 12:56 ,則只需要傳入formate為yyyy-MM-dd HH:mm即可。
* @param formate 格式化格式,如:yyyy-MM-dd HH:mm
* @return String 格式后的日期字符串。如果當前對象為null,則直接返回null。
*/
public String toString(String formate) {
DateFormat df = new SimpleDateFormat(formate);
return (null == this) ? null : df.format(this);
}
/**
* 得到某個時間的時間戳yyyyMMddHHmmss。
* @param date 時間
* @return String 如果當前對象為null,則直接返回null。
*/
public String toTimeStamp() {
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
return (null == this) ? null : df.format(this);
}
/**
* 將日期轉換成短日期字符串,例如:2009-09-09。
* @return String ,如果當前對象為null,返回null。
*/
public String toShortDate() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return (null == this) ? null : df.format(this);
}
/**
* 功能:用java.util.Date進行構造。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 10:59:05 AM
* @param java.util.Date date
*/
public Date(java.util.Date date) {
super(date.getTime());
}
/**
* 功能:用毫秒進行構造。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 10:59:05 AM
* @param timeInMillis
*/
public Date(long timeInMillis) {
super(timeInMillis);
}
/**
* 功能:默認構造函數。
* @author 沙琪瑪 QQ:862990787
* May 29, 2013 11:00:05 AM
*/
public Date() {
super();
}
}