java 20*60*1000L
时间: 2023-07-23 16:27:27 浏览: 93
这是一个Java语言中的数学运算,结果为1200000L。在Java中,如果我们要表示一个 long 类型的整数,需要在数字后面加上 L 或 l。因此,20*60*1000L 表示一个 long 类型的整数,结果为 1200000。如果不加 L 或 l,则默认为 int 类型,结果为 1200000,但是在计算过程中可能会出现溢出的情况。
相关问题
java超过24小时的时间怎么表示
Java中可以使用java.util.Date或java.time.Instant类来表示超过24小时的时间。
使用java.util.Date类:
1. 创建Date对象表示一段时间间隔。
例如,下面的代码表示从1970年1月1日起,经过100天、10小时、30分、20秒的时间:
```
Date timeInterval = new Date(100L * 24L * 3600L * 1000L + 10L * 3600L * 1000L + 30L * 60L * 1000L + 20L * 1000L);
```
2. 使用SimpleDateFormat格式化输出时间间隔。
例如,下面的代码将上面的时间间隔格式化为天-时:分:秒的形式:
```
SimpleDateFormat sdf = new SimpleDateFormat("DD-'days'-HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
System.out.println(sdf.format(timeInterval));
```
输出:
```
100-days-10:30:20
```
使用java.time.Instant类:
1. 创建Instant对象表示一个持续时间。
例如,下面的代码表示持续3天、5小时、10分钟、15秒:
```
Instant duration = Instant.ofEpochSecond(3L * 24L * 3600L + 5L * 3600L + 10L * 60L + 15L);
```
2. 使用Duration格式化输出持续时间。
例如,下面的代码将上面的持续时间格式化为天-时:分:秒的形式:
```
Duration d = Duration.between(Instant.EPOCH, duration);
long days = d.toDays();
long hours = d.toHours() % 24;
long minutes = d.toMinutes() % 60;
long seconds = d.getSeconds() % 60;
System.out.println(String.format("%d-days-%02d:%02d:%02d", days, hours, minutes, seconds));
```
输出:
```
3-days-05:10:15
```
注意:以上两种方式都是基于UTC时间计算的,如果需要转换到其他时区,需要使用时区相关的类。
用Java绘制方形电子表
你可以使用Java提供的Graphics类和JFrame类来绘制方形电子表。
以下是一个简单的示例代码:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SquareClock extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private int hours, minutes, seconds;
public SquareClock() {
Thread t = new Thread(this);
t.start();
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
updateTime();
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int squareSize = Math.min(width, height) - 20;
int x = (width - squareSize) / 2;
int y = (height - squareSize) / 2;
// 绘制方形边框
g.setColor(Color.BLACK);
g.drawRect(x, y, squareSize, squareSize);
// 绘制小时数
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(String.format("%02d", hours), x + 10, y + 30);
// 绘制冒号
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(":", x + squareSize / 2 - 10, y + 30);
// 绘制分钟数
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(String.format("%02d", minutes), x + squareSize / 2 + 10, y + 30);
// 绘制冒号
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(":", x + squareSize / 2 + 40, y + 30);
// 绘制秒数
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString(String.format("%02d", seconds), x + squareSize / 2 + 50, y + 30);
}
private void updateTime() {
long millis = System.currentTimeMillis();
seconds = (int) (millis / 1000 % 60);
minutes = (int) (millis / (60 * 1000) % 60);
hours = (int) (millis / (60 * 60 * 1000) % 24);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.add(new SquareClock());
frame.setVisible(true);
}
}
```
这个示例代码创建一个继承自JPanel的SquareClock类。在构造函数中启动一个新线程来更新时间,并在每次更新后调用repaint()方法重新绘制电子表。
在paintComponent()方法中,首先计算出方形的大小和位置,然后使用Graphics类的drawRect()方法绘制方形边框。接下来,使用setFont()和drawString()方法绘制小时数、分钟数和秒数。
最后,在main()方法中创建一个JFrame并将SquareClock添加到其中。运行程序后将会看到一个简单的方形电子表。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)