java打印万年历代码 java制作万年历
JAVA编写一个多功能万年历程序
import java.text.SimpleDateFormat; import java.util.Calendar; public class TestDate { public static final String[] weeks = { "日", "一", "二", "三", "四", "五", "六" }; public static void main(String[] args) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR,2011);//2011年 c.set(Calendar.MONTH,0);//java中Calendar类,月从0开始, 0代表一月 c.set(Calendar.DATE,1);//1号 int day = c.get(Calendar.DAY_OF_WEEK);//获致是本周的第几天地, 1代表星期天...7代表星期六 System.out.println(new SimpleDateFormat( "yyyy-MM-dd ").format(c.getTime())); System.out.println("星期" + weeks[day-1]); } } 把以上测试代码写作一个方法 方法的参数名为年月日, 即可。当然Calendar 还有很多功能,比如一周的第几天,一年的第几个月……
企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,创新互联建站面向各种领域:主动防护网等成都网站设计公司、成都全网营销推广解决方案、网站设计等建站排名服务。
用java控制台实现万年历-要求打印当前年月日的日历表格,要求对当天的日期单独标示
以下是用java swing编写的日历,很好用,在我所做的系统里就能够正常的使用
接下来 是具体代码:
package Demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class DateChooser extends JPanel implements ActionListener,
ChangeListener {
/**
*
*/
private static final long serialVersionUID = 1L;
int startYear = 1980;
int lastYear = 2050;
int width = 270;
int height = 200;
Color backGroundColor = Color.gray;
Color palletTableColor = Color.white;
Color todayBackColor = Color.orange;
Color weekFontColor = Color.blue;
Color dateFontColor = Color.black;
Color weekendFontColor = Color.red;
Color controlLineColor = Color.pink;
Color controlTextColor = Color.white;
Color rbFontColor = Color.white;
Color rbBorderColor = Color.red;
Color rbButtonColor = Color.pink;
Color rbBtFontColor = Color.red;
JDialog dialog;
JSpinner yearSpin;
JSpinner monthSpin;
JSpinner hourSpin;
JSpinner minuteSpin;
JButton[][] daysButton = new JButton[6][7];
JFormattedTextField jFormattedTextField;
Calendar c = getCalendar();
DateChooser(JFormattedTextField jftf) {
jFormattedTextField = jftf;
setLayout(new BorderLayout());
setBorder(new LineBorder(backGroundColor, 2));
setBackground(backGroundColor);
JPanel topYearAndMonth = createYearAndMonthPanal();
add(topYearAndMonth, BorderLayout.NORTH);
JPanel centerWeekAndDay = createWeekAndDayPanal();
add(centerWeekAndDay, BorderLayout.CENTER);
}
private JPanel createYearAndMonthPanal() {
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH) + 1;
int currentHour = c.get(Calendar.HOUR_OF_DAY);
int currentMinute = c.get(Calendar.MINUTE);
JPanel result = new JPanel();
result.setLayout(new FlowLayout());
result.setBackground(controlLineColor);
yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear,
lastYear, 1));
yearSpin.setPreferredSize(new Dimension(48, 20));
yearSpin.setName("Year");
yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
yearSpin.addChangeListener(this);
result.add(yearSpin);
JLabel yearLabel = new JLabel("年");
yearLabel.setForeground(controlTextColor);
result.add(yearLabel);
monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1));
monthSpin.setPreferredSize(new Dimension(35, 20));
monthSpin.setName("Month");
monthSpin.addChangeListener(this);
result.add(monthSpin);
JLabel monthLabel = new JLabel("月");
monthLabel.setForeground(controlTextColor);
result.add(monthLabel);
hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23, 1));
hourSpin.setPreferredSize(new Dimension(35, 20));
hourSpin.setName("Hour");
hourSpin.addChangeListener(this);
result.add(hourSpin);
JLabel hourLabel = new JLabel("时");
hourLabel.setForeground(controlTextColor);
result.add(hourLabel);
minuteSpin = new JSpinner(new SpinnerNumberModel(currentMinute, 0, 59,
1));
minuteSpin.setPreferredSize(new Dimension(35, 20));
minuteSpin.setName("Minute");
minuteSpin.addChangeListener(this);
result.add(minuteSpin);
JLabel minuteLabel = new JLabel("分");
minuteLabel.setForeground(controlTextColor);
result.add(minuteLabel);
return result;
}
private JPanel createWeekAndDayPanal() {
String colname[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel result = new JPanel();
result.setFont(new Font("宋体", Font.PLAIN, 12));
result.setLayout(new GridLayout(7, 7));
result.setBackground(Color.white);
JLabel cell;
for (int i = 0; i 7; i++) {
cell = new JLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.CENTER);
if (i == 0 || i == 6)
cell.setForeground(weekendFontColor);
else
cell.setForeground(weekFontColor);
result.add(cell);
}
int actionCommandId = 0;
for (int i = 0; i 6; i++)
for (int j = 0; j 7; j++) {
JButton numberButton = new JButton();
numberButton.setBorder(null);
numberButton.setHorizontalAlignment(SwingConstants.CENTER);
numberButton.setActionCommand(String.valueOf(actionCommandId));
numberButton.addActionListener(this);
numberButton.setBackground(palletTableColor);
numberButton.setForeground(dateFontColor);
if (j == 0 || j == 6)
numberButton.setForeground(weekendFontColor);
else
numberButton.setForeground(dateFontColor);
daysButton[i][j] = numberButton;
numberButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
closeAndSetDate();
}
}
});
result.add(numberButton);
actionCommandId++;
}
return result;
}
private JDialog createDialog(Frame owner) {
JDialog result = new JDialog(owner, "日期时间选择", true);
result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
result.getContentPane().add(this, BorderLayout.CENTER);
result.pack();
result.setSize(width, height);
return result;
}
public void showDateChooser(Point position) {
Object tmpobj=SwingUtilities.getWindowAncestor(jFormattedTextField);
if(tmpobj.getClass().isInstance(new JDialog())||tmpobj.getClass().getSuperclass().isInstance(new JDialog()))
{
JDialog ownerdialog = (JDialog) SwingUtilities
.getWindowAncestor(jFormattedTextField);
Frame owner = (Frame) SwingUtilities.getWindowAncestor(ownerdialog);
if (dialog == null || dialog.getOwner() != owner) {
dialog = createDialog(owner);
}
dialog.setLocation(getAppropriateLocation(owner, position));
}
else if(tmpobj.getClass().isInstance(new JFrame())||tmpobj.getClass().getSuperclass().isInstance(new JFrame()))
{
JFrame ownerFrame = (JFrame) SwingUtilities
.getWindowAncestor(jFormattedTextField);
if (dialog == null || dialog.getOwner() != ownerFrame) {
dialog = createDialog(ownerFrame);
}
dialog.setLocation(getAppropriateLocation(ownerFrame, position));
}
flushWeekAndDay();
dialog.setVisible(true);
}
Point getAppropriateLocation(Frame owner, Point position) {
Point result = new Point(position);
Point p = owner.getLocation();
int offsetX = (position.x + width) - (p.x + owner.getWidth());
int offsetY = (position.y + height) - (p.y + owner.getHeight());
if (offsetX 0) {
result.x -= offsetX;
}
if (offsetY 0) {
result.y -= offsetY;
}
return result;
}
public void closeAndSetDate() {
setDate(c.getTime());
dialog.dispose();
}
private Calendar getCalendar() {
Calendar result = Calendar.getInstance();
result.setTime(getDate());
return result;
}
private int getSelectedYear() {
return ((Integer) yearSpin.getValue()).intValue();
}
private int getSelectedMonth() {
return ((Integer) monthSpin.getValue()).intValue();
}
private int getSelectedHour() {
return ((Integer) hourSpin.getValue()).intValue();
}
private int getSelectedMinute() {
return ((Integer) minuteSpin.getValue()).intValue();
}
private void dayColorUpdate(boolean isOldDay) {
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, 1);
int actionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);
int i = actionCommandId / 7;
int j = actionCommandId % 7;
if (isOldDay)
daysButton[i][j].setForeground(dateFontColor);
else
daysButton[i][j].setForeground(todayBackColor);
}
private void flushWeekAndDay() {
c.set(Calendar.DAY_OF_MONTH, 1);
int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i 6; i++) {
for (int j = 0; j 7; j++) {
String s = "";
if (dayNo = 1 dayNo = maxDayNo)
s = String.valueOf(dayNo);
daysButton[i][j].setText(s);
dayNo++;
}
}
dayColorUpdate(false);
}
public void stateChanged(ChangeEvent e) {
JSpinner source = (JSpinner) e.getSource();
if (source.getName().equals("Minute")) {
c.set(Calendar.MINUTE, getSelectedMinute());
return;
}
if (source.getName().equals("Hour")) {
c.set(Calendar.HOUR_OF_DAY, getSelectedHour());
return;
}
dayColorUpdate(true);
if (source.getName().equals("Year")) {
c.set(Calendar.YEAR, getSelectedYear());
}
if (source.getName().equals("Month")) {
c.set(Calendar.MONTH, getSelectedMonth() - 1);
}
flushWeekAndDay();
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if (source.getText().length() == 0)
return;
dayColorUpdate(true);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
c.set(Calendar.DAY_OF_MONTH, newDay);
}
public void setDate(Date date) {
jFormattedTextField.setText(getDefaultDateFormat().format(date));
}
public Date getDate() {
try {
String dateString = jFormattedTextField.getText();
return getDefaultDateFormat().parse(dateString);
} catch (ParseException e) {
return getNowDate();
} catch (Exception ee) {
return getNowDate();
}
}
private static Date getNowDate() {
return Calendar.getInstance().getTime();
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
}
测试程序:
package Demo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DemoForDateChooser extends JFrame {
private JFormattedTextField formattedTextField;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
DemoForDateChooser frame = new DemoForDateChooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public DemoForDateChooser() {
super();
setTitle("日期选择框");
getContentPane().setLayout(null);
setBounds(100, 100, 383, 137);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 375, 107);
getContentPane().add(panel);
formattedTextField = new JFormattedTextField();
formattedTextField.setBounds(68, 48, 175, 23);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateChooser mDateChooser=new DateChooser(formattedTextField);
Point p = button.getLocationOnScreen();
p.y = p.y + 30;
mDateChooser.showDateChooser(p);
formattedTextField.requestFocusInWindow();
}
});
button.setText("选择日期");
button.setBounds(249, 47, 99, 23);
panel.add(button);
final JLabel label = new JLabel();
label.setText("日期:");
label.setBounds(10, 47, 71, 23);
panel.add(label);
panel.add(formattedTextField);
//
}
}
怎么用java for循环打印万年历基础源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalenderTrain extends JFrame implements ActionListener {
JComboBox Month = new JComboBox(); //月份下拉列表框
JComboBox Year = new JComboBox(); //年份下拉列表框
JLabel Year_l = new JLabel("Year::"); //定义标签
JLabel Month_l = new JLabel("Month::"); //定义标签
Date now_date = new Date(); //获取今天的日期
JButton[] button_day = new JButton[49]; //定义一个数组用来存放日期
JButton button_ok = new JButton("Enter"); //现实选择日期
JButton button_today = new JButton("Today"); //显示今天按钮
int now_year = now_date.getYear() + 1900; //获取年份值
int now_month = now_date.getMonth(); //获取月份值(当前月份-1)
String year_int = null; //存放年份
int month_int; //存放月份
JPanel pane_ym = new JPanel(); //放置下拉列表框和控制按钮面板
JPanel pane_day = new JPanel(); //放置日期面板
JPanel pane_parent = new JPanel(); //放置以上两个面板
//定义方法绘制面板
public CalenderTrain() {
super("Calender!"); //设定面板得title
//---以下几行使得关闭面板时退出程序
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClose(WindowEvent e) {
System.exit(0);
}
});
//---
setResizable(false); //面板的大小不能变化
//设定年月
/*年份的区间是当前年份的过去10年到当前年份的未来20年
* 月份正常1??12月
*/
for (int i = now_year - 10; i = now_year + 20; i++) {
Year.addItem(i + "");
}
for (int i = 1; i 13; i++) {
Month.addItem(i + "");
}
Year.setSelectedIndex(10);//设定年份下拉列表为当前年份
pane_ym.add(Year_l);//添加年份标签
pane_ym.add(Year);//添加年份下拉列表框
Month.setSelectedIndex(now_month);//设定月份下拉列表为当前月份
pane_ym.add(Month_l);//添加月份标签
pane_ym.add(Month);//添加月份下拉列表框
pane_ym.add(button_ok);//添加确定按钮
pane_ym.add(button_today);//添加“今天”按钮
button_ok.addActionListener(this);//确定按钮添加监听事件
button_today.addActionListener(this);//“今天”按钮添加监听事件
//年月设定结束
//初始化日期按钮并绘制
pane_day.setLayout(new GridLayout(7, 7, 10, 10));
for (int i = 0; i 49; i++) {
button_day[i] = new JButton(" ");
pane_day.add(button_day[i]);
}
this.setDay();//调用setDay()方法
pane_parent.setLayout(new BorderLayout());//设定布局管理器
setContentPane(pane_day);
setContentPane(pane_ym);
pane_parent.add(pane_day, BorderLayout.SOUTH);
pane_parent.add(pane_ym, BorderLayout.NORTH);
setContentPane(pane_parent);
pack();
show();
}
void setDay() {
year_int = Year.getSelectedItem().toString();
month_int = Month.getSelectedIndex();
int year_sel = Integer.parseInt(year_int) - 1900;//获得年份值
Date dt = new Date(year_sel, month_int, 1);//构造一个日期
GregorianCalendar cal = new GregorianCalendar();//创建一个Calendar实例
cal.setTime(dt);
String week[] = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
int day = 0;//day中存放某个月份的天数
int day_week = 0;//用来存放某个月的第一天是星期几的数值
//--将星期添加到前7个按钮中
for (int i = 0; i 7; i++) {
button_day[i].setText(week[i]);
}
//--
/*判断是几月份,根据它来设定day的值
* 其中二月份要判断是否是闰年
*/
if (month_int == 0
|| month_int == 2
|| month_int == 4
|| month_int == 6
|| month_int == 7
|| month_int == 9
|| month_int == 11) {
day = 31;
} else if (
month_int == 3
|| month_int == 5
|| month_int == 8
|| month_int == 10) {
day = 30;
} else {
if (cal.isLeapYear(year_sel)) {
day = 29;
} else {
day = 28;
}
}
day_week = 7 + dt.getDay();
int count = 1;
/*绘制按钮
* 在这里我们首先要根据选定的月份的第一天是星期几来确定我们绘制按钮的起始位置
* 其中day_week就是我们要绘制的起始位置
* 对于那些没有数值可以显示的按钮要置空
*/
for (int i = day_week; i day_week + day; count++, i++) {
if (i % 7 == 0
|| i == 13
|| i == 20
|| i == 27
|| i == 48
|| i == 34
|| i == 41) {
button_day[i].setForeground(Color.RED);
button_day[i].setText(count + "");
} else {
button_day[i].setText(count + "");
}
}
//--对于没有日期数值显示的按钮进行置空处理
if (day_week == 0) {
for (int i = day; i 49; i++) {
button_day[i].setText(" ");
}
} else {
//第一天前面的按钮置空
for (int i = 7; i day_week; i++) {
button_day[i].setText(" ");
}//最后一天后面的按钮置空
for (int i = day_week + day; i 49; i++) {
button_day[i].setText(" ");
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_ok) {
this.setDay();//如果点击确定按钮就调用setDay()重新方法绘制按钮
} else if (e.getSource() == button_today) {
new CalenderTrain();//如果点击今天按钮,得到今天的日期
}
}
public static void main(String[] args) {
CalenderTrain ct = new CalenderTrain();
}
}
java中,一段万年历代码,跑起来总是差4天,修改之后也只能让一部分月份的显示正确,代码如下
我改造了一下你的代码 :
package com.mikuma.calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Calendar{
public static void main(String[] args){
int year = 0;
int month = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要查询的年份");
year = scanner.nextInt();
System.out.println("请输入您要查询的月份");
while (true){
month = scanner.nextInt();
if (month 0 || month 12){
System.out.println("月份输入有误,请重新输入");
}else{
break;
}
}
printPermanentCalendar(year, month);
}
/**
* 输出万年历
*
* @param year
* @param month
*/
private static void printPermanentCalendar(int year,int month){
int days = 0;
int totaldays = 0;//获取1990年至查询的年份的天数
for (int i = 1900; i year; i++){
totaldays = totaldays + (isLeapYear(i) ? 366 : 365);
}
int beforedays = 0;//到指定月份的天数
for (int i = 1; i = month; i++){
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = isLeapYear(year) ? 29 : 28;
break;
default:
break;
}
if (i month){
beforedays = beforedays + days;
}
}
totaldays = totaldays + beforedays;//总计天数,以判断周几;
int weekDay = 0;
int temp = (1 + totaldays) % 7;
if (temp == 0){//1990年1月1日星期一,据此日0天星期一,以此类推
weekDay = 0;
}else{
weekDay = temp;
}
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for (int i = 0; i weekDay; i++){
System.out.print("\t");
}
for (int i = 1; i = days; i++){
System.out.print(i + "\t");
if ((totaldays + i) % 7 == 6){
System.out.print("\n");
}
}
}
private static boolean isLeapYear(int year){
return new GregorianCalendar().isLeapYear(year);
}
}
运行:
对比了下 360日历:
结果正确
我们再测试下 2017 年 2月
对比 360 日历
也是正确
JAVA万年历代码
/*
题目:输出任意年份任意月份的日历表(公元后)
思路:
1.已知1年1月1日是星期日,1 % 7 = 1 对应的是星期日,2 % 7 = 2 对应的是星期一,以此类推;
2.计算当年以前所有天数+当年当月1号之前所有天数;
a.年份分平年闰年,平年365天,闰年366天;
b.闰年的判断方法year % 400 == 0 || (year % 100 != 0 year % 4 == 0)若为真,则为闰年否则为平年;
c.定义平年/闰年数组,包含各月天数;
d.遍历数组求和,计算当年当月前总天数;
e.当年以前所有天数+当年当月前总天数+1即为1年1月1日到当年当月1日的总天数;
3.总天数对7取模,根据结果判断当月1号是星期几,输出空白区域;
4.输出当月日历表,逢星期六换行
*/
import java.util.Scanner;
class FindMonthList {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt(); //年份
if (year 1) { //判断非法输入年份
System.out.println("输入错误!");
return;
}
System.out.println("请输入月份:");
int month = sc.nextInt(); //月份
if (month 1 || month 12) { //判断非法输入月份
System.out.println("输入错误!");
return;
}
//输出表头
System.out.println("-------" + year + " 年 " + month + " 月 " + "-------");
System.out.println();
System.out.println("日 一 二 三 四 五 六");
//计算当前年份以前所有天数beforeYearTotalDay;每4年一个闰年,闰年366天,平年365天
int beforeYearTotalDay = ((year - 1) / 4 * 366) + (year-1 - ((year - 1) / 4)) * 365;
int[] arrLeapYear = {0,31,29,31,30,31,30,31,31,30,31,30,31}; //闰年各月天数 int数组
int[] arrNormalYear = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //平年各月天数 int数组
int beforeMonthTotalDay = 0; //定义本年当月之前月份的总天数
if (year % 400 == 0 || (year % 100 != 0 year % 4 == 0)) { //判断当前年份是否是闰年
for (int i = 0 ; i month ; i ++ ) { //for循环计算当月之前总天数
//计算当前月份之前的所有天数
beforeMonthTotalDay = beforeMonthTotalDay + arrLeapYear[i];
}
//判断当月1日是星期几
int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;
int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日
for (int i = 0 ; i (week - 1 + 7) % 7 ; i ++) { //如果写成i (week-1)会出现i-1的情况
System.out.print(" ");//输出开头空白
}
for (int i = 1 ;i = arrLeapYear[month] ;i ++ ) { //for循环输出各月天数
System.out.print(i + " ");
if (i 10 ) { //小于10的数补一个空格,以便打印整齐
System.out.print(" ");
}
if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrLeapYear[month]) {//每逢星期六/尾数换行
System.out.println();
}
}
} else { //不是闰年就是平年
for (int i = 0 ; i month ; i ++ ) { //for循环计算出当月之前月份总天数
beforeMonthTotalDay = beforeMonthTotalDay + arrNormalYear[i];
}
//判断当月1日是星期几
int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;
int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日
for (int i = 0 ; i (week - 1 + 7) % 7 ; i ++) { //如果写成i (week-1)会出现i-1的情况
System.out.print(" ");//输出开头空白
}
for (int i = 1 ;i = arrNormalYear[month] ;i ++ ) {//for循环输出各月天数
System.out.print(i + " ");
if (i 10 ) { //小于10的数补一个空格,以便打印整齐
System.out.print(" ");
}
if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrNormalYear[month]) {//每逢星期六/尾数换行
System.out.println();
}
}
}
}
}
显示效果:
java万年历源代码是多少?
package org.java.test;
import java.util.Scanner;
public class CalendarTest{
public static void main(String[] args) {
System.out.println("欢 迎 使 用 万 年 历");
Scanner input = new Scanner(System.in);
System.out.print("\n请选择年份: ");
int year = input.nextInt();
System.out.print("\n请选择月份: ");
int month = input.nextInt();
System.out.println();
int days = 0; // 存储当月的天数
boolean isRn;
/* 判断是否是闰年 */
if (year % 4 == 0 !(year % 100 == 0) || year % 400 == 0) { // 判断是否为闰年
isRn = true; // 闰年
} else {
isRn = false;// 平年
}
/* 计算输入的年份之前的天数 */
int totalDays = 0;
for (int i = 1900; i year; i++) {
/* 判断闰年或平年,并进行天数累加 */
if (i % 4 == 0 !(i % 100 == 0) || i % 400 == 0) { // 判断是否为闰年
totalDays = totalDays + 366; // 闰年366天
} else {
totalDays = totalDays + 365; // 平年365天
}
}
/* 计算输入月份之前的天数 */
int beforeDays = 0;
for (int i = 1; i = month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
if (isRn) {
days = 29;
} else {
days = 28;
}
break;
default:
days = 30;
break;
}
if (i month) {
beforeDays = beforeDays + days;
}
}
totalDays = totalDays + beforeDays; // 距离1900年1月1日的天数
/* 计算星期几 */
int firstDayOfMonth; // 存储当月第一天是星期几:星期日为0,星期一~星期六为1~6
int temp = 1 + totalDays % 7; // 从1900年1月1日推算
if (temp == 7) { // 求当月第一天
firstDayOfMonth = 0; // 周日
} else {
firstDayOfMonth = temp;
}
/* 输出日历 */
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for (int nullNo = 0; nullNo firstDayOfMonth; nullNo++) {
System.out.print("\t"); // 输出空格
}
for (int i = 1; i = days; i++) {
System.out.print(i + "\t");
if ((totalDays + i-1) % 7 == 5) { // 如果当天为周六,输出换行
System.out.println();
}
}
}
}
这是你要的万年历吗?
网站名称:java打印万年历代码 java制作万年历
浏览路径:http://scyanting.com/article/hiodic.html