Skip to content

Instantly share code, notes, and snippets.

@francisnnumbi
Last active January 8, 2018 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisnnumbi/7f069bde4c7c22951632ba55ce8974d8 to your computer and use it in GitHub Desktop.
Save francisnnumbi/7f069bde4c7c22951632ba55ce8974d8 to your computer and use it in GitHub Desktop.
Converting a String date from one displayable format to another.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
public class MyDateUtils {
/**
* Convert from one format to another
* @param date the date in string. e.g. "23/10/2014 23:45.50"
* @param currentFormat the actual format of date. e.g. "dd/MM/yyyy HH:mm.ss"
* @param newFormat the new format in which date will be displayed. e.g. "MM-dd-yyyy hh:mm a"
* @return the date in a new displayed format.
*/
public static String swapFormat(String date, String currentFormat, String newFormat) throws ParseException {
Date d = new SimpleDateFormat(currentFormat).parse(date);
return new SimpleDateFormat(newFormat).format(d);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment