英文:
convert time variable to hh:mm format in java
问题
我是Java的初学者,使用以下代码可以生成“hh:mm:ss”格式的随机时间。我不知道如何调整代码以在“hh:mm”格式中显示时间,因为我不熟悉Java的日期和时间库。我查看了这里的帖子,例如https://stackoverflow.com/questions/2949699/converting-time-from-hhmmss-to-hhmm-in-java,但对我没有帮助。
```Java
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));
我还尝试过:
// 以hh:mm格式创建随机时间,涵盖0-12小时,但我想要完整的24小时时间线
public static String createRandomTime() {
DateFormat format = new SimpleDateFormat("h.mm aa");
String timeString = format.format(new Date()).toString();
return timeString;
}
我会感谢您的帮助。
<details>
<summary>英文:</summary>
I'm a beginner in java and I with my code below I can generate a random time in `"hh:mm:ss"` format . I have no idea how to tweak my code to display time in "hh:mm" format as I am not familiar with the
Date and Time java libraries . I have checked posts here like https://stackoverflow.com/questions/2949699/converting-time-from-hhmmss-to-hhmm-in-java but it does not help here .
```Java
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));
I have also tried :
// 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;
}
I would appreciate your help .
答案1
得分: 2
你可以编写一个方法来创建合适的随机小时和随机分钟,然后构造一个java.time.LocalTime
对象,并返回所需的String
表示。
以下是一个示例:
public static String createRandomTime() {
// 为小时和分钟创建有效(在范围内)的随机int值
int randomHours = ThreadLocalRandom.current().nextInt(0, 23);
int randomMinutes = ThreadLocalRandom.current().nextInt(0, 59);
// 然后使用它们创建一个LocalTime对象并返回其String表示
return LocalTime.of(randomHours, randomMinutes).format(
// 使用所需的模式
DateTimeFormatter.ofPattern("HH:mm")
);
}
在类似下面的main
方法中执行该方法十次:
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(createRandomTime());
}
}
将产生类似于以下输出(不一定相等):
08:16
07:54
17:15
19:41
14:24
12:00
12:33
11:00
09:11
02:33
请注意,如果你只想要另一种格式,从这些int
值和相应的从中创建的LocalTime
不会改变。你可以轻松地将模式切换到另一个模式(也许可以将模式字符串作为方法的参数)。例如,你可以将模式设置为"hh:mm a"
,得到类似于10:23 AM
的String
。
英文:
You could write a method that creates proper random hours and random minutes, then construct a java.time.LocalTime
of them and return a desired String
representation.
Here's an example:
public static String createRandomTime() {
// create valid (in range) random int values for hours and minutes
int randomHours = ThreadLocalRandom.current().nextInt(0, 23);
int randomMinutes = ThreadLocalRandom.current().nextInt(0, 59);
// then create a LocalTime from them and return its String representation
return LocalTime.of(randomHours, randomMinutes).format(
// using a desired pattern
DateTimeFormatter.ofPattern("HH:mm")
);
}
Executing that method ten times in a main
like this
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(createRandomTime());
}
}
will produce an output like (not necessarily equal to)
08:16
07:54
17:15
19:41
14:24
12:00
12:33
11:00
09:11
02:33
Please note that the int
values and corresponding LocalTime
created from them will not change if you just want another format. You can easily switch the pattern to another one (maybe make the pattern String
a parameter of the method). E.g. you could make it "hh:mm a"
for String
s like 10:23 AM
.
答案2
得分: 1
因为您正在使用SimpleDateFormat
,我建议您查看一下其文档。
在文档中,您可以看到h
代表上午/下午制的小时。因为您想要24小时制的格式,您需要使用H
或者k
,具体取决于您是想要0-23还是1-24。
英文:
Since you're using the SimpleDateFormat
, I'd suggest taking a look to its documentation
In there, you can see that h
is for hour in AM/PM format. Since you want the 24h format, you'll need either H
or k
, depending if you want it to be 0-23 or 1-24
答案3
得分: 1
以下是翻译好的内容:
你可以尝试使用以下代码,
public void testDateFormat() {
String format = "HH:mm"; //24小时制
//hh:mm aa 为12小时制
DateFormat dateFormat = new SimpleDateFormat(format);
String date = dateFormat.format(new Date());
System.out.println(date);
}
有一个很棒的Javadoc可用于解释各种选项的详细信息。请同时参考Javadoc,链接:https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html
英文:
You can try with the following code,
public void testDateFormat() {
String format = "HH:mm"; //24 hours format
//hh:mm aa for 12 hours format
DateFormat dateFormat = new SimpleDateFormat(format);
String date = dateFormat.format(new Date());
System.out.println(date);
}
There is a fantastic Javadoc is available to explain the details of various options. Please refer the Javadoc as well https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论