Java Date Format formatDate(Date date, DateFormat dateFormat, char delimiter, boolean endWithDelimiter)

Here you can find the source of formatDate(Date date, DateFormat dateFormat, char delimiter, boolean endWithDelimiter)

Description

Formats the date according to the specified format and returns as a string.

License

Apache License

Parameter

Parameter Description
date The date / time object to format
dateFormat The date format to use when outputting the date
delimiter The character used to separate the time zone difference hours and minutes
endWithDelimiter Determines whether the date string will end with the delimiter character

Return

the formatted date string

Declaration

private static String formatDate(Date date, DateFormat dateFormat,
        char delimiter, boolean endWithDelimiter) 

Method Source Code

//package com.java2s;
/*//from   w w w  .  j a v  a  2 s  . co m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    /**
     * Formats the date according to the specified format and returns as a string.
     * @param date The date / time object to format
     * @param dateFormat The date format to use when outputting the date
     * @param delimiter The character used to separate the time zone difference hours and minutes
     * @param endWithDelimiter Determines whether the date string will end with the delimiter character
     * @return the formatted date string
     */
    private static String formatDate(Date date, DateFormat dateFormat,
            char delimiter, boolean endWithDelimiter) {
        Calendar cal = Calendar.getInstance(dateFormat.getTimeZone(),
                Locale.ENGLISH);
        cal.setTime(date);
        int offset = getOffsetInMinutes(cal);
        StringBuilder sb = new StringBuilder(dateFormat.format(date));
        appendOffset(sb, delimiter, offset, endWithDelimiter);
        return sb.toString();
    }

    private static int getOffsetInMinutes(Calendar cal) {
        int offset = cal.get(Calendar.ZONE_OFFSET);
        offset += cal.get(Calendar.DST_OFFSET);
        offset /= (1000 * 60);
        return offset;
    }

    private static void appendOffset(StringBuilder sb, char delimiter,
            int offset, boolean endWithDelimiter) {
        if (offset == 0) {
            appendOffsetUTC(sb);
        } else {
            appendOffsetNoUTC(sb, delimiter, offset, endWithDelimiter);
        }
    }

    private static void appendOffsetUTC(StringBuilder sb) {
        sb.append('Z');
    }

    private static void appendOffsetNoUTC(StringBuilder sb, char delimiter,
            int offset, boolean endWithDelimiter) {
        int zoneOffsetHours = offset / 60;
        appendOffsetSign(sb, zoneOffsetHours);
        appendPaddedNumber(sb, Math.abs(zoneOffsetHours));
        sb.append(delimiter);
        appendPaddedNumber(sb, Math.abs(offset % 60));
        if (endWithDelimiter) {
            sb.append(delimiter);
        }
    }

    private static void appendOffsetSign(StringBuilder sb,
            int zoneOffsetHours) {
        if (zoneOffsetHours >= 0) {
            sb.append('+');
        } else {
            sb.append('-');
        }
    }

    private static void appendPaddedNumber(StringBuilder sb, int number) {
        if (number < 10) {
            sb.append('0');
        }
        sb.append(number);
    }
}

Related

  1. formatDate(Date date)
  2. formatDate(Date date)
  3. formatDate(Date date)
  4. formatDate(Date date)
  5. formatDate(Date date)
  6. formatDate(Date date, Locale locale)
  7. formatDate(Date date, SimpleDateFormat dateFormat)
  8. formatDate(Date fecha)
  9. formatDate(Date in)