Here you can find the source of getTimeString(Date self)
Return a string representation of the time portion of this date according to the locale-specific java.text.DateFormat#MEDIUM default format.
Parameter | Description |
---|---|
self | a Date |
public static String getTimeString(Date self)
//package com.java2s; /*//from w ww . j a v a2 s . c o 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.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class Main { /** * <p>Return a string representation of the time portion of this date * according to the locale-specific {@link java.text.DateFormat#MEDIUM} default format. * For an "en_UK" system locale, this would be <code>HH:MM:ss</code>. * <p> * <p>Note that a new DateFormat instance is created for every * invocation of this method (for thread safety). * * @param self a Date * @return a string representing the time portion of this date * @see java.text.DateFormat#getTimeInstance(int) * @see java.text.DateFormat#MEDIUM * @since 1.5.7 */ public static String getTimeString(Date self) { return DateFormat.getTimeInstance(DateFormat.MEDIUM).format(self); } /** * <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()); } }