Java Utililty Methods Date Format Change

List of utility methods to do Date Format Change

Description

The list of methods to do Date Format Change are organized into topic(s).

Method

StringchangeDateFormat(String dateString, String srcFormat, String destFormat)
Convert the string date to a particular format.
if (srcFormat == null || destFormat == null || srcFormat.isEmpty() || destFormat.isEmpty()) {
    return "";
SimpleDateFormat dateFormat = new SimpleDateFormat(srcFormat);
if (dateString == null || dateString.isEmpty()) {
    return "";
java.util.Date dateVal = dateFormat.parse(dateString, new ParsePosition(0));
...
DateFormatchangeDateFormatPattern(final DateFormat dateFormat, String regex, String replacement)
change Date Format Pattern
if (dateFormat instanceof SimpleDateFormat) {
    SimpleDateFormat simpleDateFormat = (SimpleDateFormat) dateFormat;
    String pattern = simpleDateFormat.toPattern().replaceAll(regex, replacement);
    simpleDateFormat.applyPattern(pattern);
    return simpleDateFormat;
return dateFormat;
StringchangeDateTimeZone(String date_GMT)
change Date Time Zone
DateFormat format = new SimpleDateFormat("M/d/yyyy hh:mm:ss a");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(date_GMT);
format.setTimeZone(timeZone);
return format.format(date);
DatechangeDateValue(Date firstDate, Date secondDate)
change Date Value
Calendar calendarFirst = Calendar.getInstance();
calendarFirst.setTime(firstDate);
Calendar calendarSecond = Calendar.getInstance();
calendarSecond.setTime(secondDate);
calendarSecond.set(Calendar.YEAR, calendarFirst.get(Calendar.YEAR));
calendarSecond.set(Calendar.MONTH, calendarFirst.get(Calendar.MONTH));
calendarSecond.set(Calendar.DAY_OF_MONTH, calendarFirst.get(Calendar.DAY_OF_MONTH));
return calendarSecond.getTime();
...
StringconvertDate(String date, String sourceFormat, String destinationFormat)
convert the input date from source to destination format
String returnDateString = "";
SimpleDateFormat formatter = new SimpleDateFormat(sourceFormat);
Date returnDate = formatter.parse(date);
formatter = new SimpleDateFormat(destinationFormat);
returnDateString = formatter.format(returnDate);
return returnDateString;
DateconvertDate(String date, String[] formats)
This method try to parse a string into a Date object using an array with the valid formats
Date ret = null;
for (String pattern : formats) {
    try {
        ret = new SimpleDateFormat(pattern).parse(date);
        break;
    } catch (java.text.ParseException e) {
if (ret == null)
    throw new java.text.ParseException(date, 0);
return ret;
StringconvertDate(String dateIn, String fromDateFormat, String toDateFormat)
convert Date
String dateOut;
try {
    SimpleDateFormat formatIn = new SimpleDateFormat(fromDateFormat);
    SimpleDateFormat formatOut = new SimpleDateFormat(toDateFormat);
    Date data = formatIn.parse(dateIn, new ParsePosition(0));
    dateOut = formatOut.format(data);
} catch (Exception e) {
    dateOut = null;
...
StringconvertDate(String inPattern, String outPattern, String date)
convert Date
Date newDate = getDate(inPattern, date);
return formatDate(outPattern, newDate);
StringconvertDate(String inPattern, String outPattern, String date)
Parses a date string according to a specified input pattern, and formats the date back to a string according to a specified output pattern.
Date newDate = getDate(inPattern, date);
return formatDate(outPattern, newDate);
StringconvertDate(String str)
convert Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
return sdf1.format(sdf.parse(str));