Created
June 5, 2018 08:16
-
-
Save pfieffer/e771443819c43f340783c8fbaffd4e1e to your computer and use it in GitHub Desktop.
A function to convert 12 hr time in string format to 24 hr time in string format and vice versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param timeString A string value of time in 12 hr format | |
* @return A string value of time in 24 hr format | |
*/ | |
private String getTimeIn24hrFormat(String timeString) { | |
DateFormat inFormat = new SimpleDateFormat( "hh:mm a"); | |
/* This is a 24 hour date time format that we are using to convert all | |
* input date and time formats to this format. | |
*/ | |
DateFormat outFormat = new SimpleDateFormat( "HH:mm"); | |
Date date = null; | |
try | |
{ | |
date = inFormat.parse(timeString); | |
} | |
catch ( ParseException e ) | |
{ | |
e.printStackTrace(); | |
} | |
if( date != null ) | |
{ | |
return outFormat.format(date); | |
} else { | |
return "NULL"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment