|
WebObjects 5.2.3 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.Date java.sql.Timestamp com.webobjects.foundation.NSTimestamp
NSTimestamp objects represent a particular instant in time. Unlike the NSGregorianDate class in previous versions of the Foundation framework, NSTimestamp does not support calendar functions. Refer to the table below to determine which classes to use to perform date operations in WebObjects.
Class | Description |
NSTimestamp | represents an instant in time |
java.util.GregorianCalendar | represents a calendar date |
NSTimeZone | represents a time zone |
NSTimestampFormatter | converts NSTimestamps to strings and vice versa |
This class's absolute reference date is the first instant of 1 January 1970, 00:00:00, GMT.
For the following code segments, you need to import
java.util.*
to access Java's date API.
To break up a NSTimestamp into its component year, month, day, hour,
etc., you can convert it into a java.util.GregorianCalendar and invoke its
get
method on the individual fields:
NSTimestamp myNSTimestamp = new NSTimestamp(); GregorianCalendar myCalendar = new GregorianCalendar(); myCalendar.setTime(myNSTimestamp); int year = myCalendar.get(GregorianCalendar.YEAR); int dayOfMonth = myCalendar.get(GregorianCalendar.DAY_OF_MONTH);
To create an NSTimestamp based on its components, use the following code:
NSTimeZone tz = NSTimeZone.timeZoneWithName("America/Los_Angeles", true); NSTimestamp myNSTimestamp = new NSTimestamp(year, month, day, hour, minute, seconds, tz);
To add an offset in Gregorian units to an NSTimestamp, use the following code:
NSTimestamp myNSTimestamp = timestampByAddingGregorianUnits(year, month, day, hour, minute, seconds);
To create an NSTimestamp representing the current time, use the no-argument constructor:
NSTimestamp currentTime = new NSTimestamp();
The Enterprise Objects Framework expects dates to be represented as NSTimestamp objects. To convert a java.util.Date to an NSTimestamp use:
NSTimestamp myNSTimestamp = new NSTimestamp(myJavaUtilDate);
Since NSTimestamp is a subclass of java.util.Date, you don't need to convert an NSTimestamp into a java.util.Date.
NSTimestamp does not maintain time zone information. All NSTimestamps are 64 bit millisecond offsets since the reference date in the reference time zone. This is January 1, 1970, 00:00:00 GMT.
NSTimestamp.getTime()'s behavior matches that of java.util.Date, NOT java.sql.Timestamp. java.sql.Timestamp.getTime() returns a value with a precision of 1 second. Both NSTimestamp and java.util.Date return a value with a precision of 1 millisecond. java.sql.Timestamps have all fractional seconds in their nanosecond field, while NSTimestamps only place fractions of milliseconds in their nanosecond field.
getTime()
,
NSTimestampFormatter
,
NSTimeZone
,
Date
,
GregorianCalendar
,
Timestamp
,
Serialized FormNested Class Summary | |
static class |
NSTimestamp.IntRef
Deprecated. in the Java Foundation framework. NSTimestamp.IntRef objects act as a containers to hold the values returned by NSTimestamp's gregorianUnitsSinceTimestamp
method and consist solely of an instance variable, value . |
Nested classes inherited from class com.webobjects.foundation.NSCoding |
NSCoding.Support |
Field Summary | |
static NSTimestamp |
DistantFuture
An NSTimestamp that represents a date in the distant future (in terms of centuries). |
static NSTimestamp |
DistantPast
An NSTimestamp that represents a date in the distant past (in terms of centuries). |
Constructor Summary | |
NSTimestamp()
Creates an NSTimestamp representing the current time, accurate to the millisecond. |
|
NSTimestamp(Date date)
Creates an NSTimestamp from a Date. |
|
NSTimestamp(int year,
int month,
int date,
int hour,
int minute,
int second,
TimeZone tz)
Creates an NSTimestamp representing the specified year, month,day, hours, minutes, and seconds in the specified time zone. |
|
NSTimestamp(long time)
Creates an NSTimestamp representing the specified number of milliseconds since the reference date, January 1, 1970, 00:00:00 GMT. |
|
NSTimestamp(long milliseconds,
int nanoseconds)
Creates an NSTimestamp representing the specified number of milliseconds and nanoseconds since the reference date, January 1, 1970, 00:00:00 GMT. |
|
NSTimestamp(long time,
int nanos,
TimeZone tz)
Creates an NSTimestamp object representing the specified number of milliseconds and nanoseconds since the reference date in the specified time zone. |
|
NSTimestamp(long time,
NSTimestamp date)
Creates an NSTimestamp object representing the specified number of milliseconds after the time specified by date . |
|
NSTimestamp(long time,
TimeZone tz)
Creates an NSTimestamp representing the specified number of milliseconds since the reference date in the specified time zone. |
|
NSTimestamp(Timestamp sqlTimestamp)
Creates an NSTimestamp from a java.sql.Timestamp . |
Method Summary | |
Class |
classForCoder()
Conformance to the NSCoding interface |
int |
compare(NSTimestamp ts)
Determines whether this NSTimestamp is before, after, or the same as another NSTimestamp. |
static long |
currentTimeIntervalSinceReferenceDate()
Deprecated. Use (System.currentTimeMillis() / 1000) instead |
long |
dayOfCommonEra()
Deprecated. Use GregorianCalendar instead. |
int |
dayOfMonth()
Deprecated. Use GregorianCalendar instead. |
int |
dayOfWeek()
Deprecated. Use GregorianCalendar instead. |
int |
dayOfYear()
Deprecated. Use GregorianCalendar instead. |
static Object |
decodeObject(NSCoder coder)
Provides an NSTimestamp from data in coder . |
static NSTimestamp |
distantFuture()
Deprecated. Use DistantFuture instead |
static NSTimestamp |
distantPast()
Deprecated. Use DistantPast |
NSTimestamp |
earlierTimestamp(NSTimestamp ts)
Deprecated. Use before instead |
void |
encodeWithCoder(NSCoder coder)
Encodes the receiver using coder . |
int |
getNanos()
|
long |
getTime()
NSTimestamp.getTime()'s behavior matches that of java.util.Date, NOT java.sql.Timestamp. |
void |
gregorianUnitsSinceTimestamp(NSTimestamp.IntRef years,
NSTimestamp.IntRef months,
NSTimestamp.IntRef days,
NSTimestamp.IntRef hours,
NSTimestamp.IntRef minutes,
NSTimestamp.IntRef seconds,
NSTimestamp time)
Deprecated. |
int |
hourOfDay()
Deprecated. Use GregorianCalendar instead. |
NSTimestamp |
laterTimestamp(NSTimestamp ts)
Deprecated. Use after instead |
int |
microsecondOfSecond()
Deprecated. Do not use this method. Java typically uses milliseconds, not microseconds. |
static long |
millisecondsToTimeInterval(long milliseconds)
Deprecated. Use (milliseconds / 1000) instead |
int |
minuteOfHour()
Deprecated. Use GregorianCalendar instead. |
int |
monthOfYear()
Deprecated. Use GregorianCalendar instead. |
int |
secondOfMinute()
Deprecated. Use GregorianCalendar instead. |
void |
setDate(int date)
Deprecated. NSTimestamp objects are immutable. |
void |
setHours(int hours)
Deprecated. NSTimestamp objects are immutable. |
void |
setMinutes(int minutes)
Deprecated. NSTimestamp objects are immutable. |
void |
setMonth(int month)
Deprecated. NSTimestamp objects are immutable. |
void |
setNanos(int nanoseconds)
Deprecated. NSTimestamp objects are immutable. |
void |
setSeconds(int seconds)
Deprecated. NSTimestamp objects are immutable. |
void |
setTime(long time)
Deprecated. NSTimestamp objects are immutable. |
void |
setYear(int year)
Deprecated. NSTimestamp objects are immutable. |
long |
timeIntervalSinceNow()
Deprecated. Use getTime and calculate the delta from System.currentTimeMillis instead |
long |
timeIntervalSinceReferenceDate()
Deprecated. Use (getTime() / 1000) instead. |
long |
timeIntervalSinceTimestamp(NSTimestamp ts)
Deprecated. Use getTime and calculate the delta instead |
static long |
timeIntervalToMilliseconds(long timeInterval)
Deprecated. Use (timeInterval * 1000) instead |
NSTimestamp |
timestampByAddingGregorianUnits(int years,
int months,
int days,
int hours,
int minutes,
int seconds)
Returns an NSTimestamp that is updated with the years, months, days,
hours, minutes , and seconds offsets specified as arguments. |
NSTimestamp |
timestampByAddingTimeInterval(long interval)
Deprecated. Use (new NSTimestamp((interval * 1000) + oldTs.getTime(), oldTs.getNanos())) instead. |
NSTimeZone |
timeZone()
Deprecated. Deprecated in the Java Foundation framework. Do not use this method. All NSTimestamps are in UTC. |
String |
toString()
|
int |
yearOfCommonEra()
Deprecated. Use GregorianCalendar instead. |
Methods inherited from class java.sql.Timestamp |
after, before, compareTo, compareTo, equals, equals, valueOf |
Methods inherited from class java.util.Date |
after, before, clone, compareTo, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTimezoneOffset, getYear, hashCode, parse, toGMTString, toLocaleString, UTC |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final NSTimestamp DistantFuture
public static final NSTimestamp DistantPast
Constructor Detail |
public NSTimestamp()
public NSTimestamp(long time)
time
- milliseconds since the reference datepublic NSTimestamp(long milliseconds, int nanoseconds)
milliseconds
- milliseconds since the reference datenanoseconds
- nanoseconds in addition to milliseconds
public NSTimestamp(long time, NSTimestamp date)
date
.
time
- offset in millisecondsdate
- input date after which the time is to be calculatedpublic NSTimestamp(long time, TimeZone tz)
NSTimestamp does not maintain time zone information. All
NSTimestamps are millisecond offsets since the reference date in
the reference time zone. This method assumes time
is
a time from the tz
time zone, and converts time
into a
time appropriate for NSTimestamp to handle.
time
- time in millisecondstz
- new time zonepublic NSTimestamp(long time, int nanos, TimeZone tz)
NSTimestamp does not maintain time zone information. All
NSTimestamps are millisecond offsets since the reference date in
the reference time zone. This method assumes time
is
a time from the tz
time zone, and converts time
into a
time appropriate for NSTimestamp to handle.
time
- time in millisecondsnanos
- time in nanosecondstz
- time zonepublic NSTimestamp(int year, int month, int date, int hour, int minute, int second, TimeZone tz)
The arguments should specify dates which fall within the range of 1583 C.E. - 4000 C.E. Other dates may suffer from loss of precision, or only remain valid as long as they are not rendered human readable. Dates before DistantPast or after DistantFuture will result in an IllegalArgumentException.
year
- year where DistantPast < year < DistantFuturemonth
- month where 1 <= month <= 12date
- date where 1 <= date <= 31hour
- hour where 0 <= hour <= 23minute
- minute where 0 <= minute <= 59second
- second where 0 <= second <= 59tz
- time zone
IllegalArgumentException
- (year <= DistantPast.yearOfCommonEra()) || (year >= DistantFuture.yearOfCommonEra())
public NSTimestamp(Date date)
date
- Date to use to initialize the new NSTimestampDate
public NSTimestamp(Timestamp sqlTimestamp)
java.sql.Timestamp
.
sqlTimestamp
- Timestamp to use to initialize the new NSTimestampTimestamp
Method Detail |
public Class classForCoder()
classForCoder
in interface NSCoding
NSTimestamp.class
NSCoding
public int compare(NSTimestamp ts)
ts
.
If the receiver is before ts
, this method returns
NSComparator.OrderedAscending.
If the receiver is after ts
, this method returns
NSComparator.OrderedDescending.
If the dates match, this method returns NSComparator.OrderedSame.
ts
- input timestamp
NSComparator.OrderedAscending
when ts
is after this NSTimestamp,
NSComparator.OrderedDescending
when ts
is before this NSTimestamp,
NSComparator.OrderedSame
when ts
and this NSTimestamp are equalNSComparator
public static long currentTimeIntervalSinceReferenceDate()
(System.currentTimeMillis() / 1000)
instead
System.currentTimeMillis()
public long dayOfCommonEra()
GregorianCalendar
instead.
GregorianCalendar
public int dayOfMonth()
GregorianCalendar
instead.
1
through 31
GregorianCalendar
public int dayOfWeek()
GregorianCalendar
instead.
0
through 6
; 0 = SundayGregorianCalendar
public int dayOfYear()
GregorianCalendar
instead.
1
through 366
GregorianCalendar
public static Object decodeObject(NSCoder coder)
coder
.
coder
- NSCoder from which to obtain the timestamp
NSCoding
public static NSTimestamp distantFuture()
DistantFuture
instead
tryLock(NSTimeStamp)
returns false
if the receiver
fails to acquire the lock before the specified date. You can use
the object returned by distantFuture
as the date argument to wait
indefinitely to acquire the lock.
DistantFuture
public static NSTimestamp distantPast()
DistantPast
DistantPast
public NSTimestamp earlierTimestamp(NSTimestamp ts)
before
instead
ts
- the other timestamp
ts
Timestamp.before(java.sql.Timestamp)
public void encodeWithCoder(NSCoder coder)
NSCoding
coder
. Object type information along with an
object's data is stored.
encodeWithCoder
in interface NSCoding
coder
- an NSCoder object that will be used to encode object of classes that
implement this interfaceNSCoder
public int getNanos()
public long getTime()
Date.getTime()
,
Timestamp.getTime()
public void gregorianUnitsSinceTimestamp(NSTimestamp.IntRef years, NSTimestamp.IntRef months, NSTimestamp.IntRef days, NSTimestamp.IntRef hours, NSTimestamp.IntRef minutes, NSTimestamp.IntRef seconds, NSTimestamp time)
timestamp
and returns it in years, months, days, hours, minutes
,
and seconds
. NSTimestamp.IntRef is a local class that contains a
single element: the integer value
.
You can choose any representation you wish for the time difference
by passing null
for the arguments you want to ignore. For example, the
following code fragment computes the difference in months, days,
and years between two dates:
NSTimestamp momsBDay = new NSTimestamp(1936, 1, 8, 7, 30, 0, java.util.TimeZone.getTimeZone("EST")); NSTimestamp dateOfBirth = new NSTimestamp(1965, 12, 7, 17, 25, 0, new NSTimeZone("EST")); NSTimestamp.IntRef years = new NSTimestamp.IntRef(); NSTimestamp.IntRef months = new NSTimestamp.IntRef(); NSTimestamp.IntRef days = new NSTimestamp.IntRef(); dateOfBirth.gregorianUnitsSinceTimestamp(momsBDay, years, months, days, null, null, null)
If you want to express the years in terms of months, you pass null
for the years argument:
This message returns 358 months and 29 days.dateOfBirth.gregorianUnitsSinceTimestamp(momsBDay, null, months, days, null, null, null);
years
- yearmonths
- mongthdays
- dayshours
- hoursminutes
- minutesseconds
- secondstime
- timestamppublic int hourOfDay()
GregorianCalendar
instead.
GregorianCalendar
public NSTimestamp laterTimestamp(NSTimestamp ts)
after
instead
ts
- the other timestamp
ts
Timestamp.after(java.sql.Timestamp)
public int microsecondOfSecond()
0
through 999999
)
of the receivergetTime()
,
GregorianCalendar
public static long millisecondsToTimeInterval(long milliseconds)
(milliseconds / 1000)
instead
milliseconds
. Any fractional part of a second
is truncated.
milliseconds
- time in milliseconds
public int minuteOfHour()
GregorianCalendar
instead.
0
through 59
) of this NSTimestamp.GregorianCalendar
public int monthOfYear()
GregorianCalendar
instead.
1
through 12
) of this NSTimestampGregorianCalendar
public int secondOfMinute()
GregorianCalendar
instead.
0
through 59
) of this NSTimestampGregorianCalendar
public void setDate(int date)
date
- date value to be specifiedpublic void setHours(int hours)
hours
- input hour value to be specifiedpublic void setMinutes(int minutes)
minutes
- input minutes value to be specifiedpublic void setMonth(int month)
month
- input month value to be specifiedpublic void setNanos(int nanoseconds)
nanoseconds
- input nanoseconds value to be specifiedpublic void setSeconds(int seconds)
seconds
- input seconds value to be specifiedpublic void setTime(long time)
time
- input time to be specifiedpublic void setYear(int year)
year
- input year value to be specifiedpublic long timeIntervalSinceNow()
getTime
and calculate the delta from System.currentTimeMillis
instead
getTime()
,
System.currentTimeMillis()
public long timeIntervalSinceReferenceDate()
(getTime() / 1000)
instead.
getTime()
public long timeIntervalSinceTimestamp(NSTimestamp ts)
getTime
and calculate the delta instead
ts
. This value is negative if this NSTimestamp's time is earlier than
time.
ts
- timestamp
getTime()
public static long timeIntervalToMilliseconds(long timeInterval)
(timeInterval * 1000)
instead
timeInterval
represented in seconds.
timeInterval
- time interval in seconds
public NSTimeZone timeZone()
NSTimeZone
public NSTimestamp timestampByAddingGregorianUnits(int years, int months, int days, int hours, int minutes, int seconds)
years, months, days,
hours, minutes
, and seconds
offsets specified as arguments. The offsets
can be positive (future) or negative (past). This method preserves
"clock time" across changes in Daylight Savings Time zones and leap
years. For example, adding one month to an NSTimestamp with a time
of 12 noon correctly maintains time at 12 noon.The following code fragment shows an NSTimestamp created with a date a week later than an existing NSTimestamp.
NSTimestamp now = new NSTimestamp(); NSTimestamp nextWeek = now.timestampByAddingGregorianUnits(0, 0, 7, 0, 0, 0);
years
- input value of yearmonths
- input value of monthsdays
- input value of dayshours
- input value of hoursminutes
- input value of minutesseconds
- input value of seconds
public NSTimestamp timestampByAddingTimeInterval(long interval)
(new NSTimestamp((interval * 1000) + oldTs.getTime(), oldTs.getNanos()))
instead.
interval
- input time interval in seconds
public String toString()
public int yearOfCommonEra()
GregorianCalendar
instead.
GregorianCalendar
|
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 |