Here you can find the source of format(Calendar self, String pattern)
Shortcut for java.text.SimpleDateFormat to output a String representation of this calendar instance.
Parameter | Description |
---|---|
self | this calendar |
pattern | format pattern |
public static String format(Calendar self, String pattern)
//package com.java2s; /*/*from w w w.j ava 2 s. c om*/ * 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.TimeZone; public class Main { /** * <p>Create a String representation of this date according to the given * format pattern. * <p> * <p>For example, if the system timezone is GMT, * <code>new Date(0).format('MM/dd/yy')</code> would return the string * <code>"01/01/70"</code>. See documentation for {@link java.text.SimpleDateFormat} * for format pattern use. * <p> * <p>Note that a new DateFormat instance is created for every * invocation of this method (for thread safety). * * @param self a Date * @param format the format pattern to use according to {@link java.text.SimpleDateFormat} * @return a string representation of this date. * @see java.text.SimpleDateFormat * @since 1.5.7 */ public static String format(Date self, String format) { return new SimpleDateFormat(format).format(self); } /** * <p>Create a String representation of this date according to the given * format pattern and timezone. * <p> * <p>For example: * <code> * def d = new Date(0) * def tz = TimeZone.getTimeZone('GMT') * println d.format('dd/MMM/yyyy', tz) * </code> would return the string * <code>"01/Jan/1970"</code>. See documentation for {@link java.text.SimpleDateFormat} * for format pattern use. * <p> * <p>Note that a new DateFormat instance is created for every * invocation of this method (for thread safety). * * @param self a Date * @param format the format pattern to use according to {@link java.text.SimpleDateFormat} * @param tz the TimeZone to use * @return a string representation of this date. * @see java.text.SimpleDateFormat * @since 1.8.3 */ public static String format(Date self, String format, TimeZone tz) { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(tz); return sdf.format(self); } /** * <p>Shortcut for {@link java.text.SimpleDateFormat} to output a String representation * of this calendar instance. This method respects the Calendar's assigned * {@link java.util.TimeZone}, whereas calling <code>cal.time.format('HH:mm:ss')</code> * would use the system timezone. * <p>Note that Calendar equivalents of <code>date.getDateString()</code> * and variants do not exist because those methods are Locale-dependent. * Although a Calendar may be assigned a {@link java.util.Locale}, that information is * lost and therefore cannot be used to control the default date/time formats * provided by these methods. Instead, the system Locale would always be * used. The alternative is to simply call * {@link java.text.DateFormat#getDateInstance(int, java.util.Locale)} and pass the same Locale * that was used for the Calendar. * * @param self this calendar * @param pattern format pattern * @return String representation of this calendar with the given format. * @see java.text.DateFormat#setTimeZone(java.util.TimeZone) * @see java.text.SimpleDateFormat#format(java.util.Date) * @see #format(java.util.Date, String) * @since 1.6.0 */ public static String format(Calendar self, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(self.getTimeZone()); return sdf.format(self.getTime()); } }