天天看點

日期

package com.hefeng.test;   

import java.text.dateformat;   

import java.text.parseposition;   

import java.text.simpledateformat;   

import java.util.calendar;   

import java.util.date;   

import java.util.gregoriancalendar;   

public class timetest {   

    //用來全局控制 上一周,本周,下一周的周數變化   

    private  int weeks = 0;   

    private int maxdate;//一月最大天數   

    private int maxyear;//一年最大天數   

    /**  

     * @param args  

     */  

    public static void main(string[] args) {   

        timetest tt = new timetest();   

        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("擷取兩個日期之間間隔天數2008-12-1~2008-9.29:"+timetest.gettwoday("2008-12-1","2008-9-29"));   

    }   

        * 得到二個日期間的間隔天數  

        */  

    public static string gettwoday(string sj1, string sj2) {   

        simpledateformat myformatter = new simpledateformat("yyyy-mm-dd");   

        long day = 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 "";   

        }   

        return day + "";   

        * 根據一個日期,傳回是星期幾的字元串  

        *   

        * @param sdate  

        * @return  

    public static string 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=星期六,其他類推   

        return new simpledateformat("eeee").format(c.gettime());   

        * 将短時間格式字元串轉換為時間 yyyy-mm-dd   

        * @param strdate  

    public static date strtodate(string strdate) {   

        simpledateformat formatter = new simpledateformat("yyyy-mm-dd");   

        parseposition pos = new parseposition(0);   

        date strtodate = formatter.parse(strdate, pos);   

        return strtodate;   

        * 兩個時間之間的天數  

        * @param date1  

        * @param date2  

    public static long getdays(string date1, string date2) {   

        if (date1 == null || date1.equals(""))   

         return 0;   

        if (date2 == null || date2.equals(""))   

        // 轉換為标準時間   

        java.util.date date = null;   

        java.util.date mydate = null;   

         date = myformatter.parse(date1);   

         mydate = myformatter.parse(date2);   

        long day = (date.gettime() - mydate.gettime()) / (24 * 60 * 60 * 1000);   

        return day;   

    // 計算當月最後一天,傳回字元串   

    public string getdefaultday(){     

       string str = "";   

       simpledateformat sdf=new simpledateformat("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());   

       return str;     

    // 上月第一天   

    public string getpreviousmonthfirst(){     

       lastdate.add(calendar.month,-1);//減一個月,變為下月的1号   

       //lastdate.add(calendar.date,-1);//減去一天,變為當月最後一天   

    //擷取當月第一天   

    public string getfirstdayofmonth(){     

    // 獲得本周星期日的日期     

    public string getcurrentweekday() {   

        weeks = 0;   

        int mondayplus = this.getmondayplus();   

        gregoriancalendar currentdate = new gregoriancalendar();   

        currentdate.add(gregoriancalendar.date, mondayplus+6);   

        date monday = currentdate.gettime();   

        dateformat df = dateformat.getdateinstance();   

        string premonday = df.format(monday);   

        return premonday;   

    //擷取當天時間    

    public string getnowtime(string dateformat){   

        date   now   =   new   date();      

        simpledateformat   dateformat   =   new   simpledateformat(dateformat);//可以友善地修改日期格式      

        string  hehe  = dateformat.format(now);      

        return hehe;   

    // 獲得目前日期與本周日相差的天數   

    private int getmondayplus() {   

        calendar cd = calendar.getinstance();   

        // 獲得今天是一周的第幾天,星期日是第一天,星期二是第二天......   

        int dayofweek = cd.get(calendar.day_of_week)-1;         //因為按中國禮拜一作為第一天是以這裡減1   

        if (dayofweek == 1) {   

            return 0;   

        } else {   

            return 1 - dayofweek;   

    //獲得本周一的日期   

    public string getmondayofweek(){   

         weeks = 0;   

         int mondayplus = this.getmondayplus();   

         gregoriancalendar currentdate = new gregoriancalendar();   

         currentdate.add(gregoriancalendar.date, mondayplus);   

         date monday = currentdate.gettime();   

         dateformat df = dateformat.getdateinstance();   

         string premonday = df.format(monday);   

         return premonday;   

  //獲得相應周的周六的日期   

    public string getsaturday() {   

        currentdate.add(gregoriancalendar.date, mondayplus + 7 * weeks + 6);   

 // 獲得上周星期日的日期   

    public string getpreviousweeksunday() {   

        weeks=0;   

        weeks--;   

        currentdate.add(gregoriancalendar.date, mondayplus+weeks);   

 // 獲得上周星期一的日期   

    public string getpreviousweekday() {   

        currentdate.add(gregoriancalendar.date, mondayplus + 7 * weeks);   

    // 獲得下周星期一的日期   

    public string getnextmonday() {   

        weeks++;   

        currentdate.add(gregoriancalendar.date, mondayplus + 7);   

 // 獲得下周星期日的日期   

    public string getnextsunday() {   

        currentdate.add(gregoriancalendar.date, mondayplus + 7+6);   

    private int getmonthplus(){   

        int monthofnumber = cd.get(calendar.day_of_month);   

        cd.set(calendar.date, 1);//把日期設定為當月第一天    

        cd.roll(calendar.date, -1);//日期復原一天,也就是最後一天    

        maxdate=cd.get(calendar.date);    

        if(monthofnumber == 1){   

            return -maxdate;   

        }else{   

            return 1-monthofnumber;   

  //獲得上月最後一天的日期   

    public string getpreviousmonthend(){   

        string str = "";   

      lastdate.add(calendar.month,-1);//減一個月   

      lastdate.set(calendar.date, 1);//把日期設定為當月第一天    

      lastdate.roll(calendar.date, -1);//日期復原一天,也就是本月最後一天    

  //獲得下個月第一天的日期   

    public string getnextmonthfirst(){   

      lastdate.add(calendar.month,1);//減一個月   

  //獲得下個月最後一天的日期   

    public string getnextmonthend(){   

      lastdate.add(calendar.month,1);//加一個月   

    //獲得明年最後一天的日期   

    public string getnextyearend(){   

      calendar lastdate = calendar.getinstance();   

      lastdate.add(calendar.year,1);//加一個年   

      lastdate.set(calendar.day_of_year, 1);   

     lastdate.roll(calendar.day_of_year, -1);   

  //獲得明年第一天的日期   

    public string getnextyearfirst(){   

           str=sdf.format(lastdate.gettime());   

      return str;     

  //獲得本年有多少天   

    private int getmaxyear(){   

        cd.set(calendar.day_of_year,1);//把日期設為當年第一天   

        cd.roll(calendar.day_of_year,-1);//把日期復原一天。   

        int maxyear = cd.get(calendar.day_of_year);    

        return maxyear;   

    private int getyearplus(){   

        int yearofnumber = cd.get(calendar.day_of_year);//獲得當天是一年中的第幾天   

        int maxyear = cd.get(calendar.day_of_year);   

        if(yearofnumber == 1){   

            return -maxyear;   

            return 1-yearofnumber;   

  //獲得本年第一天的日期   

    public string getcurrentyearfirst(){   

        int yearplus = this.getyearplus();   

        currentdate.add(gregoriancalendar.date,yearplus);   

        date yearday = currentdate.gettime();   

        string preyearday = df.format(yearday);   

        return preyearday;   

  //獲得本年最後一天的日期 *   

    public string getcurrentyearend(){   

        date date = new date();   

        simpledateformat   dateformat   =   new   simpledateformat("yyyy");//可以友善地修改日期格式      

        string  years  = dateformat.format(date);      

        return years+"-12-31";   

  //獲得上年第一天的日期 *   

    public string getpreviousyearfirst(){   

        string  years  = dateformat.format(date); int years_value = integer.parseint(years);     

        years_value--;   

        return years_value+"-1-1";   

  //獲得上年最後一天的日期   

    public string getpreviousyearend(){   

        currentdate.add(gregoriancalendar.date,yearplus+maxyear*weeks+(maxyear-1));   

        getthisseasontime(11);   

  //獲得本季度   

    public string getthisseasontime(int month){   

        int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};   

        int season = 1;   

        if(month>=1&&month<=3){   

            season = 1;   

        if(month>=4&&month<=6){   

            season = 2;   

        if(month>=7&&month<=9){   

            season = 3;   

        if(month>=10&&month<=12){   

            season = 4;   

        int start_month = array[season-1][0];   

        int end_month = array[season-1][2];   

        int years_value = integer.parseint(years);   

        int start_days =1;//years+"-"+string.valueof(start_month)+"-1";//getlastdayofmonth(years_value,start_month);   

        int end_days = getlastdayofmonth(years_value,end_month);   

        string seasondate = years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;   

        return seasondate;   

     * 擷取某年某月的最後一天  

     * @param year 年  

     * @param month 月  

     * @return 最後一天  

   private int getlastdayofmonth(int year, int month) {   

         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8  

                   || month == 10 || month == 12) {   

               return 31;   

         }   

         if (month == 4 || month == 6 || month == 9 || month == 11) {   

               return 30;   

         if (month == 2) {   

               if (isleapyear(year)) {   

                   return 29;   

               } else {   

                   return 28;   

               }   

   }   

   /**  

    * 是否閏年  

    * @param year 年  

    * @return   

    */  

  public boolean isleapyear(int year) {   

        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);   

  }   

繼續閱讀