Java Date RFC Format toRFC822Date(Date date)

Here you can find the source of toRFC822Date(Date date)

Description

to RFC Date

License

Open Source License

Declaration

public static String toRFC822Date(Date date) 

Method Source Code

//package com.java2s;
/*/*ww  w.  j  a  va 2  s .  c  om*/
 * ***** BEGIN LICENSE BLOCK *****
 * Zimbra Collaboration Suite Server
 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Synacor, Inc.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software Foundation,
 * version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 * ***** END LICENSE BLOCK *****
 */

import java.text.DateFormatSymbols;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    private static final String[] DAY_NAME = new String[] { "", "Sun, ", "Mon, ", "Tue, ", "Wed, ", "Thu, ",
            "Fri, ", "Sat, " };
    private static final String[] MONTH_NAME = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
            "Aug", "Sep", "Oct", "Nov", "Dec" };
    private static final String[][] ZONE_INFO = new DateFormatSymbols().getZoneStrings();

    public static String toRFC822Date(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        return toRFC822Date(cal);
    }

    public static String toRFC822Date(Calendar cal) {

        String tzabbr = getTimezoneAbbreviation(cal.getTimeZone().getID(), cal.get(Calendar.DST_OFFSET) != 0);
        int tzoffset = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 60000;
        char tzsign = tzoffset > 0 ? '+' : '-';
        tzoffset = Math.abs(tzoffset);

        StringBuilder sb = new StringBuilder(40);
        sb.append(DAY_NAME[cal.get(Calendar.DAY_OF_WEEK)]);
        append2DigitNumber(sb, cal.get(Calendar.DAY_OF_MONTH)).append(' ');
        sb.append(MONTH_NAME[cal.get(Calendar.MONTH)]).append(' ');
        sb.append(cal.get(Calendar.YEAR)).append(' ');

        append2DigitNumber(sb, cal.get(Calendar.HOUR_OF_DAY)).append(':');
        append2DigitNumber(sb, cal.get(Calendar.MINUTE)).append(':');
        append2DigitNumber(sb, cal.get(Calendar.SECOND)).append(' ');

        sb.append(tzsign);
        append2DigitNumber(sb, tzoffset / 60);
        append2DigitNumber(sb, tzoffset % 60);
        if (tzabbr != null)
            sb.append(" (").append(tzabbr).append(')');
        return sb.toString();
    }

    private static String getTimezoneAbbreviation(String tzid, boolean dst) {
        if (tzid == null)
            return null;

        for (int tzindex = 0; tzindex < ZONE_INFO.length; tzindex++) {
            if (tzid.equalsIgnoreCase(ZONE_INFO[tzindex][0]))
                return dst ? ZONE_INFO[tzindex][4] : ZONE_INFO[tzindex][2];
        }
        return null;
    }

    private static StringBuilder append2DigitNumber(StringBuilder sb, int number) {
        return sb.append((char) ('0' + number / 10)).append((char) ('0' + number % 10));
    }
}

Related

  1. rfc822(Date date)
  2. rfc822Date(Date date)
  3. rfc822Date(final Date val)
  4. RFC822Section5Format_to_Date(String time)
  5. toRfc822(Calendar cal)
  6. translateStartTimeToRFC3339(String startTime)