gpt4 book ai didi

java - 如何在java中更新小时?

转载 作者:行者123 更新时间:2023-11-29 03:21:09 25 4
gpt4 key购买 nike

我用java做了一个显示当前时间的时钟。我会确保时钟都以一分钟的间隔更新,从而更新控制台中的结果。我阅读使用线程,但我对这个主题不是很了解,谁能帮助我实现它?

import java.util.*;

public class Current
{
public static void main(String[] args)
{
Calendar calendar = new GregorianCalendar();
String hour;
int time = calendar.get(Calendar.HOUR);
int m = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);

if(calendar.get(Calendar.AM_PM) == 0)
hour = "A.M.";
else
hour = "P.M.";
System.out.println(time + ":" + m + ":" + sec + " " + hour);
}
}

class Data
{
public static void main(String[] args)
{
Calendar cal = new GregorianCalendar();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
System.out.println(day + "-" + (month + 1) + "-" + year);
}
}

最佳答案

public static void main(String[] args) {
while (true) {
try {
Thread.sleep(60*1000); //one minute

Calendar calendar = new GregorianCalendar();
String hour;
int time = calendar.get(Calendar.HOUR);
int m = calendar.get(Calendar.MINUTE);
int sec = calendar.get(Calendar.SECOND);

if(calendar.get(Calendar.AM_PM) == 0)
hour = "A.M.";
else
hour = "P.M.";
System.out.println(time + ":" + m + ":" + sec + " " + hour);

} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

关于java - 如何在java中更新小时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23567398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com