Created
April 28, 2016 10:11
-
-
Save nichtemna/99fecb9c12ccc46f7b258f6f280094ff to your computer and use it in GitHub Desktop.
Utilities class for converting date/time related strings
This file contains hidden or 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.ParseException; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Date; | |
| public class DateTimeUtils { | |
| public static String getCurrentDateToFormat(SimpleDateFormat dateFormat) { | |
| return dateToFormat(new Date(), dateFormat); | |
| } | |
| public static String dateToFormat(Date date, SimpleDateFormat dateFormat) { | |
| return dateFormat.format(date); | |
| } | |
| public static Date dateFromStringFormat(String string, SimpleDateFormat dateFormat) { | |
| Date date = null; | |
| try { | |
| date = dateFormat.parse(string); | |
| } catch (ParseException e) { | |
| e.printStackTrace(); | |
| } | |
| return date; | |
| } | |
| public static String formatToFormat(String dateString, SimpleDateFormat fromFormat, SimpleDateFormat toFormat) { | |
| String formattedString = ""; | |
| try { | |
| Date date = fromFormat.parse(dateString); | |
| formattedString = toFormat.format(date); | |
| } catch (ParseException e) { | |
| e.printStackTrace(); | |
| } | |
| return formattedString; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment