gpt4 book ai didi

java - 如何从时间(小时)中删除前导零

转载 作者:行者123 更新时间:2023-12-01 14:09:11 26 4
gpt4 key购买 nike

我想要从 1 到 9 的小时没有前导零,但是有零的分钟同时还要向时间添加 15 分钟。
现在,当我输入 1 和 46 时,我得到 02:01,我想得到 2:01

Scanner scan = new Scanner(System.in);
int hour = scan.nextInt();
int minutes = scan.nextInt();
LocalTime time = LocalTime.of(hour , minutes);
time = time.plusMinutes(15);
System.out.println(time);

最佳答案

您可以使用 DateTimeFormatter 与格式 “高:毫米”
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html

DateTimeFormatter.ofPattern("H:mm").format(LocalTime.now())

Number: If the count of letters is one, then the value is output using the minimum number of digits and without padding. Otherwise, the count of digits is used as the width of the output field, with the value zero-padded as necessary. The following pattern letters have constraints on the count of letters. Only one letter of 'c' and 'F' can be specified. Up to two letters of 'd', 'H', 'h', 'K', 'k', 'm', and 's' can be specified. Up to three letters of 'D' can be specified.

关于java - 如何从时间(小时)中删除前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61000127/

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