Skip to content

Instantly share code, notes, and snippets.

@jimjam88
Created January 23, 2014 11:36
Show Gist options
  • Save jimjam88/8577164 to your computer and use it in GitHub Desktop.
Save jimjam88/8577164 to your computer and use it in GitHub Desktop.
Java method to convert a date from one format to another
// Imports required
import java.text.SimpleDateFormat;
import java.text.ParseException;
/**
* Convert a date to a different format.
*
* @param currentFormat The current date format
* @param newFormat The new date format
* @param subject The date that's going to be converted
* @return The new data as a string
* @throws ParseException If the conversion fails (Invalid date format)
*/
final public static String convertDate(String currentFormat, String newFormat, String subject)
throws ParseException
{
return new SimpleDateFormat(newFormat).format(
new SimpleDateFormat(currentFormat).parse(subject)
).toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment