Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Equivalent of following methods in Joda-Time DateFormatSymbols #getMonths() /** * Gets month strings. For example: "January", "February", etc. * @return the month strings. */ public String[] getMonths()
How can I get the month in android,as a String, as May,September,November,etc...and not like 5,9,11,...Can anyone help me? I have this : int month = c.get(Calendar.MONTH) but I need the name of the month.
I am developing an application which needs the current date from the device, but it only needs day-of-week, month, and day-of-month, like Friday October 14 I have tried this code with Calendar. How do I convert Date to String? Is this possible to get date in this format? Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT")); System.out.println("Date" + c.getTime()); and my out...
I've got the following chart made with JFreeChart: Is it possible (and if it is how) to extend the dates on the x-axis so that they contain the year, eg. 4-II-2010, 5-II-2010, ..., 6-III-2010?
I want to display current date as 00:50:32 A Here is my code Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a"); String time = sdf.format(date); System.out.println("time : " + time); But it print as: time : 00:50:32 AM I tried both HH:mm:ss a and HH:mm:ss aaa, but results are the same.
I would like to do something more clever than: String[] strDaysEn = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thusday", "Friday", "Saturday" }; String[] strDaysFr = new String[] { "Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi" }; or <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="week_days"> <...
I am working on an application and I need to return Arabic/French month name and day name, depending on the local I am using. For example, if the local is "Ar" i need the month to be فبراير for February. If local is "Fr" month name should be Février. I am currently working around it and reading the month and day names from different .properties files. Now, my question is: is there any way to r...
  /*
   * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code 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
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 /*
  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  *
  *   The original version of this source code and documentation is copyrighted
  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  * materials are provided under terms of a License Agreement between Taligent
  * and Sun. This technology is protected by multiple US and International
  * patents. This notice and attribution to Taligent may not be removed.
  *   Taligent is a registered trademark of Taligent, Inc.
  *
  */
 
 package java.text;
 
 import java.util.List;
DateFormatSymbols is a public class for encapsulating localizable date-time formatting data, such as the names of the months, the names of the days of the week, and the time zone data. DateFormat and SimpleDateFormat both use DateFormatSymbols to encapsulate this information.

Typically you shouldn't use DateFormatSymbols directly. Rather, you are encouraged to create a date-time formatter with the DateFormat class's factory methods: getTimeInstance, getDateInstance, or getDateTimeInstance. These methods automatically create a DateFormatSymbols for the formatter so that you don't have to. After the formatter is created, you may modify its format pattern using the setPattern method. For more information about creating formatters using DateFormat's factory methods, see DateFormat.

If you decide to create a date-time formatter with a specific format pattern for a specific locale, you can do so with:

 new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)).
 

DateFormatSymbols objects are cloneable. When you obtain a DateFormatSymbols object, feel free to modify the date-time formatting data. For instance, you can replace the localized date-time format pattern characters with the ones that you feel easy to remember. Or you can change the representative cities to your favorite ones.

New DateFormatSymbols subclasses may be added to support SimpleDateFormat for date-time formatting for additional locales.

