Here you can find the source of format(final Calendar cal)
Calendar
value into an ISO8601-compliant date/time string.
Parameter | Description |
---|---|
cal | The time value to be formatted into a date/time string. |
public static String format(final Calendar cal)
//package com.java2s; /*/*from w w w . ja va 2s .c o m*/ * Copyright (C) 2007 ETH Zurich * * This file is part of Fosstrak (www.fosstrak.org). * * Fosstrak is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * Fosstrak 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Fosstrak; if not, write to the Free * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */ import java.text.DecimalFormat; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; public class Main { /** * Miscellaneous numeric formats used in formatting. */ public static final DecimalFormat XX_FORMAT = new DecimalFormat("00"); public static final DecimalFormat XXX_FORMAT = new DecimalFormat("000"); public static final DecimalFormat XXXX_FORMAT = new DecimalFormat("0000"); /** * Formats a <code>Calendar</code> value into an ISO8601-compliant date/time * string. * * @param cal * The time value to be formatted into a date/time string. * @return The formatted date/time string. */ public static String format(final Calendar cal) { if (cal == null) { throw new IllegalArgumentException("argument can not be null"); } // determine era and adjust year if necessary int year = cal.get(Calendar.YEAR); if (cal.isSet(Calendar.ERA) && cal.get(Calendar.ERA) == GregorianCalendar.BC) { /** * calculate year using astronomical system: year n BCE => * astronomical year -n + 1 */ year = 0 - year + 1; } /** * the format of the date/time string is: YYYY-MM-DDThh:mm:ss.SSSTZD * note that we cannot use java.text.SimpleDateFormat for formatting * because it can't handle years <= 0 and TZD's */ StringBuilder buf = new StringBuilder(); // year ([-]YYYY) buf.append(XXXX_FORMAT.format(year)); buf.append('-'); // month (MM) buf.append(XX_FORMAT.format(cal.get(Calendar.MONTH) + 1)); buf.append('-'); // day (DD) buf.append(XX_FORMAT.format(cal.get(Calendar.DAY_OF_MONTH))); buf.append('T'); // hour (hh) buf.append(XX_FORMAT.format(cal.get(Calendar.HOUR_OF_DAY))); buf.append(':'); // minute (mm) buf.append(XX_FORMAT.format(cal.get(Calendar.MINUTE))); buf.append(':'); // second (ss) buf.append(XX_FORMAT.format(cal.get(Calendar.SECOND))); buf.append('.'); // millisecond (SSS) buf.append(XXX_FORMAT.format(cal.get(Calendar.MILLISECOND))); // time zone designator (+/-hh:mm) buf.append(getTimeZone(cal)); return buf.toString(); } /** * Returns the time zone designator in a ISO6601-compliant format from the * given <code>Calendar</code> value. * * @param cal * The Calendar to be formatted. * @return The time zone designator from the given Calendar. */ public static String getTimeZone(final Calendar cal) { StringBuilder buf = new StringBuilder(); TimeZone tz = cal.getTimeZone(); // determine offset of timezone from UTC (incl. daylight saving) int offset = tz.getOffset(cal.getTimeInMillis()); int hours = Math.abs((offset / (60 * 1000)) / 60); int minutes = Math.abs((offset / (60 * 1000)) % 60); buf.append(offset < 0 ? '-' : '+'); buf.append(XX_FORMAT.format(hours)); buf.append(':'); buf.append(XX_FORMAT.format(minutes)); return buf.toString(); } }