熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

GregorianCalendar類的應用——當前月日歷打印

2022-06-13   來源: Java核心技術 

   import javatextDateFormatSymbols;

   import javautilCalendar;

   import javautilGregorianCalendar;

   import javautilLocale;

  

   public class CalenderTest {

  

       /**

        * 本程序是打印一個當前月歷的示例

        *

        * @Version //

        * @author Eduardo

        */

       public static void main(String[] args) {

           //將本地區設置為美國地區為了輸出英文星期格式

           LocalesetDefault(LocaleUS);

           //構造一個日歷對象

           GregorianCalendar d = new GregorianCalendar();

  

           int today =dget(CalendarDAY_OF_MONTH);//today為獲取當前的日

           int month=dget(CalendarMONTH);//month為獲取當前的月

           //將d設置為本月

           dset(CalendarDAY_OF_MONTH);

  

           int weekday=dget(CalendarDAY_OF_WEEK);//weekday為獲取本月號為星期幾

           //firstDayOfWeek為獲取本地區的起始日

           int firstDayOfWeek=dgetFirstDayOfWeek();

  

           /*進行縮進將日歷對象的日減一知道一個星期的第一天位置*/

           int indent=;//記錄縮進幾次

           while(weekday!=firstDayOfWeek)

           {

               indent++;

               dadd(CalendarDAY_OF_MONTH );//當前日期提前一天

               weekday=dget(CalendarDAY_OF_WEEK);//獲取當前日期為星期幾

           }

  

           /*打印星期縮寫字符串*/

           String[] weekdayNames=new DateFormatSymbols()getShortWeekdays();//輸出表示星期的方式為縮寫

           do

           {

               Systemoutprintf(%sweekdayNames[weekday]);

               dadd(CalendarDAY_OF_MONTH);//日期增移動

               weekday=dget(CalendarDAY_OF_WEEK);//獲取當前日期為星期幾

           }

           while(weekday!=firstDayOfWeek);

           Systemoutprintln();

  

           /*本月號前的日期補位空格*/

           for(int i=;i<=indent;i++)

               Systemoutprint(    );

  

           /*進行打印日期當日日期後用*注釋直到d進入下一個月停止打印*/

           dset(CalendarDAY_OF_MONTH );//將d設置為本月

           do

           {

               int day=dget(CalendarDAY_OF_MONTH);

               Systemoutprintf(%d day);

  

               if(day==today)Systemoutprint(*);

               else Systemoutprint( );

  

               dadd(CalendarDAY_OF_MONTH );

               weekday=dget(CalendarDAY_OF_WEEK);

               if(weekday==firstDayOfWeek)Systemoutprintln();//weekday為星期的第一天則換行打印

           }

           while(dget(CalendarMONTH)==month);

  

           if(weekday!=firstDayOfWeek)Systemoutprintln();

  

       }

  

   }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26355.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.