Author(s):
Chen-Lieh Huang
See also:
DateFormat
SimpleDateFormat
java.util.SimpleTimeZone
public class DateFormatSymbols implements SerializableCloneable {

    
Construct a DateFormatSymbols object by loading format data from resources for the default locale. This constructor can only construct instances for the locales supported by the Java runtime environment, not for those supported by installed DateFormatSymbolsProvider implementations. For full locale coverage, use the getInstance method.

Throws:
java.util.MissingResourceException if the resources for the default locale cannot be found or cannot be loaded.
See also:
getInstance()
    public DateFormatSymbols()
    {
        initializeData(Locale.getDefault());
    }

    
Construct a DateFormatSymbols object by loading format data from resources for the given locale. This constructor can only construct instances for the locales supported by the Java runtime environment, not for those supported by installed DateFormatSymbolsProvider implementations. For full locale coverage, use the getInstance method.

Throws:
java.util.MissingResourceException if the resources for the specified locale cannot be found or cannot be loaded.
See also:
getInstance(java.util.Locale)
    public DateFormatSymbols(Locale locale)
    {
        initializeData(locale);
    }

    
Era strings. For example: "AD" and "BC". An array of 2 strings, indexed by Calendar.BC and Calendar.AD.

Serial:
    String eras[] = null;

    
Month strings. For example: "January", "February", etc. An array of 13 strings (some calendars have 13 months), indexed by Calendar.JANUARY, Calendar.FEBRUARY, etc.

Serial:
    String months[] = null;

    
Short month strings. For example: "Jan", "Feb", etc. An array of 13 strings (some calendars have 13 months), indexed by Calendar.JANUARY, Calendar.FEBRUARY, etc.

Serial:
    String shortMonths[] = null;

    
Weekday strings. For example: "Sunday", "Monday", etc. An array of 8 strings, indexed by Calendar.SUNDAY, Calendar.MONDAY, etc. The element weekdays[0] is ignored.

Serial:
    String weekdays[] = null;

    
Short weekday strings. For example: "Sun", "Mon", etc. An array of 8 strings, indexed by Calendar.SUNDAY, Calendar.MONDAY, etc. The element shortWeekdays[0] is ignored.

Serial:
    String shortWeekdays[] = null;

    
AM and PM strings. For example: "AM" and "PM". An array of 2 strings, indexed by Calendar.AM and Calendar.PM.

Serial:
    String ampms[] = null;

    
Localized names of time zones in this locale. This is a two-dimensional array of strings of size n by m, where m is at least 5. Each of the n rows is an entry containing the localized names for a single TimeZone. Each such row contains (with i ranging from 0..n-1):
  • zoneStrings[i][0] - time zone ID
  • zoneStrings[i][1] - long name of zone in standard time
  • zoneStrings[i][2] - short name of zone in standard time
  • zoneStrings[i][3] - long name of zone in daylight saving time
  • zoneStrings[i][4] - short name of zone in daylight saving time
The zone ID is not localized; it's one of the valid IDs of the TimeZone class that are not custom IDs. All other entries are localized names.

See also:
java.util.TimeZone
Serial:
    String zoneStrings[][] = null;

    
Indicates that zoneStrings is set externally with setZoneStrings() method.
    transient boolean isZoneStringsSet = false;

    
Unlocalized date-time pattern characters. For example: 'y', 'd', etc. All locales use the same these unlocalized pattern characters.
    static final String  patternChars = "GyMdkHmsSEDFwWahKzZ";

    
Localized date-time pattern characters. For example, a locale may wish to use 'u' rather than 'y' to represent years in its date format pattern strings. This string must be exactly 18 characters long, with the index of the characters described by DateFormat.ERA_FIELD, DateFormat.YEAR_FIELD, etc. Thus, if the string were "Xz...", then localized patterns would use 'X' for era and 'z' for year.

Serial:
    String  localPatternChars = null;

    
The locale which is used for initializing this DateFormatSymbols object.

Since:
1.6
Serial:
    Locale locale = null;
    /* use serialVersionUID from JDK 1.1.4 for interoperability */
    static final long serialVersionUID = -5987973545549424702L;

    
Returns an array of all locales for which the getInstance methods of this class can return localized instances. The returned array represents the union of locales supported by the Java runtime and by installed DateFormatSymbolsProvider implementations. It must contain at least a Locale instance equal to Locale.US.

Returns:
An array of locales for which localized DateFormatSymbols instances are available.
Since:
1.6
    public static Locale[] getAvailableLocales() {
        LocaleServiceProviderPool pool=
            LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class);
        return pool.getAvailableLocales();
    }

    
Gets the DateFormatSymbols instance for the default locale. This method provides access to DateFormatSymbols instances for locales supported by the Java runtime itself as well as for those supported by installed DateFormatSymbolsProvider implementations.

Returns:
a DateFormatSymbols instance.
Since:
1.6
    public static final DateFormatSymbols getInstance() {
        return getInstance(Locale.getDefault());
    }

    
Gets the DateFormatSymbols instance for the specified locale. This method provides access to DateFormatSymbols instances for locales supported by the Java runtime itself as well as for those supported by installed DateFormatSymbolsProvider implementations.

Parameters:
locale the given locale.
Returns:
a DateFormatSymbols instance.
Throws:
java.lang.NullPointerException if locale is null
Since:
1.6
    public static final DateFormatSymbols getInstance(Locale locale) {
        // Check whether a provider can provide an implementation that's closer
        // to the requested locale than what the Java runtime itself can provide.
        LocaleServiceProviderPool pool =
            LocaleServiceProviderPool.getPool(DateFormatSymbolsProvider.class);
        if (pool.hasProviders()) {
            DateFormatSymbols providersInstance = pool.getLocalizedObject(
                                .locale);
            if (providersInstance != null) {
                return providersInstance;
            }
        }
        return new DateFormatSymbols(locale);
    }

    
