一個用來獲得日期信息的實用類

          /*
           *@author 我為J狂 建立日期 2007-4-20
           *
           
          */

          package net.blogjava.lzqdiy;

          import java.util.Calendar;
          import java.util.GregorianCalendar;

          public class DateInf
          {
              Calendar now 
          = new GregorianCalendar();

              String year;

              String month;

              String date;

              
          // 得到當前日期
              public DateInf()
              
          {
                  year 
          = String.valueOf(now.get(Calendar.YEAR));
                  
          if ((now.get(Calendar.MONTH) + 1<= 9)
                      month 
          = String.valueOf("0" + (now.get(Calendar.MONTH) + 1));
                  
          else
                      month 
          = String.valueOf(now.get(Calendar.MONTH) + 1);
                  
          if (now.get(Calendar.DATE) <= 9)
                      date 
          = String.valueOf("0" + now.get(Calendar.DATE));
                  
          else
                      date 
          = String.valueOf(now.get(Calendar.DATE));
              }


              
          // 得到n天后的日期
              public DateInf(int n)
              
          {
                  java.util.Date myDate 
          = new java.util.Date();

                  
          long myTime = (myDate.getTime() / 1000+ n * 60 * 60 * 24;
                  myDate.setTime(myTime 
          * 1000);
                  now.setTime(myDate);
                  year 
          = String.valueOf(now.get(Calendar.YEAR));
                  
          if ((now.get(Calendar.MONTH) + 1<= 9)
                      month 
          = String.valueOf("0" + (now.get(Calendar.MONTH) + 1));
                  
          else
                      month 
          = String.valueOf(now.get(Calendar.MONTH) + 1);
                  
          if (now.get(Calendar.DATE) <= 9)
                      date 
          = String.valueOf("0" + now.get(Calendar.DATE));
                  
          else
                      date 
          = String.valueOf(now.get(Calendar.DATE));
              }


              String getYear()
              
          {
                  
          return year;
              }


              String getMonth()
              
          {
                  
          return month;
              }


              String getDate()
              
          {
                  
          return date;
              }


              String getDateInf()
              
          {
                  
          return year + "." + month + "." + date;// 例如:"2006.05.11"
              }

              
          public static void main(String[] args)
              
          {
                  DateInf now
          =new DateInf();
                  
          //輸出當前的日期信息
                  System.out.println("今天是"+now.getYear()+""+now.getMonth()+""+now.getDate()+"日("+now.getDateInf()+")");
                  
          //輸出三天后的日期信息
                  DateInf date=new DateInf(3);
                  System.out.println(
          "三天后是"+date.getYear()+""+date.getMonth()+""+date.getDate()+"日("+date.getDateInf()+")");
              }

          }



          posted on 2007-04-20 17:05 我為J狂 閱讀(849) 評論(1)  編輯  收藏 所屬分類: Java算法

          評論

          # re: 一個用來獲得日期信息的實用類 2007-04-20 20:49 久城

          題外話,今天上課時,感覺Excel的日期計算挺不錯。而且,在我身邊用JAVA連接Excel表的應用也有很多。可以研究下...  回復  更多評論   

          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阿巴嘎旗| 隆昌县| 博爱县| 巩留县| 靖安县| 贵南县| 左云县| 鄂尔多斯市| 静乐县| 元氏县| 鲜城| 宁强县| 梅州市| 阿坝县| 柘荣县| 宿迁市| 新宾| 临清市| 安徽省| 临泽县| 东台市| 榆林市| 临城县| 平湖市| 德阳市| 通渭县| 鄂托克旗| 赫章县| 林芝县| 宁晋县| 馆陶县| 鱼台县| 商南县| 玉溪市| 新宾| 金阳县| 韶关市| 新乡县| 新巴尔虎右旗| 龙南县| 平定县|