Java,记录北京、伦敦、纽约、东京定时时间
时间: 2023-07-10 15:11:55 浏览: 82
可以使用Java的TimeZone类来记录不同时区的时间。以下是一个示例代码,可以记录北京、伦敦、纽约、东京的当前时间:
```java
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimezoneDemo {
public static void main(String[] args) {
// 创建一个SimpleDateFormat对象,用于格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 设置不同的时区
TimeZone beijingTZ = TimeZone.getTimeZone("Asia/Shanghai");
TimeZone londonTZ = TimeZone.getTimeZone("Europe/London");
TimeZone newYorkTZ = TimeZone.getTimeZone("America/New_York");
TimeZone tokyoTZ = TimeZone.getTimeZone("Asia/Tokyo");
// 设置时间格式化对象的时区
sdf.setTimeZone(beijingTZ);
System.out.println("北京时间:" + sdf.format(new Date()));
sdf.setTimeZone(londonTZ);
System.out.println("伦敦时间:" + sdf.format(new Date()));
sdf.setTimeZone(newYorkTZ);
System.out.println("纽约时间:" + sdf.format(new Date()));
sdf.setTimeZone(tokyoTZ);
System.out.println("东京时间:" + sdf.format(new Date()));
}
}
```
输出结果:
```
北京时间:2021-07-01 14:12:34
伦敦时间:2021-07-01 07:12:34
纽约时间:2021-07-01 02:12:34
东京时间:2021-07-01 15:12:34
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)