Here you can find the source of formatForCookieExpiry(Date date)
public static String formatForCookieExpiry(Date date)
//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.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; public class Main { /** * Like PATTERN_RFC1123 but with hyphens between the date components * */ public static final String PATTERN_RFC1123_HYPHENS = "EEE, dd-MMM-yyyy HH:mm:ss zzz"; public static final TimeZone GMT = TimeZone.getTimeZone("GMT"); public static String formatForCookieExpiry(Date date) { return formatDate(date, PATTERN_RFC1123_HYPHENS); } public static String formatDate(Date date) { Calendar cal = Calendar.getInstance(GMT); cal.setTime(date); return formatDate(cal); } /** * * @param cal * @return * @see #PATTERN_WEBDAV */ public static String formatDate(Calendar cal) { // 2005-03-30T05:18:33Z StringBuilder sb = new StringBuilder(); sb.append(cal.get(Calendar.YEAR)).append(""); sb.append('-'); sb.append(pad2(cal.get(Calendar.MONTH) + 1)); sb.append('-'); sb.append(pad2(cal.get(Calendar.DAY_OF_MONTH))); // sb.append('-'); // sb.append( pad2(cal.get(Calendar.DAY_OF_MONTH)) ); // sb.append('-'); // sb.append( pad2(cal.get(Calendar.MONTH)) ); sb.append('T'); sb.append(pad2(cal.get(Calendar.HOUR_OF_DAY))); sb.append(':'); sb.append(pad2(cal.get(Calendar.MINUTE))); sb.append(':'); sb.append(pad2(cal.get(Calendar.SECOND))); sb.append('Z'); String s = sb.toString(); // log.debug(date.toString() + " -> " + s); return s; } /** * Formats the given date according to the specified pattern. The pattern * must conform to that used by the {@link SimpleDateFormat simple date * format} class. * * @param date The date to format. * @param pattern The pattern to use for formatting the date. * @return A formatted date string. * * @throws IllegalArgumentException If the given date pattern is invalid. * * @see SimpleDateFormat */ public static String formatDate(Date date, String pattern) { if (date == null) { throw new IllegalArgumentException("date is null"); } if (pattern == null) { throw new IllegalArgumentException("pattern is null"); } SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.US); formatter.setTimeZone(GMT); return formatter.format(date); } public static String pad2(int i) { if (i < 10) { return "0" + i; } else { return i + ""; } } }