Skip to content

Instantly share code, notes, and snippets.

@nichtemna
Created April 28, 2016 10:11
Show Gist options
  • Select an option

  • Save nichtemna/99fecb9c12ccc46f7b258f6f280094ff to your computer and use it in GitHub Desktop.

Select an option

Save nichtemna/99fecb9c12ccc46f7b258f6f280094ff to your computer and use it in GitHub Desktop.
Utilities class for converting date/time related strings
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