|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjodd.datetime.JDateTime
Universal all-in-one date and time class that uses Astronomical Julian Dates for time calculations. Guarantied precision is up to 0.001 sec.
JDateTime
contains date/time information for current day. By
default, behaviour and formats are set to ISO standard, although this may
be changed.
JDateTime
can be set in many different ways by using setXxx()
methods or equivalent constructors. Moreover, it may be set from an instance
of any available java date-time class. This functionality can be easily
enhanced for any custom date/time class. Furthermore, JDateTime
can be used for generation of any such date/time class.
Rolling dates with JDateTime
is easy. For this
JDateTime
contains many addXxx() methods. Time can be added
or subtracted with any value or more values at once. All combinations are
valid. Calculations also performs month fixes by default.
JDateTime
behaviour is set by several attributes (or
parameters). Each one contains 2 values: one is the default value, used by
all instances of JDateTime
and the other one is just for a
specific instance of JDateTime
. This means that it is
possible to set behavior of all instances at once or of one particular
instance.
Bellow is the list of behavior attributes:
JDateTime
will
additionally fix all time adding and fix the date. For example, adding
one month to 2003-01-31 will give 2003-02-28 and not 2003-03-03.
By default, monthFix is turned on and set to true
.
JdtFormatter
parses and uses this template.JDateTime
works significantly faster then java's Calendar
s. Since
JDateTime
doesn't use lazy initialization, setXxx() method is
slower. However, this doesn't have much effect to the global performances:
settings are not used without gettings. As soon as any other method is
used (getXxx() or addXxx()) performances of JDateTime
becomes
significantly better.
Field Summary | |
static JulianDateStamp |
JD_1970
Julian Date for 1970-01-01. |
Constructor Summary | |
JDateTime()
Constructor that sets current local date and time. |
|
JDateTime(DateTimeStamp dts)
Contructs JDateTime from DateTimeStamp . |
|
JDateTime(int year,
int month,
int day)
Constructor that sets just date. |
|
JDateTime(int year,
int month,
int day,
int hour,
int minute,
double second)
Constructor that set date and time. |
|
JDateTime(JulianDateStamp jds)
Contructs JDateTime from JulianDateStamp . |
|
JDateTime(long milis)
Constructor that sets current time specified as time in milliseconds, from the midnight, January 1, 1970 UTC. |
|
JDateTime(java.lang.Object o)
Contructs JDateTime from various objects. |
Method Summary | |
void |
add(int year,
int month,
int day)
Adds date, leaving time unchanged, with preset value of monthFix. |
void |
add(int year,
int month,
int day,
boolean monthFix)
Adds date, leaving time unchanged. |
void |
add(int year,
int month,
int day,
int hour,
int minute,
double second)
Performs time adding with preset value of monthFix attribute. |
void |
add(int year,
int month,
int day,
int hour,
int minute,
double second,
boolean monthFix)
Adds time to current time. |
void |
addDay(int d)
Adds days, with preset value of monthFix. |
void |
addDay(int d,
boolean monthFix)
Adds days. |
void |
addHour(int h)
Adds hours, with preset value of monthFix. |
void |
addHour(int h,
boolean monthFix)
Adds hours. |
void |
addMillisecond(int ms)
Adds milliseconds, with preset value of monthFix. |
void |
addMillisecond(int ms,
boolean monthFix)
Adds milliseconds. |
void |
addMinute(int m)
Adds minutes, with preset value of monthFix. |
void |
addMinute(int m,
boolean monthFix)
Adds minutes. |
void |
addMonth(int m)
Adds month, with preset value of monthFix. |
void |
addMonth(int m,
boolean monthFix)
Adds month. |
void |
addSecond(double s)
Adds seconds, with preset value of monthFix. |
void |
addSecond(double s,
boolean monthFix)
Adds seconds. |
void |
addTime(int hour,
int minute,
double second)
Adds time, with preset value of monthFix. |
void |
addTime(int hour,
int minute,
double second,
boolean monthFix)
Adds time. |
void |
addYear(int y)
Adds year, with preset value of monthFix. |
void |
addYear(int y,
boolean monthFix)
Adds year. |
int |
compareTo(java.lang.Object gt)
Compares current JDateTime object with another one, up to 1 millisecond. |
java.lang.String |
get()
Get current date/time in default format. |
java.lang.String |
get(java.lang.String template)
Get current date/time in specified format. |
java.util.Calendar |
getCalendarInstance()
Shortcut for getInstance() that builds and returns Calendar
instance. |
java.util.Date |
getDateInstance()
Shortcut for getInstance() that builds and returns Date
instance. |
DateTimeStamp |
getDateTimeStamp()
Returns new object generic time stamp. |
int |
getDay()
Returns current day of month. |
int |
getDayOfMonth()
Returns current day of month. |
int |
getDayOfWeek()
Returns current day of week. |
int |
getDayOfYear()
Returns current day of year. |
int |
getFirstDayOfWeek()
Returns the first day of the week. |
java.lang.String |
getFormatTemplate()
Returns actual format template. |
java.util.GregorianCalendar |
getGregorianCalendarInstace()
Shortcut for getInstance() that builds and returns GregorianCalendar instance. |
int |
getHour()
Returns current hour. |
java.lang.Object |
getInstance(java.lang.Class c)
Stores time to a new instance of desired class, by using converters. |
JDateTime |
getJDateTimeInstance()
Shortcut for getInstance() that builds and returns new JDateTime instance with the date/time set as the original
one. |
JulianDateStamp |
getJulianDate()
Returns Julian Date stamp. |
int |
getMillisecond()
Returns current milliseconds. |
int |
getMinimalDaysInFirstWeek()
Returns minimal number of days of the first week. |
int |
getMinute()
Returns current minutes. |
int |
getMonth()
Returns current month. |
boolean |
getMonthFix()
Returns actual mont fix value. |
int |
getMonthLength()
Returns the length of the current month in days. |
int |
getMonthLength(int m)
Returns the length of the specified month in days. |
int |
getMustHaveDayOfFirstWeek()
Returns must have day of the 1st week. |
JdtNames |
getNames()
Returns actual date time names. |
double |
getSecond()
Return current secodns. |
java.sql.Date |
getSqlDateInstace()
Shortcut for getInstance() that builds and returns java.sql.Date instance. |
java.sql.Timestamp |
getSqlTimestampInstance()
Shortcut for getInstance() that builds and returns java.sql.Timestamp instance. |
int |
getWeekOfMonth()
|
int |
getWeekOfYear()
Returns current week of year. |
int |
getYear()
Returns current year. |
boolean |
isLeap()
Returns current leap year flag. |
static boolean |
isValid(java.lang.String s)
Checks if some string represents a valid date. |
static boolean |
isValid(java.lang.String s,
java.lang.String template)
Checks if some string represents a valid date. |
void |
loadFrom(java.lang.Object o)
Loads time from an object by using registered converters. |
static void |
register(java.lang.Class c,
JdtConverter gtc)
Registers a JdtConvertor for a specific class. |
static void |
registerDefaults()
Registers default converters. |
static void |
resetDefaultFormatTemplate()
Resets default format to JDateTime defaults format: YYYY-MM-DD hh:mm:s.m |
static void |
resetDefaultMonthFix()
Resets default month fix value to true . |
static void |
resetDefaultNames()
Resets default date time names to English. |
static void |
resetDefaultWeekDefinition()
Resets default week definition. |
void |
resetFormatTemplate()
Resets current format template to default value. |
void |
resetMonthFix()
Resets month fix value to default one. |
void |
resetNames()
Reset current names to default values. |
void |
resetWeekDefintion()
Resets week definition. |
void |
set()
Sets current local date and time. |
void |
set(int year,
int month,
int day)
Sets date, time is set to midnight (00:00:00.000). |
void |
set(int year,
int month,
int day,
int hour,
int minute,
double second)
Core method that sets date and time. |
void |
set(long milis)
Sets the time based on current time in milliseconds. |
void |
set(java.lang.String s)
Sets date/time from a string and default template. |
void |
set(java.lang.String s,
java.lang.String template)
Sets date/time from a string and specified template. |
void |
setDate(int year,
int month,
int day)
Sets date, time remains unchanged. |
void |
setDateTimeStamp(DateTimeStamp t)
Sets the new timestamp. |
void |
setDay(int d)
Sets current day. |
static void |
setDefaultFormatTemplate(java.lang.String newFormatTemplate)
Sets default format template. |
static void |
setDefaultFormatter(JdtFormatter f)
Sets default formatter |
static void |
setDefaultMonthFix(boolean v)
Sets default month fix value. |
static void |
setDefaultNames(JdtNames newNames)
Sets default JDateTime names. |
static void |
setDefaultWeekDefinition(int start,
int must)
Defines default week. |
static void |
setDefaultWeekDefinitionAlt(int start,
int min)
Defines default week alternatively. |
void |
setFormatTemplate(java.lang.String newFormat)
Sets current format template. |
void |
setHour(int h)
Set current hour. |
void |
setJulianDate(JulianDateStamp jds)
Sets current Julian Date. |
void |
setMillisecond(int m)
Sets current millisecond. |
void |
setMinute(int m)
Set current minute. |
void |
setMonth(int m)
Sets current month. |
void |
setMonthFix(boolean v)
Sets month fix value. |
void |
setNames(JdtNames newNames)
Sets current date time names. |
void |
setSecond(double s)
Sets current second and millisecond. |
void |
setSecond(int s)
Sets current second. |
void |
setTime(int hour,
int minute,
double second)
Sets time, date is unchanged. |
void |
setTimeStamp(DateTimeStamp t)
Deprecated. use setDateTimeStamp() instead |
void |
setWeekDefinition(int start,
int must)
Defines week. |
void |
setWeekDefinitionAlt(int start,
int min)
Defines week alternatively. |
void |
setYear(int y)
Sets current year. |
void |
storeTo(java.lang.Object o)
Stores time to an existing instance of desired class, by using converters. |
java.lang.String |
toString()
Returns date time string in YYYY-MM-DD hh:mm:s.m format. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final JulianDateStamp JD_1970
Constructor Detail |
public JDateTime(int year, int month, int day, int hour, int minute, double second)
year
- year to setmonth
- month to setday
- day to sethour
- hours to setminute
- minutes to setsecond
- seconds to setset(int, int, int, int, int, double)
public JDateTime(int year, int month, int day)
year
- year to setmonth
- month to setday
- day to setset(int, int, int)
public JDateTime(long milis)
milis
- time in milliseconds, from the midnight, January 1, 1970 UTCset(long )
public JDateTime()
public JDateTime(java.lang.Object o)
JDateTime
from various objects.
o
- object from where to read date infopublic JDateTime(DateTimeStamp dts)
JDateTime
from DateTimeStamp
.
dts
- date time stamppublic JDateTime(JulianDateStamp jds)
JDateTime
from JulianDateStamp
.
jds
- date time stampMethod Detail |
public void setJulianDate(JulianDateStamp jds)
jds
- current julian datepublic void set(int year, int month, int day, int hour, int minute, double second)
year
- year to setmonth
- month to setday
- day to sethour
- hour to setminute
- minute to setsecond
- second to setpublic JulianDateStamp getJulianDate()
public int compareTo(java.lang.Object gt)
compareTo
in interface java.lang.Comparable
gt
- JDateTime to compare
public void add(int year, int month, int day, int hour, int minute, double second, boolean monthFix)
There are 2 different kind of addings, when months are added:
year
- delta yearmonth
- delta monthday
- delta dayshour
- delta hoursminute
- delta minutessecond
- delta secondsmonthFix
- true
for month fixing, false
otherwisepublic void add(int year, int month, int day, int hour, int minute, double second)
year
- delta yearmonth
- delta monthday
- delta dayshour
- delta hoursminute
- delta minutessecond
- delta secondsadd(int, int, int, int, int, double, boolean)
public void add(int year, int month, int day, boolean monthFix)
year
- years to addmonth
- months to addday
- days to addmonthFix
- true
for month fixing, false
otherwiseadd(int, int, int, int, int, double, boolean)
public void add(int year, int month, int day)
year
- years to addmonth
- months to addday
- days to addadd(int, int, int, boolean)
public void addTime(int hour, int minute, double second, boolean monthFix)
hour
- hours to addminute
- minutes to addsecond
- seconds to addmonthFix
- true
for month fixing, false
otherwiseadd(int, int, int, int, int, double)
public void addTime(int hour, int minute, double second)
hour
- hours to addminute
- minutes to addsecond
- seconds to addaddTime(int, int, double, boolean)
public void addYear(int y, boolean monthFix)
y
- year to addmonthFix
- true
for month fixing, false
otherwisepublic void addYear(int y)
y
- year to addpublic void addMonth(int m, boolean monthFix)
m
- month to addmonthFix
- true
for month fixing, false
otherwisepublic void addMonth(int m)
m
- month to addpublic void addDay(int d, boolean monthFix)
d
- days to addmonthFix
- true
for month fixing, false
otherwisepublic void addDay(int d)
d
- days to addpublic void addHour(int h, boolean monthFix)
h
- hours to addmonthFix
- true
for month fixing, false
otherwisepublic void addHour(int h)
h
- hours to addpublic void addMinute(int m, boolean monthFix)
m
- minutes to add.monthFix
- true
for month fixing, false
otherwisepublic void addMinute(int m)
m
- minutes to add.public void addSecond(double s, boolean monthFix)
s
- seconds to addmonthFix
- true
for month fixing, false
otherwisepublic void addSecond(double s)
s
- seconds to addpublic void addMillisecond(int ms, boolean monthFix)
ms
- miliseconds to addmonthFix
- true
for month fixing, false
otherwisepublic void addMillisecond(int ms)
ms
- miliseconds to addpublic void set(int year, int month, int day)
year
- year to setmonth
- month to setday
- day to setpublic void setTime(int hour, int minute, double second)
hour
- hours to setminute
- minutes to setsecond
- secnds to setpublic void setDate(int year, int month, int day)
year
- yearmonth
- monthday
- daypublic void set(long milis)
milis
- time in milliseconds, from the midnight, January 1, 1970 UTCpublic void setYear(int y)
y
- year to setpublic void setMonth(int m)
m
- month to setpublic void setDay(int d)
d
- day to setpublic void setHour(int h)
h
- hour to setpublic void setMinute(int m)
m
- minutes to setpublic void setSecond(double s)
s
- seconds and milliseconds to setpublic void setSecond(int s)
s
- seconds to setpublic void setMillisecond(int m)
m
- milliseconds to setpublic int getYear()
public int getMonth()
public int getDay()
getDayOfMonth()
public int getDayOfMonth()
getDay()
public int getHour()
public int getMinute()
public double getSecond()
public int getMillisecond()
public int getDayOfWeek()
public int getDayOfYear()
public boolean isLeap()
public int getWeekOfYear()
public int getWeekOfMonth()
public int getMonthLength(int m)
public int getMonthLength()
public void set()
public static void registerDefaults()
public static void register(java.lang.Class c, JdtConverter gtc)
JdtConvertor
for a specific class. Convertor
knows how to read (extract) data from an object, but also how to populate
the same object after it creates a new instance of it. Because both reading and creating new instance is required by converter, it is not possible to register a superclass and work with its subclasses. Instead, all classes that would be used for the time converters must be registered separately. Example: java.util.Calendar has a subclass java.util.GregorianCalendar. But it is not possible to have just converter for Calendar - there must be converter for GregorianCalendar, too (of course, if it attended to be used).
c
- class of an object that will be instanced and populated with timegtc
- converterpublic void loadFrom(java.lang.Object o)
o
- object to read time frompublic java.lang.Object getInstance(java.lang.Class c)
c
- class of new object
public java.util.Calendar getCalendarInstance()
Calendar
instance.
getInstance(java.lang.Class)
public java.util.Date getDateInstance()
Date
instance.
getInstance(java.lang.Class)
public java.util.GregorianCalendar getGregorianCalendarInstace()
GregorianCalendar
instance.
getInstance(java.lang.Class)
public java.sql.Date getSqlDateInstace()
java.sql.Date
instance.
getInstance(java.lang.Class)
public java.sql.Timestamp getSqlTimestampInstance()
java.sql.Timestamp
instance.
getInstance(java.lang.Class)
public JDateTime getJDateTimeInstance()
JDateTime
instance with the date/time set as the original
one.
getInstance(java.lang.Class)
public void storeTo(java.lang.Object o)
o
- object where to store time infopublic void setTimeStamp(DateTimeStamp t)
t
- setDateTimeStamp(jodd.datetime.DateTimeStamp)
public void setDateTimeStamp(DateTimeStamp t)
t
- public DateTimeStamp getDateTimeStamp()
public static void setDefaultMonthFix(boolean v)
v
- default month fix valuepublic static void resetDefaultMonthFix()
true
.
public void setMonthFix(boolean v)
v
- month fix valuepublic void resetMonthFix()
public boolean getMonthFix()
public static void setDefaultNames(JdtNames newNames)
newNames
- namespublic static void resetDefaultNames()
public void setNames(JdtNames newNames)
newNames
- public void resetNames()
public JdtNames getNames()
public static void setDefaultFormatTemplate(java.lang.String newFormatTemplate)
newFormatTemplate
- public static void resetDefaultFormatTemplate()
public void setFormatTemplate(java.lang.String newFormat)
newFormat
- public void resetFormatTemplate()
public java.lang.String getFormatTemplate()
public static void setDefaultFormatter(JdtFormatter f)
f
- formatter instancepublic java.lang.String get(java.lang.String template)
template
- format template
public java.lang.String get()
public void set(java.lang.String s, java.lang.String template)
s
- string containing date time informationtemplate
- format templatepublic void set(java.lang.String s)
s
- string containing date time informationpublic static void setDefaultWeekDefinition(int start, int must)
start
- first day in weekmust
- must have day of the 1st weekpublic void setWeekDefinition(int start, int must)
start
- first day in weekmust
- must have day of the 1st weekpublic static void resetDefaultWeekDefinition()
public void resetWeekDefintion()
public int getFirstDayOfWeek()
public int getMustHaveDayOfFirstWeek()
public int getMinimalDaysInFirstWeek()
public static void setDefaultWeekDefinitionAlt(int start, int min)
start
- first day in weekmin
- minimal days of weekpublic void setWeekDefinitionAlt(int start, int min)
start
- first day in weekmin
- minimal days of weekpublic static boolean isValid(java.lang.String s)
JDateTime
's default format template.
s
- string
public static boolean isValid(java.lang.String s, java.lang.String template)
s
- stringtemplate
- template
public java.lang.String toString()
get(java.lang.String)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |