Java Hour Format formatForHttpHeader(long unixTime)

Here you can find the source of formatForHttpHeader(long unixTime)

Description

Can be used to format a unix timestamp into http header compatible strings.

License

Apache License

Parameter

Parameter Description
unixTime The long (unixtime) to format

Return

a http header compatible string like "Thu, 01 Jan 1970 00:00:00 GMT"

Declaration

public static String formatForHttpHeader(long unixTime) 

Method Source Code


//package com.java2s;
/*/*from ww w  . j a  v  a2 s .  c  o m*/
 * Copyright (C) 2014 the original author or authors.
 *
 * Licensed 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.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    /**
     * Can be used to format a date into http header compatible strings.
     * <p/>
     * It can be used to generate something like: Date: Wed, 05 Sep 2012
     * 09:16:19 GMT Expires: Thu, 01 Jan 1970 00:00:00 GMT
     *
     * @param date The date to format
     * @return a http header compatible string like
     * "Thu, 01 Jan 1970 00:00:00 GMT"
     */
    public static String formatForHttpHeader(Date date) {
        return getRFC1123_DateFormat().format(date);
    }

    /**
     * Can be used to format a unix timestamp into http header compatible
     * strings.
     * <p/>
     * It can be used to generate something like: Date: Wed, 05 Sep 2012
     * 09:16:19 GMT Expires: Thu, 01 Jan 1970 00:00:00 GMT
     *
     * @param unixTime The long (unixtime) to format
     * @return a http header compatible string like
     * "Thu, 01 Jan 1970 00:00:00 GMT"
     */
    public static String formatForHttpHeader(long unixTime) {
        return getRFC1123_DateFormat().format(new Date(unixTime));
    }

    /**
     * From here: http://www.ietf.org/rfc/rfc1123.txt
     */
    static DateFormat getRFC1123_DateFormat() {
        return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
    }
}

Related

  1. formatFF(String date)
  2. formatFileDate(Date from)
  3. formatFilesystemOrder(Date date)
  4. formatForCookieExpiry(Date date)
  5. formatForFileName()
  6. formatFromDate(Date date)
  7. formatFromString(String date)
  8. formatFullDate(Object date)
  9. formatFullTime(Date date)