码迷,mamicode.com
首页 > 其他好文 > 详细

第四章:显示当月日期的小程序。

时间:2014-08-29 17:45:58      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   ar   for   2014   art   div   

 1 package cha4;
 2 
 3 import java.text.DateFormatSymbols;
 4 import java.util.Calendar;
 5 import java.util.GregorianCalendar;
 6 
 7 public class CalendarTest {
 8 
 9     public static void main(String[] args) {
10         GregorianCalendar today = new GregorianCalendar(2014,10,30) ;
11         
12         int curMonth = today.get(Calendar.MONTH) ;
13         today.set(Calendar.DAY_OF_MONTH, 1);
14         
15         int weekday = today.getFirstDayOfWeek() ;//一个星期的星期几为第一天
16         int curWeekday = today.get(Calendar.DAY_OF_WEEK) ;//今天为这个星期的第几天
17         int indent = 0 ; //缩进,显示上个月的日期
18         
19         while(weekday != curWeekday) {
20             today.add(Calendar.DAY_OF_MONTH, -1);
21             curWeekday = today.get(Calendar.DAY_OF_WEEK) ;
22             indent++ ;
23         }
24         
25         String[] weekdayNames = new DateFormatSymbols().getShortWeekdays() ; 
26         
27         for (String weekdayName : weekdayNames) {
28             if(!"".equals(weekdayName.trim()))
29                 System.out.print(weekdayName + "\t");
30         }
31         
32         System.out.println();
33         
34         for(int i =  0 ; i< indent ;i++) {//打印上个月的几天
35             System.out.print(today.get(Calendar.DAY_OF_MONTH) + "\t\t");
36             today.add(Calendar.DAY_OF_MONTH, 1);
37         }
38         
39         do{
40             int day = today.get(Calendar.DAY_OF_MONTH) ;
41             System.out.print(day + "\t\t");
42             today.add(Calendar.DAY_OF_MONTH, 1) ;
43             curWeekday = today.get(Calendar.DAY_OF_WEEK) ;
44             if(curWeekday == weekday)
45                 System.out.println();
46         }while(today.get(Calendar.MONTH) == curMonth) ;
47         
48     }
49 }

 

第四章:显示当月日期的小程序。

标签:style   blog   color   java   ar   for   2014   art   div   

原文地址:http://www.cnblogs.com/twoslow/p/3945345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!