時間日期類

package com.mw.platform.util;import java.io.Serializable;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;//時間類(參考了.net 的接口)//// demo:: int date = Datetime.Now().addDay(-3).getDate();//public class Datetime implements Serializable {   private Date _datetime;   private Calendar _calendar = null;   public Datetime(){      setFulltime(new Date());   }   public Datetime(Date date) {      setFulltime(date);   }   public Datetime(long milliseconds) {      setFulltime(new Date(milliseconds));   }   public Datetime setFulltime(Date date) {      _datetime = date;      _calendar = Calendar.getInstance();      _calendar.setTime(date);      return this;   }/* public Datetime setFulltime1(Date date){      _datetime = date;      _calendar = Calendar.getInstance();      _calendar.setTime(date);      return this;   }*/   public Date getFullTime() {      return _datetime;   }   // 當前時間   public static Datetime Now() {      return new Datetime(new Date());   }   // 添加年   public Datetime addYear(int year) {      return doAdd(Calendar.YEAR, +year);   }   // 添加月   public Datetime addMonth(int month) {      return doAdd(Calendar.MONTH, +month);   }   // 添加日   public Datetime addDay(int day) {      return doAdd(Calendar.DAY_OF_MONTH, +day);   }   // 添加小時   public Datetime addHour(int hour) {      return doAdd(Calendar.HOUR_OF_DAY, +hour);   }   // 添加分鐘   public Datetime addMinute(int minute) {      return doAdd(Calendar.MINUTE, +minute);   }   // 添加秒   public Datetime addSecond(int minute) {      return doAdd(Calendar.SECOND, +minute);   }   private Datetime doAdd(int field, int amount) {      _calendar.add(field, +amount);      _datetime = _calendar.getTime();      return this;   }   // 獲取當前年份   public int getYear() {      return _calendar.get(Calendar.YEAR);   }   // 獲取當前月份   public int getMonth() {      return _calendar.get(Calendar.MONTH);   }   // 獲取當前日份   public int getDays() {      return _calendar.get(Calendar.DAY_OF_MONTH);   }   // 獲取當前小時   public int getHours() {      return _calendar.get(Calendar.HOUR_OF_DAY);   }   // 獲取當前分鐘   public int getMinutes() {      return _calendar.get(Calendar.MINUTE);   }   // 獲取當前秒數   public int getSeconds() {      return _calendar.get(Calendar.SECOND);   }   // 獲取當前豪秒   public long getMilliseconds() {      return _calendar.get(Calendar.MILLISECOND);   }   // 獲取總天數(相對於:1970.01.01 00:00:00 GMT)   public long getAllDays() {      return getAllHours() / 24;   }   // 獲取總小時(相對於:1970.01.01 00:00:00 GMT)   public long getAllHours() {      return getAllMinutes() / 60;   }   // 獲取總分鐘(相對於:1970.01.01 00:00:00 GMT)   public long getAllMinutes() {      return getAllSeconds() / 60;   }   // 獲取總秒(相對於:1970.01.01 00:00:00 GMT)   public long getAllSeconds() {      return getTicks() / 1000;   }   // 獲取總毫秒(相對於:1970.01.01 00:00:00 GMT)   public long getAllMilliseconds() {      return getTicks();   }   // 獲取計時週期數(相對於:1970.01.01 00:00:00 GMT)   public long getTicks() {      return _datetime.getTime();   }   // 獲取日期數字(yyyyMMdd)   public int getDate() {      return Integer.parseInt(toString("yyyyMMdd"));   }   // 轉成String   public String toString(String format) {      DateFormat df = new SimpleDateFormat(format);      return df.format(_datetime);   }/*public String toString1(String format){      DateFormat df1= new SimpleDateFormat(format);      return df1.format(-_datetime);}*/   @Override   public String toString() {      return toString("yyyy-MM-dd HH:mm:ss");   }   /**    * @return 獲取指定日期的本月第一天    */   public Datetime getMonthFirstDay() {      _calendar.add(Calendar.MONTH, 0);      _calendar.set(Calendar.DAY_OF_MONTH, 1);      Datetime datetime = new Datetime(_calendar.getTime());      return datetime;   }   /*public Datetime getMonthFirstDay1(){      _calendar.add(Calendar.MONTH,0);      _calendar.set(Calendar.DAY_OF_MONTH,1);      Datetime datetime = new Datetime(_calendar.getTime());      return datetime;   }*/   /**    * @return 獲取指定日期的本月最後一天    */   public Datetime getMonthLastDay() {      _calendar.set(Calendar.DAY_OF_MONTH,            _calendar.getActualMaximum(Calendar.DAY_OF_MONTH));      Datetime datetime = new Datetime(_calendar.getTime());      return datetime;   }   /**    * @return 獲取指定日期的週一日期    */   public Datetime getTimesWeekmorning() {      _calendar.set(_calendar.get(Calendar.YEAR), _calendar.get(Calendar.MONDAY),            _calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);      _calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);      Datetime datetime = new Datetime(_calendar.getTime());      return datetime;   }      /**    * @return 獲取指定日期的週日日期    */   public Datetime getTimesWeeknight() {      _calendar.setTime(getTimesWeekmorning().getFullTime());      _calendar.add(Calendar.DAY_OF_WEEK, 6);      Datetime datetime = new Datetime(_calendar.getTime());      return datetime;   }   // ===================   //   public static Datetime parse(String datetime, String format)         throws ParseException {      DateFormat df = new SimpleDateFormat(format);      Date date = df.parse(datetime);      return new Datetime(date);   }   public static Datetime tryParse(String datetime, String format) {      DateFormat df = new SimpleDateFormat(format);      try {         Date date = df.parse(datetime);         return new Datetime(date);      } catch (Exception ex) {         return null;      }   }}
相關文章
相關標籤/搜索