java日期處理通用類

來源:互聯網
上載者:User

 /**
 * <p>通用日期處理類</p>
 * <p>Description:通用日期處理函數</p>
 * <p>Copyright: Copyright (c) 2009-2099</p>
 * <p>Company: fmcc,newland</p>
 * @author huang yongjin
 */
package mydate;
import java.io.*;
import java.util.Date;
import java.util.Calendar;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;


public class MyDate {
 private int year;
 private int month;
 private int day;
 private int hour;
 private int minute;
 private int second;
 
 
 private static final int[] dayArray = new int[]  //每月的天數
         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
 /**
   * 標準日期格式 MM/dd/yyyy
   */
  public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
   "MM/dd/yyyy" );
  /**
   * 標準時間格式 MM/dd/yyyy HH:mm
   */
  public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat(
   "MM/dd/yyyy HH:mm" );
  /**
   * 帶時分秒的標準時間格式 MM/dd/yyyy HH:mm:ss
   */
  public static final SimpleDateFormat DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat(
   "MM/dd/yyyy HH:mm:ss" );
  /**
   * ORA標準日期格式 yyyyMMdd
   */
  public static final SimpleDateFormat ORA_DATE_FORMAT = new SimpleDateFormat(
   "yyyyMMdd" );
  /**
   * ORA標準時間格式 yyyyMMddHHmm
   */
  public static final SimpleDateFormat ORA_DATE_TIME_FORMAT = new SimpleDateFormat(
   "yyyyMMddHHmm" );
  /**
   * 帶時分秒的ORA標準時間格式 yyyyMMddHHmmss
   */
  public static final SimpleDateFormat ORA_DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat(
   "yyyyMMddHHmmss" );
  /**
   * yyyy-MM-dd
   */
  public static final SimpleDateFormat CHN_DATE_FORMAT = new SimpleDateFormat(
   "yyyy-MM-dd" );
  /**
   * yyyy-MM-dd HH:mm
   */
  public static final SimpleDateFormat CHN_DATE_TIME_FORMAT = new SimpleDateFormat(
   "yyyy-MM-dd HH:mm" );
  /**
   * yyyy-MM-dd HH:mm:ss
   */
  public static final SimpleDateFormat CHN_DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat(
   "yyyy-MM-dd HH:mm:ss" );
 
 /**
  * 無參建構函式,預設的日期為系統時間
  */
 MyDate(){
  today();
 }
 
 /**
  * 有參建構函式
  * @param inValue  14位日期格式的字串,不滿14位的,後面補0處理
  */
 MyDate(String inValue){
  SetDate(inValue);
 }
 
 /**
  * 有參建構函式
  * @param mills 毫秒數
  */
 MyDate(long mills){
  setTimeInMillis(mills);
 }
 
 /**
  * 有參建構函式
  * @param year  年
  * @param month 月
  * @param day   日
  * @param hour  時
  * @param minute  分
  * @param second  秒
  */
 MyDate(int year,int month,int day,int hour,int minute,int second){
    Calendar calendar=Calendar.getInstance();
        calendar.set(year, month-1, day, hour, minute, second);
        this.year  = calendar.get(Calendar.YEAR);
        this.month  = calendar.get(Calendar.MONTH)+1;
        this.day  = calendar.get(Calendar.DAY_OF_MONTH);
        this.hour  = calendar.get(Calendar.HOUR_OF_DAY);
        this.minute  = calendar.get(Calendar.MINUTE);
        this.second  = calendar.get(Calendar.SECOND);
  
 }
 
 /**
  * 設定日期函數
  * @param inValue
  */
 public void SetDate(String inValue){
  if(inValue.length()!=14){//不夠14位日期格式的,後面都補0處理
   for(int i=inValue.length();i<14;i++)
   {
    inValue = inValue + "0";
   }
   System.out.println(inValue);
  }
  try{
  
   int year = Integer.parseInt(inValue.substring(0, 4));
   int month = Integer.parseInt(inValue.substring(4, 6));
   int day = Integer.parseInt(inValue.substring(6, 8));
   int hour = Integer.parseInt(inValue.substring(8, 10));
   int minute = Integer.parseInt(inValue.substring(10, 12));
   int second = Integer.parseInt(inValue.substring(12));
   
   Calendar calendar=Calendar.getInstance();
      calendar.set(year, month-1, day, hour, minute, second);
      this.year  = calendar.get(Calendar.YEAR);
      this.month  = calendar.get(Calendar.MONTH)+1;
      this.day  = calendar.get(Calendar.DAY_OF_MONTH);
      this.hour  = calendar.get(Calendar.HOUR_OF_DAY);
      this.minute  = calendar.get(Calendar.MINUTE);
      this.second  = calendar.get(Calendar.SECOND);
        
    
  
  }catch(Exception e){
   System.out.println(e.getMessage());
  }
 }
 
    /**
     * 取得目前時間
     */
 private void today(){
  Calendar calendar=Calendar.getInstance();
  this.year  = calendar.get(Calendar.YEAR);
  this.month  = calendar.get(Calendar.MONTH)+1;
  this.day  = calendar.get(Calendar.DAY_OF_MONTH);
  this.hour  = calendar.get(Calendar.HOUR_OF_DAY);
  this.minute  = calendar.get(Calendar.MINUTE);
  this.second  = calendar.get(Calendar.SECOND);
 }
   
 
 
 /**
  * 格式化日期
  * @param SimpleDateFormat df
  * @return
  */
 public String format(SimpleDateFormat df){
  //Date date = new Date(year-1900,month-1,day,hour,minute,second);
  //return  df.format(date);
  
  Calendar calendar=Calendar.getInstance();
        calendar.set(this.year, this.month-1, this.day, this.hour, this.minute, this.second);
        return df.format(calendar.getTime());
 }
 
 /**
  * 格式化日期,不推薦在外面直接調用這個方法
  * 直接調用也可以,只是要保證strFormat的格式要正確
  * @param strFormat
  * @return
  */
 public String format2(String strFormat){
  /*Date date = new Date(year-1900,month-1,day,hour,minute,second);
  SimpleDateFormat df = new SimpleDateFormat(strFormat);
  return  df.format(date);*/
  
  Calendar calendar=Calendar.getInstance();
  SimpleDateFormat df = new SimpleDateFormat(strFormat);
        calendar.set(this.year, this.month-1, this.day, this.hour, this.minute, this.second);
        return df.format(calendar.getTime());
 }
 
 /**
  * toString方法 用yyyy-MM-dd HH:mm:ss格式
  */
 public String toString(){
  return this.format(this.CHN_DATE_TIME_EXTENDED_FORMAT);
 }
 
 public int getDay() {
  return day;
 }
 public void setDay(int day) {
  this.day = day;
 }
 public int getHour() {
  return hour;
 }
 public void setHour(int hour) {
  this.hour = hour;
 }
 public int getMinute() {
  return minute;
 }
 public void setMinute(int minute) {
  this.minute = minute;
 }
 public int getMonth() {
  return month;
 }
 public void setMonth(int month) {
  this.month = month;
 }
 public int getSecond() {
  return second;
 }
 public void setSecond(int second) {
  this.second = second;
 }
 public int getYear() {
  return year;
 }
 public void setYear(int year) {
  this.year = year;
 }
 
 /**
  *得到毫秒數 長整型
  */
 public long getTimeInMillis(){
  Calendar calendar=Calendar.getInstance();
  calendar.set(this.year, this.month-1, this.day, this.hour, this.minute, this.second);
    return calendar.getTime().getTime();
 }
 
 /**
  * 根據毫秒數來設定日期
  * @param mills  毫秒數 長整型
  */
 public void setTimeInMillis(long mills){
  Date    dd    =    new   Date(mills);
  Calendar calendar=Calendar.getInstance();
  calendar.setTime(dd);
  this.year  = calendar.get(Calendar.YEAR);
  this.month  = calendar.get(Calendar.MONTH)+1;
  this.day  = calendar.get(Calendar.DAY_OF_MONTH);
  this.hour  = calendar.get(Calendar.HOUR_OF_DAY);
  this.minute  = calendar.get(Calendar.MINUTE);
  this.second  = calendar.get(Calendar.SECOND);
  
 }
 
 /**
  * 判斷當前年是否是閏年
  * @return
  */
 public boolean isLeapYear(){
  return this.isLeapYear(year);
 }
 
 /**
  * 判斷是否是閏年
  * @param year 年
  * @return
  */
 public boolean isLeapYear(int year){
  if(( year % 400 ) == 0)
      return true;
  else if(( year % 4 ) == 0)
       {
         if(( year % 100 ) == 0)
           return false;
         else return true;
       }
       else return false;
 }
 
 /**
  * 日期增減操作
  * @param years  年數
  * @param months 月數
  * @param days   日數
  * @param hours  小時數
  * @param minutes 分數
  * @param seconds 秒數
  */
 public void _add(int years,int months,int days,int hours,int minutes,int seconds){
  Calendar calendar=Calendar.getInstance();
  calendar.set(this.year+years, this.month-1+months, this.day+days, this.hour+hours, this.minute+minutes, this.second+seconds);
  setTimeInMillis(calendar.getTime().getTime());
 }
 
 /**
  * 年增減操作
  * @param years 年數
  */
 public void addYear(int years){
  if(month == 2 && day == 29)//目前時間是閏年並且日期是2月29號
  {
   if(this.isLeapYear(year + years) == true)//目標年是閏年時
     this._add(years, 0, 0, 0, 0, 0);
   else this._add(years, 0, -1, 0, 0, 0);//目標年不是閏年,則2月只有28天,天數減去1
  }
  else
  this._add(years, 0, 0, 0, 0, 0);
 }
 
 /**
  * 月增減操作(與oracle對月份增減的操作一樣)
  * @param months 月數
  */
 public void addMonth(int months){
  int this_day_end = daysOfMonth();//本月的天數
  int that_day_end = getDayOfMonth(months);//離現在n個月的天數
  if(this.day == this_day_end) this.day = that_day_end;//如果現在是本月的最後一天,則day設定成that_day_end
  else if( this.day > that_day_end) this.day = that_day_end; //如果離現在n個月的天數that_day_end比現在的day小,則現在的day設定成that_day_end
   
  this._add(0, months, 0, 0, 0, 0);
 }
 
 /**
  * 天數增減操作
  * @param days  天數
  */
 public void addDay(int days){
  this._add(0, 0, days, 0, 0, 0);
 }
 
 /**
  * 小時增減操作
  * @param hours  小時數
  */
 public void addHour(int hours){
  this._add(0, 0, 0, hours, 0, 0);
 }
 
 /**
  * 分鐘增減操作
  * @param minutes  分鐘數
  */
 public void addMinute(int minutes){
  this._add(0, 0, 0, 0, minutes, 0);
 }
 
 /**
  * 秒增減操作
  * @param seconds 秒數
  */
 public void addSecond(int seconds){
  this._add(0, 0, 0, 0, 0, seconds);
 }
 
 /**
  * 取本月中有多少天
  * @param month 月份
  * @return 本月的實際天數
  */
 public int daysOfMonth(){
  if(month>12 || month<0) return 0;
  if ( month == 2 && this.isLeapYear() )
   return 29;
  else
   return dayArray[month - 1];
 }
 
 /**
  * 擷取離目前時間ms個月的月份的天數
  * @param ms 月份
  * @return
  */
 public int getDayOfMonth(int ms){
  int yy = ms/12;
  int mm = ms%12;
  int year = this.year + yy;
  int month = this.month + mm;

  if(month>12)
  {
   month = month-12;
   year = year +1;
  }
  if(month<1)
  {
   month = month +12;
   year = year -1;
  }

  if ( month == 2 && isLeapYear(year))
   return 29;
  else
   return dayArray[month - 1];
 }
 
 /**
  * 得到兩個日期之間的秒數,靜態方法
  * 調用方法:MyDate.diffSec(mydate1, mydate2)
  * 要得到兩個日期之間之間的天數,則再除以(3600*24)即可
  * @param mydate1
  * @param mydate2
  * @return
  */
 public static long diffSec(MyDate mydate1,MyDate mydate2){
  return (mydate1.getTimeInMillis()-mydate2.getTimeInMillis())/1000;
 }
 
/**********
 * Java中的時間操作不外乎這四種情況:
 * 1、擷取目前時間
 * 2、擷取某個時間的某種格式
 * 3、設定時間
 * 4、時間的運算
 *
 * Java計算時間依靠1970年1月1日開始的毫秒數.
 *
 * Calender類本身是個抽象類別,有部分方法需要是抽象的,所以本身不能通過new的方法來執行個體化,
 * 需要藉助於一些已經把抽象方法實現的子類來執行個體化,執行個體化方法都寫在getInstance()方法裡
 * 當對抽象方法的普遍實現(使用getInstance()來執行個體化)不能滿足你的需求的時候,你就應該自己來實現它。比如:
 * public class CalenderEx extends Calender {
 * }
 * Calender cal = new CalenderEx();
 *
 *
 */
 
 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.