一个显示当前时间的java程序
时间: 2024-03-11 15:49:19 浏览: 36
以下是一个简单的 Java 程序,可以显示当前时间:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class CurrentTime {
public static void main(String[] args) {
// 创建 SimpleDateFormat 对象,用于格式化时间
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
// 获取当前时间
Date now = new Date();
// 使用 SimpleDateFormat 格式化时间,并输出
System.out.println("当前时间:" + dateFormat.format(now));
}
}
```
运行程序后,会输出类似以下的内容:
```
当前时间:12:34:56
```
相关问题
用Java写一个显示当前时间的手机小程序
以下是用Java编写的一个显示当前时间的手机小程序示例:
```java
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeDisplay extends JFrame {
private JLabel timeLabel;
public TimeDisplay() {
setTitle("当前时间");
setSize(200, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timeLabel = new JLabel();
timeLabel.setFont(new Font("宋体", Font.BOLD, 20));
timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
updateTime();
add(timeLabel, BorderLayout.CENTER);
setVisible(true);
}
private void updateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = dateFormat.format(new Date());
timeLabel.setText(time);
new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
updateTime();
}).start();
}
public static void main(String[] args) {
new TimeDisplay();
}
}
```
该示例程序采用了Swing框架,使用JLabel组件显示时间,并且每秒钟更新一次。用户可以直接运行该程序,即可在窗口中看到当前的时间。
用java语言写一个显示当前日期和时间的程序
以下是Java语言写显示当前日期和时间的程序的示例代码:
```java
import java.util.Date;
public class DateTimeExample {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println("Current date and time: " + currentDate.toString());
}
}
```
运行程序后,控制台将会显示类似以下的输出:
```
Current date and time: Wed Aug 11 16:13:14 CST 2021
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)