Integer minuteUn = c.get(Calendar.MINUTE)-2; Integer hour = c.get(Calendar.HOUR_OF_DAY); //容错一分钟 Integer minute = c.get(Calendar.MINUTE)-1; if(minute.intValue() == -1){ hour = hour-1; minute = 59; }
时间: 2024-02-19 17:03:05 浏览: 116
您可以尝试使用链式编程的方式来简化代码,同时也可以将重复的代码进行封装,让代码更加简洁易读。以下是一个可能的优化示例:
```java
int minute = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR_OF_DAY);
// 当前时间减 2 分钟
c.add(Calendar.MINUTE, -2);
// 如果减去 2 分钟后,分钟数为负数,说明需要向前推 1 小时
if (c.get(Calendar.MINUTE) < 0) {
c.add(Calendar.HOUR_OF_DAY, -1);
c.set(Calendar.MINUTE, 59);
}
```
这样做的好处是:
1. 链式编程可以避免定义多个变量,使代码更加简洁。
2. 封装重复的代码,提高代码的复用性。
3. 使用 int 类型代替 Integer 类型,避免了不必要的自动装箱和拆箱,提高了性能。
相关问题
public class TestClock extends JFrame { private String AorP; private String hour; private String minute; private String second; public TestClock() { this.setSize(1000, 800); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new GridLayout(2, 1)); Panel p1 = new Panel(new GridLayout(1, 3)); while (true) { Calendar c = Calendar.getInstance(); hour = Integer.toString(c.get(Calendar.HOUR)); minute = Integer.toString(c.get(Calendar.MINUTE)); second = Integer.toString(c.get(Calendar.SECOND)); AorP = Integer.toString(c.get(Calendar.AM_PM)); JLabel HH = new JLabel(hour, SwingConstants.CENTER); JLabel MM = new JLabel(minute, SwingConstants.CENTER); JLabel SS = new JLabel(second, SwingConstants.CENTER); JLabel AorP = new JLabel("AM/PM", SwingConstants.CENTER); HH.setFont(new Font(Font.SERIF, Font.ITALIC, 36)); MM.setFont(new Font(Font.SERIF, Font.ITALIC, 36)); SS.setFont(new Font(Font.SERIF, Font.ITALIC, 36)); AorP.setFont(new Font(Font.SERIF, Font.ITALIC, 26)); p1.add(HH); p1.add(MM); p1.add(SS); this.add(p1); this.add(AorP); } } public static void main(String[] args) { // TODO Auto-generated method stub TestClock clock = new TestClock(); } }为什么不显示
这段代码可能会导致程序卡死,因为while(true)会让程序无限循环下去,而且在while循环中不断地创建和添加标签,可能会导致内存溢出。这会导致程序无法正常运行,因此窗口不会显示。
如果想要实现一个实时显示当前时间的时钟,可以使用Timer类来实现。Timer类可以在指定时间间隔内重复执行指定任务,这样就可以实现每隔一段时间更新一次时钟的效果。具体实现可以参考以下代码:
```java
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
public class TestClock extends JFrame {
private JLabel hourLabel;
private JLabel minuteLabel;
private JLabel secondLabel;
private JLabel amPmLabel;
public TestClock() {
this.setSize(300, 150);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2, 1));
JPanel timePanel = new JPanel(new GridLayout(1, 3));
hourLabel = new JLabel("00", SwingConstants.CENTER);
minuteLabel = new JLabel("00", SwingConstants.CENTER);
secondLabel = new JLabel("00", SwingConstants.CENTER);
timePanel.add(hourLabel);
timePanel.add(minuteLabel);
timePanel.add(secondLabel);
this.add(timePanel);
amPmLabel = new JLabel("AM", SwingConstants.CENTER);
amPmLabel.setFont(new Font(Font.SERIF, Font.ITALIC, 24));
this.add(amPmLabel);
Timer timer = new Timer(1000, e -> {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String amPm = hour < 12 ? "AM" : "PM";
hour = hour % 12;
hourLabel.setText(String.format("%02d", hour));
minuteLabel.setText(String.format("%02d", minute));
secondLabel.setText(String.format("%02d", second));
amPmLabel.setText(amPm);
});
timer.start();
}
public static void main(String[] args) {
TestClock clock = new TestClock();
clock.setVisible(true);
}
}
```
这个时钟每隔1秒更新一次,实时显示当前时间。注意,这里用到了Java 8中的Lambda表达式来简化代码。
b) Make the clock show the current time. For this you should use the Calendar class from java.util with: ● Calendar.getInstance() - create Calendar object for current date/time ● get(field) - return integer field from Calendar object, where field may be Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND, Calendar.AM_PM, etc. ● the value returned from Calendar.AM_PM may be compared with the values Calendar.AM and Calendar.PM to determine if it is before or after mid-day Create a function that runs continuously and updates the display to always show the current time.怎么做
你可以使用以下代码实现这个任务:
```java
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Clock extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel timeLabel;
public Clock() {
super("Current Time");
timeLabel = new JLabel();
timeLabel.setFont(new Font("Arial", Font.PLAIN, 48));
setLayout(new GridLayout(1, 1));
add(timeLabel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void updateTime() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int amPm = calendar.get(Calendar.AM_PM);
String amPmStr = (amPm == Calendar.AM) ? "AM" : "PM";
String time = String.format("%02d:%02d:%02d %s", hour, minute, second, amPmStr);
timeLabel.setText(time);
}
public static void main(String[] args) {
Clock clock = new Clock();
while (true) {
clock.updateTime();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
这个代码定义了一个 `Clock` 类,它继承了 `JFrame` 类,用于显示当前时间。在 `Clock` 类中,我们创建了一个标签 `timeLabel`,用于显示当前时间。然后我们使用 `GridLayout` 将标签放置在 `JFrame` 中。在 `updateTime()` 方法中,我们使用 `Calendar` 类获取当前时间,并将其格式化为字符串。最后我们将时间字符串设置到标签中。
在 `main()` 方法中,我们创建了一个 `Clock` 对象,并在一个无限循环中不断更新时间,并在每次更新后等待 1 秒钟。这样就可以保证时间的实时更新。
阅读全文