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

longconvertDate(String value)
Convert from the two commonly used date formats to a timestamp
long timestamp;
try {
    timestamp = Long.parseLong(value);
    if (timestamp < 0) {
        throw new NumberFormatException("negative timestamp!");
} catch (NumberFormatException e) {
    try {
...
StringconvertDateFormat(String date, SimpleDateFormat formatBefore, SimpleDateFormat formatAfter)
convert Date Format
String convertDateStr = formatAfter.format(formatBefore.parse(date));
return convertDateStr;
StringconvertDateFormat(String sourceDate, String sourceDateFormat, String destDateFormat)
convert Date Format
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat(sourceDateFormat).parse(sourceDate));
String destDate = new SimpleDateFormat(destDateFormat).format(calendar.getTime());
return destDate;
StringconvertDateFormatByRawOffset(DateFormat formatter, int rawOffset, long milliSecond)
convert Date Format By Raw Offset
TimeZone destTZ = TimeZone.getDefault();
String[] timeZoneIds = TimeZone.getAvailableIDs(rawOffset);
if (timeZoneIds != null && timeZoneIds.length > 0) {
    destTZ = TimeZone.getTimeZone(timeZoneIds[0]);
    formatter.setTimeZone(destTZ);
} else {
    formatter.setTimeZone(destTZ);
return formatter.format(new Date(milliSecond));
StringconvertDateFromInDayFormat(String stringDate, String hhmmss)
Convert date from yyyy-MM-dd format or from yyyyMMdd format into yyyyMMdd'T'HHmmss format.
if (stringDate == null || stringDate.length() == 0) {
    return "";
StringBuffer sb = null;
SimpleDateFormat formatter = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
Date date;
try {
    formatter.setLenient(false);
...
StringconvertDateFromTo(String stringDate, String patternToUse, TimeZone timezoneIn, TimeZone timezoneOut)
Convert date from the input date format into specificated format.
if (stringDate == null || stringDate.length() == 0) {
    return "";
String pattern = getDateFormat(stringDate);
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
if (timezoneIn != null) {
    formatter.setTimeZone(timezoneIn);
formatter.setLenient(false);
Date date = formatter.parse(stringDate);
formatter.applyPattern(patternToUse);
if (timezoneOut != null) {
    formatter.setTimeZone(timezoneOut);
return formatter.format(date);
StringconvertDateStringToString(String dateString, String format)
convert Date String To String
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
String dateStr = "";
try {
    date = sdf.parse(dateString);
    dateStr = convertDateToStringByFormat(date);
} catch (ParseException e) {
    e.printStackTrace();
...
StringconvertDateStringToString(String strDate, String currentFormat, String parseFormat)
convert Date String To String
try {
    return convertDateToString(convertStringToDate(strDate, currentFormat), parseFormat);
} catch (Exception e) {
    e.printStackTrace();
    return null;
StringconvertDatetimeFormat(Date tar_date, String date_format)
convert Datetime Format
SimpleDateFormat sdf = new SimpleDateFormat(date_format);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String formated_date = sdf.format(tar_date);
char last_char = date_format.charAt(date_format.length() - 1);
if (last_char == 'Z') {
    formated_date = formated_date.substring(0, formated_date.length() - 2) + ':'
            + formated_date.substring(formated_date.length() - 2, formated_date.length());
return formated_date;
StringconvertIso8601FormatToArsTime(String timeString)
Converts a string in ISO8601 Format into the string corresponding to an Ars internal time representation
Date date = iso8601DateTimeFormatter.parse(timeString);
int secondsSinceEpoch = (int) (date.getTime() / 1000);
return String.valueOf(secondsSinceEpoch);