java打印输出万年历_java输出万年历

这段Java代码演示了如何使用GregorianCalendar类来创建并打印一个日历,包括当前月份的日期布局,其中当前日期用'▲'标识。程序首先获取当前月份,然后设置日期为1,接着打印出日历的头部,再逐天打印出日历,周六时换行。
摘要由CSDN通过智能技术生成

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

public class MyDate {

public static void main(String[] args) {

MyDate date = new MyDate();

Calendar calendar = Calendar.getInstance() ;

int month = calendar.get(Calendar.MONTH ) + 1 ;

calendar.set(Calendar.MONTH , month ) ;

date.myCalendar( calendar.getTime() ) ;

}

//实现日历的方法

public void myCalendar(Date date) {

GregorianCalendar now = new GregorianCalendar();

// 打印当前时间

// 设置当前时间

now.setTime(date);

// 从日期中取得当前的日

int toDay = now.get(Calendar.DAY_OF_MONTH);

// 从日期中取得当前的月

int month = now.get(Calendar.MONTH) ;

// 设置now的日期为1

now.set(Calendar.DAY_OF_MONTH, 1);

// 得到now是一周的第几天

int week = now.get(Calendar.DAY_OF_WEEK);

// 打印日历头部标示

System.out.println("日\t一\t二\t三\t四\t五\t六");

// 打印当前日期前面的空格

for (int i = Calendar.SUNDAY; i < week; i++) {

System.out.print("\t");

}

// 打印日历主体

while (now.get(Calendar.MONTH) == month) {

int day = now.get(Calendar.DAY_OF_MONTH);

// 对输出的日历进行对齐,小于10的加上一个空格

if (day < 10) {

// 如果是当前日期,加上标示

if (day == toDay) {

System.out.print("▲" + day + "▲\t");

} else {

System.out.print(" " + day + "\t");

}

} else {

// 如果是当前日期,加上标示

if (day == toDay) {

System.out.print("▲" + day + "▲\t");

} else {

System.out.print("" + day + "\t");

}

}

//如果是周六,进行换行

if (week == Calendar.SATURDAY) {

System.out.println();

}

//每次输出日期后,将日期增加一天

now.add(Calendar.DAY_OF_MONTH, 1);

//重新获得一周的第几天

week = now.get(Calendar.DAY_OF_WEEK);

}

}

}

a162a83af2f478607284680352485b02.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值