Time Conversion

  • + 0 comments

    Scala

    def timeConversion(s: String): String = {
        // Write your code here
            val isAM = if(s.toUpperCase().contains("AM")) true else false
                
        val timeArray = s.split(":")
        val hours = timeArray(0)
        val minutes = timeArray(1)
        val seconds = timeArray(2)
                
        val militaryTime: String = hours match {
            case "12" if(isAM) => s"00:${minutes}:${seconds}"
            case "12" => s"12:${minutes}:${seconds}"
            case s if(!isAM) => s"${(Integer.parseInt(hours) + 12)}:${minutes}:${seconds}"
            case _ =>  s
        }
        
        militaryTime.replace("PM","").replace("AM", "")
    
            
        }