MIlitary时间验证
问题描述:
我想确保有5个字符在什么用户输入将其更改为军事时间和几乎这是格式##:##
(与冒号一样),这是我的代码的一部分,但我无法让它工作。MIlitary时间验证
任何帮助将是伟大的 - 谢谢。
public Time(String militaryTime)
{
//Check to make sure something was entered
if (militaryTime == null || !militaryTime.matches("^\\d{2}:\\d{2}$"))
{
System.out.println(
"You must enter a valid miliary time.");
}
//Check to make sure there are 5 characters
else
{
//Check to make sure the colon is in the right spot
if (!Character.isDigit(militaryTime.charAt(2)))
{
System.out.println(militaryTime +
" is not a valid miliary time.");
}
//Check to make sure all other characters are digits
else if (!Character.isDigit(militaryTime.charAt(0)))
{
System.out.println(militaryTime +
" is not a valid miliary time.");
}
else if (!Character.isDigit(militaryTime.charAt(0)))
{
System.out.println(militaryTime +
" is not a valid miliary time.");
}
else if (!Character.isDigit(militaryTime.charAt(0)))
{
System.out.println(militaryTime +
" is not a valid miliary time.");
}
else if (!Character.isDigit(militaryTime.charAt(0)))
{
System.out.println(militaryTime +
" is not a valid miliary time.");
}
else
{
//this separates hours and minutes
hours = Integer.parseInt(militaryTime.substring(0,2));
//validate hours and minutes are valid values
if(hours > 23)
{
System.out.println(militaryTime +
" is not a valid miliary time.");
}
else if(minutes > 59)
{
System.out.println(militaryTime +
" is not a valid miliary time.");
}
//convert military time to conventional time
//for afternoon times
else if (hours > 12)
{
hours = hours - 12;
afternoon = true;
System.out.println(this.toString());
}
//account for midnight
else if (hours == 0)
{
hours = 12;
System.out.println(this.toString());
}
//account for noon
else if (hours == 12)
{
afternoon = true;
System.out.println(this.toString());
}
//morning times don't need converting
else
{
System.out.println(this.toString());
}
}
}
}
答
在大多数语言最简单的方法是使用regular expression:
if (militaryTime == null || !militaryTime.matches("^\\d{2}:\\d{2}$")) {
System.out.println(militaryTime + " is not a valid military time.");
}
这不会检查时间是0-24之间或分钟为0-60之间,但你代码似乎也不在乎。
或者您可以使用Java time API:
try {
DateTimeFormatter.ofPattern("HH:mm").parse(militaryTime)
}
catch (DateTimeParseException e) {
System.out.println(militaryTime + " is not a valid military time.");
}
这将验证它是完全兼容的。
答
试试这个代码
public static boolean isMilitoryTmeString(String militaryTime) {
// Check to make sure something was entered
if (militaryTime == null) {
return false;
}
// Check to make sure there are 5 characters
if (militaryTime.length() != 5) {
return false;
}
// Storing characters into char variable
char hourOne = militaryTime.charAt(0);
char hourTwo = militaryTime.charAt(1);
char colon = militaryTime.charAt(2);
char minuteOne = militaryTime.charAt(3);
char minuteTwo = militaryTime.charAt(4);
//first position of hour must be 0 or 1 or 2
if (hourOne != '0' && hourOne != '1' && hourOne != '2') {
return false;
}
//if first position of hour is 0 or 1 then second
//position must be 0-9
if (hourOne == '0' || hourOne == '1') {
if (hourTwo < '0' || hourTwo > '9') {
return false;
}
//if hourOne equal 2 then second position must be 0-3
} else {
if (hourTwo < '0' || hourTwo > '3') {
return false;
}
}
//third position must be colon
if (colon != ':') {
return false;
}
// fourth position must be 0-5
if (minuteOne < '0' || minuteOne > '5') {
return false;
}
//fifth position must be 0-9
if (minuteTwo < '0' || minuteTwo > '9') {
return false;
}
// String is valid military time
return true;
}
我看不出有什么军事与此有关。看来你正在谈论一个标准的24小时时间格式。 –
@a_horse_with_no_name https://en.wikipedia.org/wiki/24-hour_clock “它在美国,讲英语的加拿大和少数几个其他国家通常被称为军事时间,其中12小时的时钟仍然占主导地位。“ –
@SkinnyJ:真的吗?听起来很奇怪...... –