Skip to content

Instantly share code, notes, and snippets.

@pfieffer
Created June 5, 2018 08:16
Show Gist options
  • Save pfieffer/e771443819c43f340783c8fbaffd4e1e to your computer and use it in GitHub Desktop.
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
/**
*
* @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