Package org.daypilot.date
Class DateTime
- java.lang.Object
-
- org.daypilot.date.DateTime
-
public class DateTime extends java.lang.Object
A helper class for date/time calculations.
-
-
Constructor Summary
Constructors Constructor Description DateTime()
Creates a new DateTime representing current date and time (now).DateTime(int year, int month, int day)
DateTime(long time)
DateTime(long ticks, java.util.TimeZone tz)
DateTime(java.lang.String str)
Creates a new DateTime object from a string in "2009-01-01T00:00:00" or "2009-01-01" format.DateTime(java.sql.Timestamp timestamp)
Creates a new DateTime object from an existing java.sql.Timestamp object.DateTime(java.sql.Timestamp timestamp, boolean isLocal)
DateTime(java.util.Date date)
DateTime(java.util.Date date, boolean isLocal)
Creates a new DateTime object from an existing java.util.Date object.
-
Method Summary
Modifier and Type Method Description DateTime
add(TimeSpan offset)
DateTime
addDays(int days)
Returns a new DateTime object, increased by the specified number of days (accepts negative values).DateTime
addHours(int hours)
DateTime
addMilliSeconds(int ms)
DateTime
addMinutes(int minutes)
Returns a new DateTime object, increased by the specified number of minutes (accepts negative values).DateTime
addMonths(int months)
DateTime
addSeconds(int seconds)
DateTime
addYears(int years)
boolean
after(DateTime another)
boolean
afterOrEqual(DateTime another)
boolean
before(DateTime another)
boolean
beforeOrEqual(DateTime another)
DateTime
bigger(DateTime another)
protected java.util.Calendar
calendar()
static boolean
canParse(java.lang.String text)
Return true if the string can be parsed using parse().int
compareTo(DateTime another)
boolean
equals(java.lang.Object another)
DateTime
firstDayOfMonth()
DateTime
firstDayOfYear()
static DateTime
fromDateLocal(java.util.Date date)
DateTime
getDatePart()
int
getDay()
DayOfWeek
getDayOfWeek()
int
getDayOfWeekOrdinal()
int
getDayOfYear()
int
getDaysOfMonth()
int
getDaysOfYear()
int
getHour()
int
getMinute()
int
getMonth()
java.lang.String
getMonthName(DateTime date, java.util.Locale locale)
long
getTicks()
TimeSpan
getTimeOfDay()
java.util.TimeZone
getTimezone()
static DateTime
getToday()
static DateTime
getToday(java.util.TimeZone tz)
Returns beginning of this day in the given time zone.int
getYear()
DateTime
inZoneByName(java.util.TimeZone newZone)
boolean
isWeekend()
TimeSpan
minus(DateTime another)
static DateTime
parse(java.lang.Object o, boolean isLocal)
static DateTime
parseAsLocal(java.lang.Object o)
static DateTime
parseString(java.lang.String str)
void
setTimezone(java.util.TimeZone timezone)
DateTime
smaller(DateTime another)
java.util.Date
toDate()
java.util.Date
toDateLocal()
java.lang.String
toDateString()
java.lang.String
toDateString(java.util.Locale locale)
java.lang.String
toDateTimeString(java.util.Locale locale)
static DateTime
today()
java.lang.String
toString()
Converts DateTime to a String in "2009-01-01T00:00:00" format.java.lang.String
toString(java.lang.String pattern)
Formats the date using the specified pattern (see http://java.sun.com/javase/6/docs/api/index.html?java/text/SimpleDateFormat.html).java.lang.String
toString(java.lang.String pattern, java.util.Locale locale)
Formats the date using the specified pattern (see http://java.sun.com/javase/6/docs/api/index.html?java/text/SimpleDateFormat.html) and locale.java.lang.String
toStringIso8601()
Converts DateTime to a String in ISO 8601 format ("2009-01-01T00:00:00").java.lang.String
toStringSortable()
Converts DateTime to a sortable string format.java.sql.Timestamp
toTimeStamp()
Converts DateTime to a java.sql.Timestamp object.DateTime
tz(java.util.TimeZone tz)
Returns a new DateTime instance with timezone set to tz.DateTime
utc()
Returns a new DateTime instance with timezone set to UTC.
-
-
-
Constructor Detail
-
DateTime
public DateTime()
Creates a new DateTime representing current date and time (now).- Parameters:
date
-
-
DateTime
public DateTime(long time)
-
DateTime
public DateTime(java.sql.Timestamp timestamp)
Creates a new DateTime object from an existing java.sql.Timestamp object.- Parameters:
date
-
-
DateTime
public DateTime(java.sql.Timestamp timestamp, boolean isLocal)
-
DateTime
public DateTime(java.util.Date date, boolean isLocal)
Creates a new DateTime object from an existing java.util.Date object.- Parameters:
date
-
-
DateTime
public DateTime(java.util.Date date)
-
DateTime
public DateTime(java.lang.String str)
Creates a new DateTime object from a string in "2009-01-01T00:00:00" or "2009-01-01" format.- Parameters:
date
-
-
DateTime
public DateTime(int year, int month, int day)
-
DateTime
public DateTime(long ticks, java.util.TimeZone tz)
-
-
Method Detail
-
calendar
protected java.util.Calendar calendar()
-
fromDateLocal
public static DateTime fromDateLocal(java.util.Date date)
-
toStringSortable
public java.lang.String toStringSortable()
Converts DateTime to a sortable string format. Uses toStringIso8601().- Parameters:
date
-
-
toStringIso8601
public java.lang.String toStringIso8601()
Converts DateTime to a String in ISO 8601 format ("2009-01-01T00:00:00").- Parameters:
date
-
-
toString
public java.lang.String toString()
Converts DateTime to a String in "2009-01-01T00:00:00" format.- Overrides:
toString
in classjava.lang.Object
- Parameters:
date
-
-
toString
public java.lang.String toString(java.lang.String pattern)
Formats the date using the specified pattern (see http://java.sun.com/javase/6/docs/api/index.html?java/text/SimpleDateFormat.html). Uses the default locale (Locale.getDefault()).- Parameters:
pattern
-- Returns:
- Formatted String.
-
toDateString
public java.lang.String toDateString(java.util.Locale locale)
-
toDateTimeString
public java.lang.String toDateTimeString(java.util.Locale locale)
-
toString
public java.lang.String toString(java.lang.String pattern, java.util.Locale locale)
Formats the date using the specified pattern (see http://java.sun.com/javase/6/docs/api/index.html?java/text/SimpleDateFormat.html) and locale.- Parameters:
pattern
-locale
-- Returns:
- Formatted String.
-
toTimeStamp
public java.sql.Timestamp toTimeStamp()
Converts DateTime to a java.sql.Timestamp object.- Parameters:
date
-
-
addDays
public DateTime addDays(int days)
Returns a new DateTime object, increased by the specified number of days (accepts negative values).- Parameters:
days
-- Returns:
-
addMinutes
public DateTime addMinutes(int minutes)
Returns a new DateTime object, increased by the specified number of minutes (accepts negative values).- Parameters:
days
-- Returns:
-
parseAsLocal
public static DateTime parseAsLocal(java.lang.Object o)
-
parseString
public static DateTime parseString(java.lang.String str)
-
parse
public static DateTime parse(java.lang.Object o, boolean isLocal)
-
toDate
public java.util.Date toDate()
-
toDateLocal
public java.util.Date toDateLocal()
-
getYear
public int getYear()
-
getMonth
public int getMonth()
-
getDay
public int getDay()
-
getDatePart
public DateTime getDatePart()
-
getHour
public int getHour()
-
getDayOfWeek
public DayOfWeek getDayOfWeek()
-
getDayOfWeekOrdinal
public int getDayOfWeekOrdinal()
- Returns:
- Sunday = 0, Monday = 1, etc.
-
toDateString
public java.lang.String toDateString()
-
getToday
public static DateTime getToday()
-
today
public static DateTime today()
-
getDayOfYear
public int getDayOfYear()
-
getMinute
public int getMinute()
-
getTimeOfDay
public TimeSpan getTimeOfDay()
-
getTicks
public long getTicks()
-
before
public boolean before(DateTime another)
-
addHours
public DateTime addHours(int hours)
-
addMonths
public DateTime addMonths(int months)
-
addYears
public DateTime addYears(int years)
-
after
public boolean after(DateTime another)
-
equals
public boolean equals(java.lang.Object another)
- Overrides:
equals
in classjava.lang.Object
-
addSeconds
public DateTime addSeconds(int seconds)
-
beforeOrEqual
public boolean beforeOrEqual(DateTime another)
-
afterOrEqual
public boolean afterOrEqual(DateTime another)
-
canParse
public static boolean canParse(java.lang.String text)
Return true if the string can be parsed using parse().- Parameters:
text
-- Returns:
-
isWeekend
public boolean isWeekend()
-
getMonthName
public java.lang.String getMonthName(DateTime date, java.util.Locale locale)
-
firstDayOfMonth
public DateTime firstDayOfMonth()
-
getDaysOfMonth
public int getDaysOfMonth()
-
getDaysOfYear
public int getDaysOfYear()
-
firstDayOfYear
public DateTime firstDayOfYear()
-
compareTo
public int compareTo(DateTime another)
-
tz
public DateTime tz(java.util.TimeZone tz)
Returns a new DateTime instance with timezone set to tz. TimeZone affects formatting methods (e.g. toString()) and info methods (e.g. getDay()). It doesn't affect the actual time (milliseconds since 1970 UTC).- Parameters:
tz
-- Returns:
-
utc
public DateTime utc()
Returns a new DateTime instance with timezone set to UTC. TimeZone affects formatting methods (e.g. toString()) and info methods (e.g. getDay()). It doesn't affect the actual time (milliseconds since 1970 UTC).- Returns:
-
getTimezone
public java.util.TimeZone getTimezone()
-
setTimezone
public void setTimezone(java.util.TimeZone timezone)
-
addMilliSeconds
public DateTime addMilliSeconds(int ms)
-
getToday
public static DateTime getToday(java.util.TimeZone tz)
Returns beginning of this day in the given time zone. The timezone of the returned class is set to tz.- Parameters:
tz
-- Returns:
-
inZoneByName
public DateTime inZoneByName(java.util.TimeZone newZone)
-
-