Java Utililty Methods Date Format

List of utility methods to do Date Format

Description

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

Method

StringdateTime2Str(Date date, String pattern)
date Time Str
if (null == date) {
    return null;
if (null == pattern) {
    pattern = DEFAULT_DATE_TIME_FORMAT;
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern(pattern);
...
StringdateTime2String(Date value)
date Time String
if (value != null) {
    SimpleDateFormat dateTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    return dateTime.format(value);
return "";
StringdateTime2StringFs(Date date)
Date ---> String (yyyyMMddHHmmss)
return date2String(date, "yyyyMMddHHmmss");
StringdateTimeFormat(Date dateTime)
Formats a given date+time value to an ISO 8601 string.
DateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
iso8601.setTimeZone(UTC);
return iso8601.format(dateTime);
StringDateTimeFormat(final Date date)
Date Time Format
SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
return format.format(date);
StringdateTimeFormatterDate(Date date)
date Time Formatter Date
if (date != null) {
    return dateTimeFormatter.format(date);
return "";
longdateTimeToMillis(String dateTime, String format)
date Time To Millis
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
try {
    return dateFormat.parse(dateTime).getTime();
} catch (ParseException e) {
    return -1;
StringdateTimeToStr(Date date, String format)
date Time To Str
String _timeZone = System.getProperty("user.timezone");
TimeZone timeZone = null;
if (_timeZone == null || "".equals(_timeZone) || "UTC".equals(_timeZone)) {
    timeZone = TimeZone.getDefault();
} else {
    timeZone = TimeZone.getTimeZone("Asia/Shanghai");
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
...
StringdateTimeToZuluFormat(DateTime dateTime)
date Time To Zulu Format
final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss:SSzzz";
final SimpleDateFormat sdf = new SimpleDateFormat(ISO_FORMAT);
final TimeZone utc = TimeZone.getTimeZone("UTC");
sdf.setTimeZone(utc);
return sdf.format(dateTime.toDate());
StringformatD(Date date)
format D
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_STYLE_D);
return sdf.format(date);