MIlitary时间验证

时间:2015-11-07 18:35:32

标签: java

我想确保用户输入的内容中有5个字符可以将其更改为军事时间,而且这几乎是<?php require_once "YOURMAGENTODIR/app/Mage.php"; umask(0); Mage::app('admin'); Mage::setIsDeveloperMode(true); $productCollection=Mage::getResourceModel('catalog/product_collection'); foreach($productCollection as $product){ echo $product->getId(); echo "<br/>"; $MediaDir=Mage::getConfig()->getOptions()->getMediaDir(); echo $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product'; echo "<br/>"; $MediaGallery=Mage::getModel('catalog/product_attribute_media_api')->items($product->getId()); echo "<pre>"; print_r($MediaGallery); echo "</pre>"; foreach($MediaGallery as $eachImge){ $MediaDir=Mage::getConfig()->getOptions()->getMediaDir(); $MediaCatalogDir=$MediaDir .DS . 'catalog' . DS . 'product'; $DirImagePath=str_replace("/",DS,$eachImge['file']); $DirImagePath=$DirImagePath; // remove file from Dir $io = new Varien_Io_File(); $io->rm($MediaCatalogDir.$DirImagePath); $remove=Mage::getModel('catalog/product_attribute_media_api')->remove($product->getId(),$eachImge['file']); } } 格式(带冒号),这是我的一部分代码,但我不能使它工作。

任何帮助都会很棒 - 谢谢。

##:##

2 个答案:

答案 0 :(得分:3)

大多数语言中最简单的方法是使用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.");
}

这将验证它是完全合规的。

答案 1 :(得分:0)

试试这段代码

 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;
        }