Last active
January 8, 2018 11:03
-
-
Save francisnnumbi/7f069bde4c7c22951632ba55ce8974d8 to your computer and use it in GitHub Desktop.
Converting a String date from one displayable format to another.
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
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