输出当前月份日历(Print a calendar of present month)
2008-05-11 22:37
731 查看
/**
@version 1.0 2008-05-11
@author Cay S.Hostsman
Modified by Climbing Huang when learning Core Java 2
*/
import java.util.*;
public class CalendarTest_CN
{
public static void main(String[] args)
{
// construct d as current date
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
// set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
// print heading
System.out.println("星期一 星期二 星期三 星期四 星期五 星期六 星期日");
// indent first line of calendar
for (int i = Calendar.MONDAY; i < weekday; i++ )
System.out.print(" ");
do
{
// print day
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%6d", day);
// mark current day with *
if (day == today)
System.out.print("* ");
else
System.out.print(" ");
// start a new line after every Saturday
if (weekday == Calendar.SUNDAY)
System.out.println();
// advance d to the next day
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (d.get(Calendar.MONTH) == month);
// the loop exits when d is day 1 of the next month
// print final end of line if necessary
if (weekday != Calendar.MONDAY)
System.out.println();
}
}
A snapshot of execution result:
@version 1.0 2008-05-11
@author Cay S.Hostsman
Modified by Climbing Huang when learning Core Java 2
*/
import java.util.*;
public class CalendarTest_CN
{
public static void main(String[] args)
{
// construct d as current date
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
// set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
// print heading
System.out.println("星期一 星期二 星期三 星期四 星期五 星期六 星期日");
// indent first line of calendar
for (int i = Calendar.MONDAY; i < weekday; i++ )
System.out.print(" ");
do
{
// print day
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%6d", day);
// mark current day with *
if (day == today)
System.out.print("* ");
else
System.out.print(" ");
// start a new line after every Saturday
if (weekday == Calendar.SUNDAY)
System.out.println();
// advance d to the next day
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (d.get(Calendar.MONTH) == month);
// the loop exits when d is day 1 of the next month
// print final end of line if necessary
if (weekday != Calendar.MONDAY)
System.out.println();
}
}
A snapshot of execution result:
相关文章推荐
- 【Calendar】为什么我的DAT_OF_MONTH输出来的不是当前月份的日期,而是5?
- 遇到的问题----java中获取当前月份c.get(Calendar.MONTH)中月份少一个月
- 输出当前月份的日历
- Andrdoi 如何获得某个月份最后一天(或号数最大那天)的号数?--使用getActualMaximum(Calendar.DAY_OF_MONTH)!
- Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段
- 遇到的问题----java中获取当前月份c.get(Calendar.MONTH)中月份少一个月
- Calendar.DAY_OF_MONTH存放什么数据
- Calendar 日历取当前时间
- 输出一个集合所有子集的元素和(Print sums of all subsets of a given set)
- JS创建当前月份的日历table型
- JDK神坑:JAVA中Calendar的月份Month少1
- 5.输入一个整数,如果该整数介于1到12之间,则输出对应的月份,比如输入为4,则输出”April“ -_-!;否则输出”not valid month number“
- java打印当前月份的日历
- 输入年月,输出该月份的日历页
- c#图片添加当前月份的日历
- calendar.getActualMaximum(calendar.DAY_OF_MONTH)的陷阱
- nowDate.add(Calendar.DAY_OF_MONTH,1); 谁能告诉我这里面的 1 是什么意思
- 日历、时间日期、星期、月份的算法(Calendar类对日期的处理)
- c#图片添加当前月份的日历
- java 实现打印当前月份的日历