Gets era strings. For example: "AD" and "BC".

Returns:
the era strings.
    public String[] getEras() {
        return duplicate();
    }

    
Sets era strings. For example: "AD" and "BC".

Parameters:
newEras the new era strings.
    public void setEras(String[] newEras) {
         = duplicate(newEras);
    }

    
Gets month strings. For example: "January", "February", etc.

Returns:
the month strings.
    public String[] getMonths() {
        return duplicate();
    }

    
Sets month strings. For example: "January", "February", etc.

Parameters:
newMonths the new month strings.
    public void setMonths(String[] newMonths) {
         = duplicate(newMonths);
    }

    
Gets short month strings. For example: "Jan", "Feb", etc.

Returns:
the short month strings.
    public String[] getShortMonths() {
        return duplicate();
    }

    
Sets short month strings. For example: "Jan", "Feb", etc.

Parameters:
newShortMonths the new short month strings.
    public void setShortMonths(String[] newShortMonths) {
         = duplicate(newShortMonths);
    }

    
Gets weekday strings. For example: "Sunday", "Monday", etc.

Returns:
the weekday strings. Use Calendar.SUNDAY, Calendar.MONDAY, etc. to index the result array.
    public String[] getWeekdays() {
        return duplicate();
    }

    
Sets weekday strings. For example: "Sunday", "Monday", etc.

Parameters:
newWeekdays the new weekday strings. The array should be indexed by Calendar.SUNDAY, Calendar.MONDAY, etc.
    public void setWeekdays(String[] newWeekdays) {
         = duplicate(newWeekdays);
    }

    
Gets short weekday strings. For example: "Sun", "Mon", etc.

Returns:
the short weekday strings. Use Calendar.SUNDAY, Calendar.MONDAY, etc. to index the result array.
    public String[] getShortWeekdays() {
        return duplicate();
    }

    
Sets short weekday strings. For example: "Sun", "Mon", etc.

Parameters:
newShortWeekdays the new short weekday strings. The array should be indexed by Calendar.SUNDAY, Calendar.MONDAY, etc.
    public void setShortWeekdays(String[] newShortWeekdays) {
         = duplicate(newShortWeekdays);
    }

    
Gets ampm strings. For example: "AM" and "PM".

Returns:
the ampm strings.
    public String[] getAmPmStrings() {
        return duplicate();
    }

    
Sets ampm strings. For example: "AM" and "PM".

Parameters:
newAmpms the new ampm strings.
    public void setAmPmStrings(String[] newAmpms) {
         = duplicate(newAmpms);
    }

    
Gets time zone strings. Use of this method is discouraged; use TimeZone.getDisplayName() instead.

The value returned is a two-dimensional array of strings of size n by m, where m is at least 5. Each of the n rows is an entry containing the localized names for a single TimeZone. Each such row contains (with i ranging from 0..n-1):

  • zoneStrings[i][0] - time zone ID
  • zoneStrings[i][1] - long name of zone in standard time
  • zoneStrings[i][2] - short name of zone in standard time
  • zoneStrings[i][3] - long name of zone in daylight saving time
  • zoneStrings[i][4] - short name of zone in daylight saving time
The zone ID is not localized; it's one of the valid IDs of the TimeZone class that are not custom IDs. All other entries are localized names. If a zone does not implement daylight saving time, the daylight saving time names should not be used.

If setZoneStrings has been called on this DateFormatSymbols instance, then the strings provided by that call are returned. Otherwise, the returned array contains names provided by the Java runtime and by installed TimeZoneNameProvider implementations.

Returns:
the time zone strings.
See also:
setZoneStrings(java.lang.String[][])
    public String[][] getZoneStrings() {
        return getZoneStringsImpl(true);
    }

    
Sets time zone strings. The argument must be a two-dimensional array of strings of size n by m, where m is at least 5. Each of the n rows is an entry containing the localized names for a single TimeZone. Each such row contains (with i ranging from 0..n-1):
  • zoneStrings[i][0] - time zone ID
  • zoneStrings[i][1] - long name of zone in standard time
  • zoneStrings[i][2] - short name of zone in standard time
  • zoneStrings[i][3] - long name of zone in daylight saving time
  • zoneStrings[i][4] - short name of zone in daylight saving time
