如何用java写一个年日历

如何用java写一个年日历

没啥难的,话不多说上代码😎😎

看注释应该没问题💕💕💖


import java.time.LocalDate;
import java.util.Scanner;

public class YearCalender {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		System.out.println("请输入年:");
		int year = input.nextInt();
	
		
		
				
		for (int i = 1; i <= 12; i++) {
			
			//得到该年第一天
			LocalDate date = LocalDate.of(year, i, 1);
			
			//得到该月1日是周几
			int dayOfWeek = date.getDayOfWeek().getValue();
			
			//得到该月是多少天
			int dayOfMonth = date.lengthOfMonth();
			
            //打印每个月的表头
			System.out.println(year+" 年"+"          "+i+" 月");
			System.out.println("----------------------------------------------------------");
			System.out.printf("%10s%10s%10s%10s%10s%10s%10s%n","周一","周二","周三","周四","周五","周六","周日");
			
			
			//输出每个月表头的空格
			for (int j = 2; j <= dayOfWeek ; j++) {
				System.out.printf("  %4s","  ");
			}
			
			//输出每天
			for (int j = 1; j <= dayOfMonth; j++) {
				System.out.printf(" %5d",j);
			//每7天换行一次	
				if ((dayOfWeek +j-1)%7 == 0 ) {
					System.out.println();
				}
			}
            
			//每个月之间空开更美观
			System.out.println();
			System.out.println();
		}
	}
}

运行结果:

请输入年:
2022
20221----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
                                   1     2
     3     4     5     6     7     8     9
    10    11    12    13    14    15    16
    17    18    19    20    21    22    23
    24    25    26    27    28    29    30
    31

20222----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
           1     2     3     4     5     6
     7     8     9    10    11    12    13
    14    15    16    17    18    19    20
    21    22    23    24    25    26    27
    28

20223----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
           1     2     3     4     5     6
     7     8     9    10    11    12    13
    14    15    16    17    18    19    20
    21    22    23    24    25    26    27
    28    29    30    31

20224----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
                             1     2     3
     4     5     6     7     8     9    10
    11    12    13    14    15    16    17
    18    19    20    21    22    23    24
    25    26    27    28    29    30

20225----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
                                         1
     2     3     4     5     6     7     8
     9    10    11    12    13    14    15
    16    17    18    19    20    21    22
    23    24    25    26    27    28    29
    30    31

20226----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
                 1     2     3     4     5
     6     7     8     9    10    11    12
    13    14    15    16    17    18    19
    20    21    22    23    24    25    26
    27    28    29    30

20227----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
                             1     2     3
     4     5     6     7     8     9    10
    11    12    13    14    15    16    17
    18    19    20    21    22    23    24
    25    26    27    28    29    30    31


20228 月
   周一   周二   周三  周四   周五   周六   周日
     1     2     3     4     5     6     7
     8     9    10    11    12    13    14
    15    16    17    18    19    20    21
    22    23    24    25    26    27    28
    29    30    31

20229 月
   周一   周二   周三  周四   周五   周六   周日
                       1     2     3     4
     5     6     7     8     9    10    11
    12    13    14    15    16    17    18
    19    20    21    22    23    24    25
    26    27    28    29    30

202210----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
                                   1     2
     3     4     5     6     7     8     9
    10    11    12    13    14    15    16
    17    18    19    20    21    22    23
    24    25    26    27    28    29    30
    31

202211----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
           1     2     3     4     5     6
     7     8     9    10    11    12    13
    14    15    16    17    18    19    20
    21    22    23    24    25    26    27
    28    29    30

202212----------------------------------------------------------
   周一   周二   周三  周四   周五   周六   周日
                       1     2     3     4
     5     6     7     8     9    10    11
    12    13    14    15    16    17    18
    19    20    21    22    23    24    25
    26    27    28    29    30    31
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个使用Java的GUI日历: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.Calendar; public class CalendarGUI extends JFrame { private JLabel label; private JButton prevButton; private JButton nextButton; private JPanel monthPanel; private JPanel dayPanel; private Calendar calendar; public CalendarGUI() { // 设置窗口标题 setTitle("Calendar"); // 创建日历对象 calendar = Calendar.getInstance(); // 初始化界面 initUI(); // 显示窗口 pack(); setLocationRelativeTo(null); setVisible(true); } private void initUI() { // 设置布局管理器 setLayout(new BorderLayout()); // 创建月份选择面板 monthPanel = new JPanel(); monthPanel.setLayout(new FlowLayout()); // 添加上个月按钮 prevButton = new JButton("<"); prevButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calendar.add(Calendar.MONTH, -1); updateUI(); } }); monthPanel.add(prevButton); // 添加月份标签 label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); monthPanel.add(label); // 添加下个月按钮 nextButton = new JButton(">"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calendar.add(Calendar.MONTH, 1); updateUI(); } }); monthPanel.add(nextButton); // 添加月份选择面板 add(monthPanel, BorderLayout.NORTH); // 创建日期面板 dayPanel = new JPanel(); dayPanel.setLayout(new GridLayout(0, 7)); // 添加星期标签 String[] weekdays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; for (String weekday : weekdays) { JLabel label = new JLabel(weekday, JLabel.CENTER); dayPanel.add(label); } // 添加日期标签 updateUI(); add(dayPanel, BorderLayout.CENTER); } private void updateUI() { // 清空日期面板 dayPanel.removeAll(); // 更新月份标签 SimpleDateFormat format = new SimpleDateFormat("MMMM yyyy"); label.setText(format.format(calendar.getTime())); // 添加日期标签 calendar.set(Calendar.DAY_OF_MONTH, 1); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 0; i < dayOfWeek; i++) { dayPanel.add(new JLabel("")); } for (int i = 1; i <= daysInMonth; i++) { JLabel label = new JLabel(Integer.toString(i), JLabel.CENTER); dayPanel.add(label); } // 重新绘制界面 revalidate(); repaint(); } public static void main(String[] args) { // 创建日历GUI对象 new CalendarGUI(); } } ``` 这个日历GUI界面由两个面板组成,一个是月份选择面板,另一个日期显示面板。月份选择面板包括上个月按钮、月份标签和下个月按钮。日期显示面板则根据当前月份显示日期标签。 当用户点击上个月或下个月按钮时,程序会根据当前日期计算出上个月或下个月的日期,并重新绘制日期面板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

让线程再跑一会

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值