《Core Java》练习:displays a calendar for the current month

打印一个类似Liunx cal命令的图形,不过不仅限于今天所在的月份,而是任意合法日期的,主要参考了LocalDate类的java doc。

效果如下图:

废话不多说,直接上代码:

package com.company;

import java.time.LocalDate;

public class DataPrint {
    public String formatMonthStr(LocalDate localDate) {
        String rel = String.format("%d: %s\n",localDate.getYear(),
                localDate.getMonth().toString())+"Mon  Tue  Wed  Thu  Fri  Sat  Sun\n";
        int dayOfMon = localDate.getDayOfMonth();
        int thisMonthDays = localDate.getMonth().length(localDate.isLeapYear());
        LocalDate firstDayOfMonth = localDate.minusDays(dayOfMon);
        int DayOfWeek = firstDayOfMonth.getDayOfWeek().getValue() % 7;
        StringBuilder startStr = new StringBuilder();
        startStr.append("     ".repeat(Math.max(0, DayOfWeek)));
        for (int i = 1; i <= thisMonthDays; i++,DayOfWeek=(DayOfWeek+1)%7)
        {
            String day=(i==dayOfMon)? String.format("%2d*",i) : String.format("%3d",i);
            startStr.append(day);
            if(DayOfWeek==6) {
                startStr.append("\n");
            }
            else {
                startStr.append("  ");
            }
        }
        return rel+startStr;
    }
}
package com.company;

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

public class Main {

    public static void main(String[] args) {
	// write your code here
        while (true) {
            System.out.println("Please print a timeStr such as 2022-02-24\n" +
                    "Enter \"quit\" to exit");
            Scanner cin=new Scanner(System.in);
            String ft=cin.next();
            if(ft.equals("quit"))
                break;
            DataPrint dataPrint = new DataPrint();
            LocalDate today = LocalDate.parse(ft);
            System.out.println(dataPrint.formatMonthStr(today));
        }
    }

}

运行效果如下:

搞定! 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Sure, I can help you with that. Here's the code for creating a digital clock in Java: ```java import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class DigitalClock extends JFrame { private JLabel hourLabel, minuteLabel, secondLabel, amPmLabel; public DigitalClock() { setTitle("Digital Clock"); setSize(300, 150); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(2, 2)); panel.setBackground(Color.BLACK); hourLabel = new JLabel("", JLabel.CENTER); hourLabel.setFont(new Font("Serif", Font.ITALIC, 36)); hourLabel.setForeground(Color.WHITE); panel.add(hourLabel); minuteLabel = new JLabel("", JLabel.CENTER); minuteLabel.setFont(new Font("Serif", Font.ITALIC, 36)); minuteLabel.setForeground(Color.WHITE); panel.add(minuteLabel); secondLabel = new JLabel("", JLabel.CENTER); secondLabel.setFont(new Font("Serif", Font.ITALIC, 36)); secondLabel.setForeground(Color.WHITE); panel.add(secondLabel); amPmLabel = new JLabel("", JLabel.CENTER); amPmLabel.setFont(new Font("Serif", Font.ITALIC, 20)); amPmLabel.setForeground(Color.WHITE); panel.add(amPmLabel); add(panel); setVisible(true); } public void updateTime() { Calendar calendar = Calendar.getInstance(); SimpleDateFormat timeFormat = new SimpleDateFormat("hh"); int hour = Integer.parseInt(timeFormat.format(calendar.getTime())); timeFormat = new SimpleDateFormat("mm"); int minute = Integer.parseInt(timeFormat.format(calendar.getTime())); timeFormat = new SimpleDateFormat("ss"); int second = Integer.parseInt(timeFormat.format(calendar.getTime())); timeFormat = new SimpleDateFormat("a"); String amPm = timeFormat.format(calendar.getTime()); hourLabel.setText(String.format("%02d", hour)); minuteLabel.setText(String.format("%02d", minute)); secondLabel.setText(String.format("%02d", second)); amPmLabel.setText(amPm); } public static void main(String[] args) { DigitalClock clock = new DigitalClock(); while (true) { clock.updateTime(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` Here's how the code works: - The `DigitalClock` class extends `JFrame` and creates a window with a 2x2 grid with labels for the hours, minutes, seconds, and AM/PM. - The `updateTime` method gets the current time using `Calendar.getInstance()` and formats it using `SimpleDateFormat`. - The `main` method creates an instance of the `DigitalClock` class and runs a continuous loop that calls the `updateTime` method every second using `Thread.sleep(1000)`. I hope this helps you create your digital clock!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值