The zone ID is not localized; it's one of the valid IDs of the TimeZone class that are not custom IDs. All other entries are localized names.

Parameters:
newZoneStrings the new time zone strings.
Throws:
java.lang.IllegalArgumentException if the length of any row in newZoneStrings is less than 5
java.lang.NullPointerException if newZoneStrings is null
See also:
getZoneStrings()
    public void setZoneStrings(String[][] newZoneStrings) {
        String[][] aCopy = new String[newZoneStrings.length][];
        for (int i = 0; i < newZoneStrings.length; ++i) {
            if (newZoneStrings[i].length < 5) {
                throw new IllegalArgumentException();
            }
            aCopy[i] = duplicate(newZoneStrings[i]);
        }
         = aCopy;
         = true;
    }

    
Gets localized date-time pattern characters. For example: 'u', 't', etc.

Returns:
the localized date-time pattern characters.
    public String getLocalPatternChars() {
        return new String();
    }

    
Sets localized date-time pattern characters. For example: 'u', 't', etc.

Parameters:
newLocalPatternChars the new localized date-time pattern characters.
    public void setLocalPatternChars(String newLocalPatternChars) {
         = new String(newLocalPatternChars);
    }

    
Overrides Cloneable
    public Object clone()
    {
        try
        {
            DateFormatSymbols other = (DateFormatSymbols)super.clone();
            copyMembers(thisother);
            return other;
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    }

    
Override hashCode. Generates a hash code for the DateFormatSymbols object.
    public int hashCode() {
        int hashcode = 0;
        String[][] zoneStrings = getZoneStringsWrapper();
        for (int index = 0; index < zoneStrings[0].length; ++index)
            hashcode ^= zoneStrings[0][index].hashCode();
        return hashcode;
    }

    
Override equals
    public boolean equals(Object obj)
    {
        if (this == objreturn true;
        if (obj == null || getClass() != obj.getClass()) return false;
        DateFormatSymbols that = (DateFormatSymbolsobj;
        return (Arrays.equals(that.eras)
                && Arrays.equals(that.months)
                && Arrays.equals(that.shortMonths)
                && Arrays.equals(that.weekdays)
                && Arrays.equals(that.shortWeekdays)
                && Arrays.equals(that.ampms)
                && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())
                && (( != null
                  && .equals(that.localPatternChars))
                 || ( == null
                  && that.localPatternChars == null)));
    }
    // =======================privates===============================

    
Useful constant for defining time zone offsets.
    static final int millisPerHour = 60*60*1000;

    
Cache to hold the FormatData and TimeZoneNames ResourceBundles of a Locale.
    private static Hashtable cachedLocaleData = new Hashtable(3);

    
Look up resource data for the desiredLocale in the cache; update the cache if necessary.
    private static ResourceBundle cacheLookup(Locale desiredLocale) {
    ResourceBundle rb;
    SoftReference data
        = (SoftReference).get(desiredLocale);
    if (data == null) {
        rb = LocaleData.getDateFormatData(desiredLocale);
        data = new SoftReference(rb);
        .put(desiredLocaledata);
    } else {
        if ((rb = (ResourceBundle)data.get()) == null) {
        rb = LocaleData.getDateFormatData(desiredLocale);
        data = new SoftReference(rb);
        }
    }
    return rb;
    }
    private void initializeData(Locale desiredLocale) {
        int i;
        ResourceBundle resource = cacheLookup(desiredLocale);
        // FIXME: cache only ResourceBundle. Hence every time, will do
        // getObject(). This won't be necessary if the Resource itself
        // is cached.
         = (String[])resource.getObject("Eras");
         = resource.getStringArray("MonthNames");
         = resource.getStringArray("MonthAbbreviations");
        String[] lWeekdays = resource.getStringArray("DayNames");
         = new String[8];
        [0] = "";  // 1-based
        for (i=0; i<lWeekdays.lengthi++)
            [i+1] = lWeekdays[i];
        String[] sWeekdays = resource.getStringArray("DayAbbreviations");
         = new String[8];
        [0] = "";  // 1-based
        for (i=0; i<sWeekdays.lengthi++)
            [i+1] = sWeekdays[i];
         = resource.getStringArray("AmPmMarkers");
         = resource.getString("DateTimePatternChars");
         = desiredLocale;
    }

    
Package private: used by SimpleDateFormat Gets the index for the given time zone ID to obtain the time zone strings for formatting. The time zone ID is just for programmatic lookup. NOT LOCALIZED!!!

Parameters:
ID the given time zone ID.
Returns:
the index of the given time zone ID. Returns -1 if the given time zone ID can't be located in the DateFormatSymbols object.
See also:
java.util.SimpleTimeZone
    final int getZoneIndex (String ID)
    {
        String[][] zoneStrings = getZoneStringsWrapper();
        for (int index=0; index<zoneStrings.lengthindex++)
        {
            if (ID.equalsIgnoreCase(zoneStrings[index][0])) return index;
        }
        return -1;
    }

    
Wrapper method to the getZoneStrings(), which is called from inside the java.text package and not to mutate the returned arrays, so that it does not need to create a defensive copy.
    final String[][] getZoneStringsWrapper() {
        if (isSubclassObject()) {
            return getZoneStrings();
        } else {
            return getZoneStringsImpl(false);
        }
    }
    private final String[][] getZoneStringsImpl(boolean needsCopy) {
        if ( == null) {
             = TimeZoneNameUtility.getZoneStrings();
        }
        if (needsCopy) {
            String[][] aCopy = new String[.][];
            for (int i = 0; i < .; ++i) {
                aCopy[i] = duplicate([i]);
            }
            return aCopy;
        } else {
            return ;
        }
    }
    private final boolean isSubclassObject() {
        return !getClass().getName().equals("java.text.DateFormatSymbols");
    }

    
Clones an array of Strings.

Parameters:
srcArray the source array to be cloned.
count the number of elements in the given source array.
Returns:
a cloned array.
    private final String[] duplicate(String[] srcArray)
    {
        String[] dstArray = new String[srcArray.length];
        System.arraycopy(srcArray, 0, dstArray, 0, srcArray.length);
        return dstArray;
    }

    
Clones all the data members from the source DateFormatSymbols to the target DateFormatSymbols. This is only for subclasses.

Parameters:
src the source DateFormatSymbols.
dst the target DateFormatSymbols.
    private final void copyMembers(DateFormatSymbols srcDateFormatSymbols dst)
    {
        dst.eras = duplicate(src.eras);
        dst.months = duplicate(src.months);
        dst.shortMonths = duplicate(src.shortMonths);
        dst.weekdays = duplicate(src.weekdays);
        dst.shortWeekdays = duplicate(src.shortWeekdays);
        dst.ampms = duplicate(src.ampms);
        if (src.zoneStrings != null) {
            if (dst.zoneStrings == null) {
                dst.zoneStrings = new String[src.zoneStrings.length][];
            }
            for (int i = 0; i < dst.zoneStrings.length; ++i) {
                dst.zoneStrings[i] = duplicate(src.zoneStrings[i]);
            }
        } else {
            dst.zoneStrings = null;
        }
        dst.localPatternChars = new String (src.localPatternChars);
    }

    
Compares the equality of the two arrays of String.

Parameters:
current this String array.
other that String array.
    private final boolean equals(String[] currentString[] other)
    {
        int count = current.length;
        for (int i = 0; i < count; ++i)
            if (!current[i].equals(other[i]))
                return false;
        return true;
    }

    
Write out the default serializable data, after ensuring the zoneStrings field is initialized in order to make sure the backward compatibility.

Since:
1.6
    private void writeObject(ObjectOutputStream streamthrows IOException {
        if ( == null) {
             = TimeZoneNameUtility.getZoneStrings();
        }
        stream.defaultWriteObject();
    }

    
Obtains a DateFormatSymbols instance from a DateFormatSymbolsProvider implementation.
    private static class DateFormatSymbolsGetter
                                                                   DateFormatSymbols> {
        private static final DateFormatSymbolsGetter INSTANCE =
            new DateFormatSymbolsGetter();
        public DateFormatSymbols getObject(DateFormatSymbolsProvider dateFormatSymbolsProvider,
                                Locale locale,
                                String key,
                                Object... params) {
            assert params.length == 0;
            return dateFormatSymbolsProvider.getInstance(locale);
        }
    }
New to GrepCode? Check out our FAQ X