Java獲取各種常用時間方法大全

Java獲取各種常用時間方法大全

package*cc.javaweb.test;   

  • importjava.text.DateFormat;      
  • importjava.text.ParsePosition;      
  • importjava.text.SimpleDateFormat;      
  • importjava.util.Calendar;      
  • importjava.util.Date;      
  • importjava.util.GregorianCalendar;      
  • publicclassTimeTest {      
  • //用來全局控制 上一週,本週,下一週的週數變化     
  • privateintweeks =0;      
  • privateintMaxDate;//一月最大天數     
  • privateintMaxYear;//一年最大天數     
  • /**   
  •      * @param args   
  •      */
  • publicstaticvoidmain(String[] args) {      
  •         TimeTest tt =newTimeTest();      
  •         System.out.println("獲取當天日期:"+tt.getNowTime("yyyy-MM-dd"));      
  •         System.out.println("獲取本週一日期:"+tt.getMondayOFWeek());      
  •         System.out.println("獲取本週日的日期~:"+tt.getCurrentWeekday());      
  •         System.out.println("獲取上週一日期:"+tt.getPreviousWeekday());      
  •         System.out.println("獲取上週日日期:"+tt.getPreviousWeekSunday());      
  •         System.out.println("獲取下週一日期:"+tt.getNextMonday());      
  •         System.out.println("獲取下週日日期:"+tt.getNextSunday());      
  •         System.out.println("獲得相應周的週六的日期:"+tt.getNowTime("yyyy-MM-dd"));      
  •         System.out.println("獲取本月第一天日期:"+tt.getFirstDayOfMonth());      
  •         System.out.println("獲取本月最後一天日期:"+tt.getDefaultDay());      
  •         System.out.println("獲取上月第一天日期:"+tt.getPreviousMonthFirst());      
  •         System.out.println("獲取上月最後一天的日期:"+tt.getPreviousMonthEnd());      
  •         System.out.println("獲取下月第一天日期:"+tt.getNextMonthFirst());      
  •         System.out.println("獲取下月最後一天日期:"+tt.getNextMonthEnd());      
  •         System.out.println("獲取本年的第一天日期:"+tt.getCurrentYearFirst());      
  •         System.out.println("獲取本年最後一天日期:"+tt.getCurrentYearEnd());      
  •         System.out.println("獲取去年的第一天日期:"+tt.getPreviousYearFirst());      
  •         System.out.println("獲取去年的最後一天日期:"+tt.getPreviousYearEnd());      
  •         System.out.println("獲取明年第一天日期:"+tt.getNextYearFirst());      
  •         System.out.println("獲取明年最後一天日期:"+tt.getNextYearEnd());      
  •         System.out.println("獲取本季度第一天到最後一天:"+tt.getThisSeasonTime(11));      
  •         System.out.println("獲取兩個日期之間間隔天數2010-12-31~2010-9-14:"+TimeTest.getTwoDay("2010-12-31","2010-9-14"));      
  •     }      
  • /**   
  •         * 得到二個日期間的間隔天數
  •         */
  • publicstaticString getTwoDay(String sj1, String sj2) {      
  •         SimpleDateFormat myFormatter =newSimpleDateFormat("yyyy-MM-dd");      
  • longday =0;      
  • try{      
  •          java.util.Date date = myFormatter.parse(sj1);      
  •          java.util.Date mydate = myFormatter.parse(sj2);      
  •          day = (date.getTime() - mydate.getTime()) / (24*60*60*1000);      
  •         }catch(Exception e) {      
  • return"";      
  •         }      
  • returnday +"";      
  •     }      
  • /**   
  •         * 根據一個日期,返回是星期幾的字符串   
  •         *     
  •         * @param sdate   
  •         * @return   
  •         */
  • publicstaticString getWeek(String sdate) {      
  • // 再轉換爲時間     
  •         Date date = TimeTest.strToDate(sdate);      
  •         Calendar c = Calendar.getInstance();      
  •         c.setTime(date);      
  • // int hour=c.get(Calendar.DAY_OF_WEEK);     
  • // hour中存的就是星期幾了,其範圍 1~7     
  • // 1=星期日 7=星期六,其他類推     
  • returnnewSimpleDateFormat("EEEE").format(c.getTime());      
  •     }      
  • /**   
  •         * 將短時間格式字符串轉換爲時間 yyyy-MM-dd     
  •         *     
  •         * @param strDate   
  •         * @return   
  •         */
  • publicstaticDate strToDate(String strDate) {      
  •         SimpleDateFormat formatter =newSimpleDateFormat("yyyy-MM-dd");      
  •         ParsePosition pos =newParsePosition(0);      
  •         Date strtodate = formatter.parse(strDate, pos);      
  • returnstrtodate;      
  •     }      
  • /**   
  •         * 兩個時間之間的天數   
  •         *     
  •         * @param date1   
  •         * @param date2   
  •         * @return   
  •         */
  • publicstaticlonggetDays(String date1, String date2) {      
  • if(date1 ==null|| date1.equals(""))      
  • return0;      
  • if(date2 ==null|| date2.equals(""))      
  • return0;      
  • // 轉換爲標準時間     
  •         SimpleDateFormat myFormatter =newSimpleDateFormat("yyyy-MM-dd");      
  •         java.util.Date date =null;      
  •         java.util.Date mydate =null;      
  • try{      
  •          date = myFormatter.parse(date1);      
  •          mydate = myFormatter.parse(date2);      
  •         }catch(Exception e) {      
  •         }      
  • longday = (date.getTime() - mydate.getTime()) / (24*60*60*1000);      
  • returnday;      
  •     }      
  • // 計算當月最後一天,返回字符串     
  • publicString getDefaultDay(){         
  •        String str ="";      
  •        SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd");           
  •        Calendar lastDate = Calendar.getInstance();      
  •        lastDate.set(Calendar.DATE,1);//設爲當前月的1號     
  •        lastDate.add(Calendar.MONTH,1);//加一個月,變爲下月的1號     
  •        lastDate.add(Calendar.DATE,-1);//減去一天,變爲當月最後一天     
  •        str=sdf.format(lastDate.getTime());      
  • returnstr;         
  •     }      
  • // 上月第一天     
  • publicString getPreviousMonthFirst(){         
  •        String str ="";      
  •        SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd");           
  •        Calendar lastDate = Calendar.getInstance();      
  •        lastDate.set(Calendar.DATE,1);//設爲當前月的1號     
  •        lastDate.add(Calendar.MONTH,-1);//減一個月,變爲下月的1號     
  • //lastDate.add(Calendar.DATE,-1);//減去一天,變爲當月最後一天     
  •        str=sdf.format(lastDate.getTime());      
  • returnstr;         
  •     }      
  • //獲取當月第一天     
  • publicString getFirstDayOfMonth(){         
  •        String str ="";      
  •        SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd");           
  •        Calendar lastDate = Calendar.getInstance();      
  •        lastDate.set(Calendar.DATE,1);//設爲當前月的1號     
  •        str=sdf.format(lastDate.getTime());      
  • returnstr;         
  •     }      
  • // 獲得本週星期日的日期      
  • publicString getCurrentWeekday() {      
  •         weeks =0;      
  • intmondayPlus =this.getMondayPlus();      
  •         GregorianCalendar currentDate =newGregorianCalendar();      
  •         currentDate.add(GregorianCalendar.DATE, mondayPlus+6);      
  •         Date monday = currentDate.getTime();      
  •         DateFormat df = DateFormat.getDateInstance();      
  •         String preMonday = df.format(monday);      
  • returnpreMonday;      
  •     }      
  • //獲取當天時間      
  • publicString getNowTime(String dateformat){      
  •         Date   now   =   new   Date();         
  •         SimpleDateFormat   dateFormat   =   new   SimpleDateFormat(dateformat);//可以方便地修改日期格式        
  •         String  hehe  = dateFormat.format(now);         
  • returnhehe;      
  •     }      
  • // 獲得當前日期與本週日相差的天數     
  • privateintgetMondayPlus() {      
  •         Calendar cd = Calendar.getInstance();      
  • // 獲得今天是一週的第幾天,星期日是第一天,星期二是第二天......     
  • intdayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;         //因爲按中國禮拜一作爲第一天所以這裏減1     
  • if(dayOfWeek ==1) {      
  • return0;      
  •         }else{      
  • return1- dayOfWeek;      
  •         }      
  •     }      
  • //獲得本週一的日期     
  • publicString getMondayOFWeek(){      
  •          weeks =0;      
  • intmondayPlus =this.getMondayPlus();      
  •          GregorianCalendar currentDate =newGregorianCalendar();      
  •          currentDate.add(GregorianCalendar.DATE, mondayPlus);      
  •          Date monday = currentDate.getTime();      
  •          DateFormat df = DateFormat.getDateInstance();      
  •          String preMonday = df.format(monday);      
  • returnpreMonday;      
  •     }      
  • //獲得相應周的週六的日期     
  • publicString getSaturday() {      
  • intmondayPlus =this.getMondayPlus();      
  •         GregorianCalendar currentDate =newGregorianCalendar();      
  •         currentDate.add(GregorianCalendar.DATE, mondayPlus +7* weeks +6);      
  •         Date monday = currentDate.getTime();      
  •         DateFormat df = DateFormat.getDateInstance();      
  •         String preMonday = df.format(monday);       return*preMonday;      
    **
  • **    }  
    **
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章