|
WebObjects 5.2.3 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.text.Format com.webobjects.foundation.NSTimestampFormatter
Instances of NSTimestampFormatter format NSTimestamps into their textual representations and convert textual representations of dates and times into NSTimestamps. You have a great degree flexibility when expressing dates and times: "Thu 22 Dec 1994" is just as acceptable as "12/22/94".
You can associate a timestamp pattern with a WOString or WOTextField dynamic element. WebObjects uses an NSTimestampFormatter object to perform the appropriate conversions.
You can also create an NSTimestampFormatter with the constructor,
provide a timestamp pattern string, and use the format
and parseObject
methods of java.text.Format
to convert between NSTimestamps and their
textual representations:
NSTimestampFormatter formatter=new NSTimestampFormatter("%m/%d/%y");
String description=formatter.format(myNSTimestamp);
NSTimestampFormatter formatter=new NSTimestampFormatter("%m/%d/%y");
NSTimestamp myNSTimestamp=(NSTimestamp)formatter.parseObject(myTimestampString);
The default pattern for NSTimestampFormatter is %Y-%m-%d %H:%M:%S %Z
.
You can specify a pattern whenever you create a NSTimestampFormatter.
This pattern is a string that contains specifiers that are very similar to
those used in the standard C library function strftime(). When
NSTimestampFormatter converts a date to a string, it uses this pattern.
The date conversion specifiers cover a range of date conventions.
Specifier | Description |
%% | a '%' character |
%a | abbreviated weekday name |
%A | full weekday name |
%b | abbreviated month name |
%B | full month name |
%c | shorthand for "%X %x ", the locale format for date and time |
%d | day of the month as a decimal number (01-31) |
%e | same as %d but does not print the leading 0 for days 1 through 9 |
%F | milliseconds as a decimal number (000-999) |
%H | hour based on a 24-hour clock as a decimal number (00-23) |
%I | hour based on a 12-hour clock as a decimal number (01-12) |
%j | day of the year as a decimal number (001-366) |
%m | month as a decimal number (01-12) |
%M | minute as a decimal number (00-59) |
%p | AM/PM designation for the locale |
%S | second as a decimal number (00-59) |
%w | weekday as a decimal number (0-6), where Sunday is 0 |
%x | date using the date representation for the locale |
%X | time using the time representation for the locale |
%y | year without century (00-99) |
%Y | year with century (such as 1990) |
%Z | time zone name (such as "Europe/Paris" or "PST") |
%z | time zone offset from GMT in hours and minutes (such as "+0200" or "-1200") |
Note that time zone names can not contain spaces. For example, "Pacific Daylight Time" is not a valid time zone name, but "America/Los_Angeles" and "PDT" are both valid.
Alternatively, you can specify the pattern using Sun's date pattern specifiers. See Sun's documentation for the java.text.SimpleDateFormat class for more information.
When you convert a string without a time zone specification to an NSTimestamp, the formatter assumes the time zone is the default parse time zone. An analogous time zone, called the default format time zone, is used when converting an NSTimestamp to a string.
Sometimes you need to give the user a choice of time zones. For example, you might put the time zones in a pop-up list. In such cases, you can use theparseObjectInUTC
method to parse a date string for the UTC time zone. The following code shows how you can
compute the offset from UTC for the particular time zone the user chooses and add it to
the parsed timestamp:
NSTimestamp date = (NSTimestamp)myFormatter.parseInUTC(myString); NSTimeZone tz = NSTimeZone.timeZoneWithName(myTimeZoneName); int offset = tz.secondsFromGMTForTimestamp(date); long milliseconds = date.getTime() - offset * 1000; NSTimestamp dateWithTimeZone = new NSTimestamp(milliseconds);
format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
,
parseObject(java.lang.String, java.text.ParsePosition)
,
defaultParseTimeZone()
,
setDefaultParseTimeZone(com.webobjects.foundation.NSTimeZone)
,
parseObjectInUTC(java.lang.String, java.text.ParsePosition)
,
Serialized FormNested Class Summary |
Nested classes inherited from class java.text.Format |
Format.Field |
Constructor Summary | |
NSTimestampFormatter()
Creates an NSTimestampFormatter with the default pattern. |
|
NSTimestampFormatter(String aPattern)
Creates an NSTimestampFormatter with the pattern string aPattern . |
|
NSTimestampFormatter(String aPattern,
DateFormatSymbols symbols)
Creates an NSTimestampFormatter with the specified pattern using the specified date format symbols. |
Method Summary | |
DateFormatSymbols |
defaultDateFormatSymbols()
Returns the default set of date format symbols. |
NSTimeZone |
defaultFormatTimeZone()
Returns the default time zone this NSTimestampFormatter uses for formatting -- converting an NSTimestamp to a string. |
NSTimeZone |
defaultParseTimeZone()
Returns the default time zone this NSTimestampFormatter uses for parsing -- converting a string to an NSTimestamp. |
StringBuffer |
format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos)
Formats obj to produce a string, appends the string to
toAppendTo , and returns the resulting string. |
Object |
parseObject(String text,
ParsePosition pos)
Parses a string to produce an object. |
Object |
parseObjectInUTC(String text,
ParsePosition pos)
Parses a string to produce an object using UTC as the time zone. |
String |
pattern()
Returns this NSTimestampFormatter's pattern. |
void |
setDefaultFormatTimeZone(NSTimeZone zone)
Sets the default time zone this NSTimestampFormatter uses for formatting -- converting an NSTimestamp into a string -- to zone . |
void |
setDefaultParseTimeZone(NSTimeZone zone)
Sets the default time zone this NSTimestampFormatter uses for parsing -- converting a string to an NSTimestamp -- to zone . |
void |
setPattern(String pattern)
Sets this NSTimestampFormatter's pattern to pattern. |
String |
toString()
Returns a string representation of this NSTimestampFormatter that includes the default format time zone, the default parse time zone, and the pattern. |
Methods inherited from class java.text.Format |
clone, format, formatToCharacterIterator, parseObject |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public NSTimestampFormatter()
public NSTimestampFormatter(String aPattern)
aPattern
.
If aPattern
is null
, this constructor uses the
default pattern. See the Calendar Specifiers table in the
NSTimestampFormatter class description for more information on specifying the pattern string.
aPattern
- the pattern stringpublic NSTimestampFormatter(String aPattern, DateFormatSymbols symbols)
aPattern
is
null
, this constructor uses the default pattern
with the slashes replaced by the appropriate date symbol.See the Calendar Specifiers table in the NSTimestampFormatter class description for more information on specifying the pattern string.
Note If you use this constructor, you must ensure that the Strings
in the symbols
zone strings contain no spaces.
aPattern
- the pattern specifiedsymbols
- the date format symbolsMethod Detail |
public DateFormatSymbols defaultDateFormatSymbols()
public NSTimeZone defaultFormatTimeZone()
defaultTimeZone
.
defaultParseTimeZone()
,
NSTimestamp
public NSTimeZone defaultParseTimeZone()
defaultTimeZone
.
defaultFormatTimeZone()
,
NSTimestamp.timeZone()
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
obj
to produce a string, appends the string to
toAppendTo
, and returns the resulting string.
The pos
parameter specifies an alignment field to place the
formatted object. When the method returns, this parameter contains
the position of the alignment field.
obj
- the object that is formatted to produce a stringtoAppendTo
- StringBuffer to which the string is appendedpos
- the position parameter
IllegalArgumentException
- if obj is not an NSTimestamppublic Object parseObject(String text, ParsePosition pos)
text
- the string that is to be parsed to produce an objectpos
- the position parameter indicating where in text
to begin parsing
defaultParseTimeZone()
public Object parseObjectInUTC(String text, ParsePosition pos)
text
- the string that is to be parsed to produce an objectpos
- the position parameter indicating where in text
to begin parsing
parseObject(java.lang.String, java.text.ParsePosition)
public String pattern()
public void setDefaultFormatTimeZone(NSTimeZone zone)
zone
.
zone
- the default time zone this NSTimestampFormatter uses for formattingsetDefaultParseTimeZone(com.webobjects.foundation.NSTimeZone)
,
NSTimestamp
public void setDefaultParseTimeZone(NSTimeZone zone)
zone
.
zone
- default time zone this NSTimestampFormatter uses for parsingsetDefaultFormatTimeZone(com.webobjects.foundation.NSTimeZone)
,
NSTimestamp
public void setPattern(String pattern)
pattern
- new pattern Stringpublic String toString()
defaultFormatTimeZone()
,
defaultParseTimeZone()
,
pattern()
|
Last updated Thu Oct 21 15:04:16 PDT 2004. | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |