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 16:03:05 浏览: 27
您可以尝试使用链式编程的方式来简化代码,同时也可以将重复的代码进行封装,让代码更加简洁易读。以下是一个可能的优化示例:
```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.util.Calendar;
public class Clock {
public static void main(String[] args) {
while (true) {
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);
System.out.print("\r" + time);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
在这个程序中,我们使用 `Calendar.getInstance()` 获取当前日期和时间的 `Calendar` 对象,并使用 `get()` 方法获取小时、分钟、秒和上午/下午的值。然后,我们使用 `String.format()` 方法格式化字符串,并使用 `System.out.print()` 方法在控制台上输出当前时间。通过使用 `\r`,我们可以在每秒更新时覆盖前一个时间的输出,从而创建实时更新的时钟。最后,我们使用 `Thread.sleep()` 方法暂停程序一秒钟,以便下一秒钟的时间可以被计算和输出。