Java Hour Format formatDateAddHMS(String strDate)

Here you can find the source of formatDateAddHMS(String strDate)

Description

format Date Add HMS

License

Apache License

Declaration

public static String formatDateAddHMS(String strDate) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String CMS_DRAW_SEQUENCE_FORMAT = "yyyyMMddHHmmss";

    public static String formatDateAddHMS(String strDate) {
        String strNewDate = "";
        if (strDate != null && strDate.length() > 8) {
            if (strDate.length() <= 14) {
                strNewDate = strDate;/*  w ww .  j  av a  2  s .c  om*/
            } else {
                strNewDate = strDate.substring(0, 14);
            }
        } else {
            DateFormat df = DateFormat.getTimeInstance();
            String str = formatDateAsCmsDrawSequence(new Date());
            str = str.substring(8, str.length());
            strNewDate = strDate + str;
        }
        return formatDateTime(formatDate(strNewDate));
    }

    public static String formatDateAsCmsDrawSequence(Date date) {
        return formatDate(date, CMS_DRAW_SEQUENCE_FORMAT);
    }

    public static String formatDateTime(Date date) {
        return formatDate(date, DATE_TIME_FORMAT);
    }

    public static String formatDate(Date date) {
        return formatDate(date, DATE_FORMAT);
    }

    public static String formatDate(Date date, String dateFormat) {
        try {
            return new SimpleDateFormat(dateFormat).format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static Date formatDate(String date) {
        return parseDate(date, CMS_DRAW_SEQUENCE_FORMAT);
    }

    public static Date parseDate(String date) {
        return parseDate(date, DATE_FORMAT);
    }

    public static Date parseDate(String dateString, String dateFormat) {
        try {
            return new SimpleDateFormat(dateFormat).parse(dateString);
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. formatDate(String inputStr)
  2. formatDate(String pattern, Date date)
  3. formatDate2DateTimeStr()
  4. formatDate2DateTimeStr()
  5. formatDate3DateTimeStr(Date date)
  6. formatDateAndTime(Date date)
  7. formatDateAsString(Date date)
  8. formatDateForCERMe(Date date, boolean includeTime)
  9. formatDateInTime(Date date)