Skip navigation links

Package js.format

User interface formatters and parsers.

See: Description

Package js.format Description

User interface formatters and parsers. Format package convert object values to/from string representation usable to user interfaces. Formatted string should be proper for display on user interfaces and may be subject to locale and time zone adjustments. Note that not all format classes should be both locale and time zone sensitive.

A formatter deals with object value. An object value is an instance of a class that wrap a single value susceptible to be represented as a single string - a sort of data atom, e.g. java.io.File or java.net.URL.

Stock Format Classes

Format package contains ready to use format classes for date/time, currency, percent, numbers and file size. Here is a list with sample output/input to help decide which best fit your needs.
Value Sample
ClassEnglish SampleRomanian Sample
FullDateTimeSunday, March 15, 1964 11:40:00 AM UTC15 martie 1964 11:40:00 UTC
FullDateSunday, March 15, 196415 martie 1964
FullTime11:40:00 AM UTC11:40:00 UTC
LongDateTimeMarch 15, 1964 11:40:00 AM UTC15 martie 1964 11:40:00 UTC
LongDateMarch 15, 196415 martie 1964
LongTime11:40:00 AM UTC11:40:00 UTC
MediumDateTimeMar 15, 1964 11:40:00 AM15.03.1964 11:40:00
MediumDateMar 15, 196415.03.1964
MediumTime11:40:00 AM11:40:00
ShortDateTime3/15/64 11:40 AM15.03.1964 11:40
ShortDate3/15/6415.03.1964
ShortTime11:40 AM11:40
StandardDateTime1964-03-15 14:30:001964-03-15 14:30:00
StandardDate1964-03-151964-03-15
StandardTime14:30:0014:30:00
Currency$12.3412,34 LEI
Percent12.34%12,34%
Number12.3412,34
FileSize2.34KB2,34KB

Usage

In order to invoke Format.format(Object) or Format.parse(String) one needs a formatter instance. If a formatter is locale and or time zone sensitive its constructor takes care to initialize default values. Formatter implementation may provide setters for locale settings and time zone used to overwrite default values. If present, these setters should be called before executing formatting or parsing. Here is a sample usage on a date time formatter.
 LongDate formatter = new LongDate();
 formatter.setLocale(new Locale("ro"));
 formatter.setTimeZone(TimeZone.getTimeZone("EET"));
 ...
 System.out.println(formatter.format(new Date()));
 

User Defined Formatters

For formatters not related to date there is no much support. Custom formatter should only implement Format interface.

For date time formatters, format package provides an abstract base class. This abstract base class implements almost entire date time formatting and parsing logic. User defined implementation should only implement the factory method DateTimeFormat.createDateFormat(Locale).

For your convenience here is an example of a date time formatter from this package.

 public final class StandardDateTime extends DateTimeFormat {
        private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
 
        @Override
        protected DateFormat createDateFormat(Locale locale) {
                return new SimpleDateFormat(STANDARD_FORMAT, locale);
        }
 }
 
Version:
final
Author:
Iulian Rotaru
Skip navigation links

Copyright © 2018. All rights reserved.