Java Hour Format formatWCPDate(String date, String sFormat)

Here you can find the source of formatWCPDate(String date, String sFormat)

Description

This method returns the date formatted by the given format.

License

Open Source License

Parameter

Parameter Description
date a date in the format 'dd/MM/yyyy' e.g. '17/04/2003'
sFormat The format to use for the output

Return

A string representation of the date.

Declaration

public static String formatWCPDate(String date, String sFormat) 

Method Source Code

//package com.java2s;
/**/*  w  w  w .  ja v a 2  s .  co  m*/
 * ? 2003 Cordys R&D B.V. All rights reserved. The computer program(s) is the proprietary information of Cordys R&D B.V. and
 * provided under the relevant License Agreement containing restrictions on use and disclosure. Use is subject to the License
 * Agreement.
 */

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     * This method returns the date formatted by the given format. If date is null or empty the current date will be used. if
     * sFormat is null or empty the SQL-server default will be used :'yyyy-mm-ddTHH:MM:SS.M' The should be specified in the
     * following format '17/04/2003' (as returned by the standard (generated) BAC).
     * 
     * @param date a date in the format 'dd/MM/yyyy' e.g. '17/04/2003'
     * @param sFormat The format to use for the output
     * @return A string representation of the date.
     */
    public static String formatWCPDate(String date, String sFormat) {
        String sReturn = null;

        Date dDate = new Date();

        if ((date != null) && (date.length() != 0)) {
            SimpleDateFormat wcpFormat = new SimpleDateFormat("dd/MM/yyyy");

            try {
                dDate = wcpFormat.parse(date);
            } catch (Exception e) {
                // ignore exceptions
            }
        }

        String sRealFormat = sFormat;

        if ((sRealFormat == null) || sRealFormat.equals("")) {
            sRealFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(sRealFormat);

        sReturn = fixedDateFormat(sdf, dDate);

        return sReturn;
    }

    /**
     * Formats the date with the given date format class. This method fixes milliseconds in the formated string. Milliseconds are
     * outputed with minimum of three digits and zeroes are appended the left (which is incorrect for more than three digits).
     * 
     * @param sdf Date format object to be used.
     * @param dDate Date to be formatted.
     * @return Formatted string.
     */
    private static String fixedDateFormat(SimpleDateFormat sdf, Date dDate) {
        String sFormat = sdf.toPattern();
        String sMillisPattern = sFormat.replaceFirst(".*[^S](S+).*", "$1");

        if (sMillisPattern.equals(sFormat)) {
            // No milliseconds defined.
            return sdf.format(dDate);
        }

        int iMillisDigits = sMillisPattern.length();

        if (iMillisDigits == 3) {
            // This is the default behaviour, so use the default implementation.
            return sdf.format(dDate);
        }

        FieldPosition fp = new FieldPosition(DateFormat.Field.MILLISECOND);
        StringBuffer sb = new StringBuffer(50);

        sdf.format(dDate, sb, fp);

        int iStart = fp.getBeginIndex();
        int iEnd = fp.getEndIndex();

        if ((iStart < 0) || (iEnd <= iStart) || (iEnd > sb.length())) {
            // Probably the field was not in the string.
            return sb.toString();
        }

        String sValue = sb.substring(iStart, iEnd);

        if (iMillisDigits < 3) {
            sValue = sValue.substring(0, iMillisDigits);
        } else {
            sValue = Integer.toString(Integer.parseInt(sValue));

            // When less that one 1000 millis, add the zeros to the beginning.
            // otherwise we change the actual value.
            while (sValue.length() < 3) {
                sValue = "0" + sValue;
            }

            // Add extra zeros to the end.
            while (sValue.length() < iMillisDigits) {
                sValue += "0";
            }
        }

        sb.replace(iStart, iEnd, sValue);

        return sb.toString();
    }
}

Related

  1. formatUnsignedVersion(String ver)
  2. formatValue(Object value)
  3. formatVerbose(int level, String message, String source)
  4. formatW3CDateTime(Date date)
  5. formatW3CDateTime(Date date)
  6. formatWithAge(Date date, String dateFormat, String suffix)
  7. formatWithSql92Date(Date date)
  8. formatXEP0082Date(Date date)
  9. formatXsdDate (final Date date)