Here you can find the source of toGMTString(Date dtIn)
Parameter | Description |
---|---|
dtIn | a parameter |
public static String toGMTString(Date dtIn)
//package com.java2s; /** *************************************************************** Util.java//from ww w. ja v a 2 s. c o m Copyright (C) 2001 Brendon Upson http://www.wnc.net.au info@wnc.net.au 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; either version 2 of the License, or (at your option) any later version. 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *************************************************************** */ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.TimeZone; public class Main { public static final String SHORT_DATE = "shortdate"; public static final String LONG_DATE = "longdate"; public static final String SHORT_DATE_TIME = "shortdatetime"; public static final String LONG_DATE_TIME = "longdatetime"; /** * Convert a date into a format suitable for http headers etc "EEE, dd MMM yyyy HH:mm:ss z" * @param dtIn * @return */ public static String toGMTString(Date dtIn) { if (dtIn == null) dtIn = new Date(); String sGMTFormat = "EEE, dd MMM yyyy HH:mm:ss z"; return formatDate(dtIn, sGMTFormat, Locale.UK, TimeZone.getTimeZone("GMT")); } /** * Format dates, convenience method * @param dtFormat * @param sFormatString * @return */ public static String formatDate(java.util.Date dtFormat, String sFormatString) { return formatDate(dtFormat, sFormatString, Locale.getDefault(), TimeZone.getDefault()); } /** * Convert a date into a formatted String. This will automatically do the timezone * conversion too. * To convert to a GMT string: * puakma.util.Util.formatDate(new java.util.Date(), "EEE, dd MMM yyyy HH:mm:ss z", Locale.UK, TimeZone.getTimeZone("GMT")); * * @param dtFormat Date to format; dtFormat; Locale (for languages); TimeZone * @param sFormatString * @param locale may be null * @param tz may be null * @return the string representation of the date of "" if the date could not be converted */ public static String formatDate(Date dtFormat, String sFormatString, Locale locale, TimeZone tz) { if (sFormatString == null) sFormatString = ""; if (dtFormat == null) return ""; if (sFormatString.indexOf("date") >= 0) { // shortdate, longdate etc if (sFormatString.equalsIgnoreCase(SHORT_DATE)) { DateFormat df = null; if (locale != null) df = DateFormat.getDateInstance(DateFormat.SHORT, locale); else df = DateFormat.getDateInstance(DateFormat.SHORT); if (tz != null) df.setTimeZone(tz); return df.format(dtFormat); } if (sFormatString.equalsIgnoreCase(SHORT_DATE_TIME)) { DateFormat df = null; if (locale != null) df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale); else df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); if (tz != null) df.setTimeZone(tz); return df.format(dtFormat); } if (sFormatString.equalsIgnoreCase(LONG_DATE)) { DateFormat df = null; if (locale != null) df = DateFormat.getDateInstance(DateFormat.LONG, locale); else df = DateFormat.getDateInstance(DateFormat.LONG); if (tz != null) df.setTimeZone(tz); return df.format(dtFormat); } if (sFormatString.equalsIgnoreCase(LONG_DATE_TIME)) { DateFormat df = null; if (locale != null) df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale); else df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT); if (tz != null) df.setTimeZone(tz); return df.format(dtFormat); } //we should never get here... } //regular formatting, eg "dd/MM/yy" SimpleDateFormat sdf; if (sFormatString.length() == 0) sdf = new SimpleDateFormat(); else { if (locale == null) sdf = new SimpleDateFormat(sFormatString); else sdf = new SimpleDateFormat(sFormatString, locale); } if (tz != null) sdf.setTimeZone(tz); try { return sdf.format(dtFormat); } catch (Exception p) { } return ""; } /** * Returns the position of one byte array within another. * @param src * @param find * @return -1 if not found */ public static int indexOf(byte src[], byte find[]) { if (src == null || find == null || find.length == 0) return -1; int i = -1; for (i = 0; i < src.length; i++) { if (src[i] == find[0]) { boolean bOK = true; for (int k = 0; k < find.length; k++) { int iSrcPos = i + k; if (iSrcPos >= src.length) return -1; if (src[iSrcPos] != find[k]) bOK = false; } //for k if (bOK) return i; } } //for i return -1; } /** * Returns the position of one byte array within another. * @param src * @param find * @return -1 if not found */ public static int indexOf(char src[], char find[]) { if (src == null || find == null || find.length == 0) return -1; int i = -1; for (i = 0; i < src.length; i++) { if (src[i] == find[0]) { boolean bOK = true; for (int k = 0; k < find.length; k++) { int iSrcPos = i + k; if (iSrcPos >= src.length) return -1; if (src[iSrcPos] != find[k]) bOK = false; } //for k if (bOK) return i; } } //for i return -1; } }