如何从时间(小时)中删除前导零 [英] How to remove leading zero from time(hours)

查看:62
本文介绍了如何从时间(小时)中删除前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Scanner scan = new Scanner(System.in);int 小时 = scan.nextInt();int 分钟 = scan.nextInt();LocalTime 时间 = LocalTime.of(hour , 分钟);时间 = time.plusMinutes(15);System.out.println(time);

解决方案

您可以使用 DateTimeFormatter 格式为 "H:mm"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:如果字母个数为1,则使用最小位数输出该值,不填充.否则,将使用数字计数作为输出字段的宽度,并根据需要填充零值.以下模式字母对字母数有限制.只能指定一个字母 'c' 和 'F'.最多可以指定两个字母d"、H"、h"、K"、k"、m"和s".最多可以指定三个字母D".

I want to have the hour from 1-9 without the leading zero, but the minutes with the zero while also adding 15 minutes to the time. Right now, when I input 1 and 46 i get 02:01, and i want to get 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);

解决方案

You can use DateTimeFormatter with format "H:mm" 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.

这篇关于如何从时间(小时)中删除前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