我是 java 的初学者,我使用下面的代码可以生成"hh:mm:ss"
格式的随机时间。我不知道如何调整我的代码以“hh:mm”格式显示时间,因为我不熟悉日期和时间 java 库。我在这里检查了帖子,例如在 java 中将时间从 hh:mm:ss 转换为 hh:mm ,但在这里没有帮助。
import java.util.Random;
import java.sql.Time;
final Random random = new Random();
final int millisInDay = 24*60*60*1000;
Time time = new Time((long)random.nextInt(millisInDay));
我也试过:
// creates random time in hh:mm format for 0-12 hours but I want the full 24 hour timeline
public static String createRandomTime() {
DateFormat format = new SimpleDateFormat("h.mm aa");
String timeString = format.format(new Date()).toString();
return timeString;
}
感谢您的帮助。