📄 jdatetime.java
字号:
/**
* Adds month.
*
* @param m month to add
* @param monthFix <code>true</code> for month fixing, <code>false</code> otherwise
*/
public void addMonth(int m, boolean monthFix) {
add(0, m, 0, monthFix);
}
/**
* Adds month, with preset value of monthFix.
*
* @param m month to add
*/
public void addMonth(int m) {
addMonth(m, getMonthFix());
}
/**
* Adds days.
*
* @param d days to add
* @param monthFix <code>true</code> for month fixing, <code>false</code> otherwise
*/
public void addDay(int d, boolean monthFix) {
add(0, 0, d, monthFix);
}
/**
* Adds days, with preset value of monthFix.
*
* @param d days to add
*/
public void addDay(int d) {
addDay(d, getMonthFix());
}
/**
* Adds hours.
*
* @param h hours to add
* @param monthFix <code>true</code> for month fixing, <code>false</code> otherwise
*/
public void addHour(int h, boolean monthFix) {
addTime(h, 0, 0, monthFix);
}
/**
* Adds hours, with preset value of monthFix.
*
* @param h hours to add
*/
public void addHour(int h) {
addHour(h, getMonthFix());
}
/**
* Adds minutes.
*
* @param m minutes to add.
* @param monthFix <code>true</code> for month fixing, <code>false</code> otherwise
*/
public void addMinute(int m, boolean monthFix) {
addTime(0, m, 0, monthFix);
}
/**
* Adds minutes, with preset value of monthFix.
*
* @param m minutes to add.
*/
public void addMinute(int m) {
addMinute(m, getMonthFix());
}
/**
* Adds seconds.
*
* @param s seconds to add
* @param monthFix <code>true</code> for month fixing, <code>false</code> otherwise
*/
public void addSecond(double s, boolean monthFix) {
addTime(0, 0, s, monthFix);
}
/**
* Adds seconds, with preset value of monthFix.
*
* @param s seconds to add
*/
public void addSecond(double s) {
addSecond(s, getMonthFix());
}
/**
* Adds milliseconds.
*
* @param ms miliseconds to add
* @param monthFix <code>true</code> for month fixing, <code>false</code> otherwise
*/
public void addMillisecond(int ms, boolean monthFix) {
addTime(0, 0, ms / 1000.0, monthFix);
}
/**
* Adds milliseconds, with preset value of monthFix.
*
* @param ms miliseconds to add
*/
public void addMillisecond(int ms) {
addMillisecond(ms, getMonthFix());
}
// ---------------------------------------------------------------- constructors/sets/gets
/**
* Constructor that set date and time.
*
* @param year year to set
* @param month month to set
* @param day day to set
* @param hour hours to set
* @param minute minutes to set
* @param second seconds to set
*
* @see #set(int, int, int, int, int, double)
*/
public JDateTime(int year, int month, int day, int hour, int minute, double second) {
this.set(year, month, day, hour, minute, second);
}
/**
* Sets date, time is set to midnight (00:00:00.000).
*
* @param year year to set
* @param month month to set
* @param day day to set
*/
public void set(int year, int month, int day) {
set(year, month, day, 0, 0, 0);
}
/**
* Constructor that sets just date. Time is set to 00:00:00.
*
* @param year year to set
* @param month month to set
* @param day day to set
*
* @see #set(int, int, int)
*/
public JDateTime(int year, int month, int day) {
this.set(year, month, day);
}
/**
* Sets time, date is unchanged.
*
* @param hour hours to set
* @param minute minutes to set
* @param second secnds to set
*/
public void setTime(int hour, int minute, double second) {
set(time.year, time.month, time.day, hour, minute, second);
}
/**
* Sets date, time remains unchanged.
*
* @param year year
* @param month month
* @param day day
*/
public void setDate(int year, int month, int day) {
set(year, month, day, time.hour, time.minute, time.second);
}
// ---------------------------------------------------------------- set from milliseconds
/**
* Constructor that sets current time specified as time in milliseconds, from
* the midnight, January 1, 1970 UTC.
*
* @param milis time in milliseconds, from the midnight, January 1, 1970 UTC
*
* @see #set(long )
*/
public JDateTime(long milis) {
set(milis);
}
private static final double MILIS_IN_DAY = 1000 * 60 * 60 * 24;
/**
* Sets the time based on current time in milliseconds. Current time is
* calculated from the midnight, January 1, 1970 UTC.
*
* @param milis time in milliseconds, from the midnight, January 1, 1970 UTC
*/
public void set(long milis) {
BigDecimal bd = JD_1970.toBigDecimal();
milis += TimeZone.getDefault().getOffset(milis);
BigDecimal delta = new BigDecimal(milis / MILIS_IN_DAY);
JulianDateStamp now = new JulianDateStamp(bd.add(delta));
setJulianDate(now);
}
// ---------------------------------------------------------------- date/time sets
/**
* Sets current year.
*
* @param y year to set
*/
public void setYear(int y) {
setDate(y, time.month, time.day);
}
/**
* Sets current month.
*
* @param m month to set
*/
public void setMonth(int m) {
setDate(time.year, m, time.day);
}
/**
* Sets current day.
*
* @param d day to set
*/
public void setDay(int d) {
setDate(time.year, time.month, d);
}
/**
* Set current hour.
*
* @param h hour to set
*/
public void setHour(int h) {
setTime(h, time.minute, time.second);
}
/**
* Set current minute.
*
* @param m minutes to set
*/
public void setMinute(int m) {
setTime(time.hour, m, time.second);
}
/**
* Sets current second and millisecond.
*
* @param s seconds and milliseconds to set
*/
public void setSecond(double s) {
setTime(time.hour, time.minute, s);
}
/**
* Sets current second.
*
* @param s seconds to set
*/
public void setSecond(int s) {
double milis = s + (time.second - (int)time.second);
setTime(time.hour, time.minute, milis);
}
/**
* Sets current millisecond.
*
* @param m milliseconds to set
*/
public void setMillisecond(int m) {
setTime(time.hour, time.minute, ((int)time.second) + m/1000.0);
}
// ---------------------------------------------------------------- date/time gets
/**
* Returns current year.
*
* @return current year
*/
public int getYear() {
return time.year;
}
/**
* Returns current month.
*
* @return current month
*/
public int getMonth() {
return time.month;
}
/**
* Returns current day of month.
*
* @return current day of month
* @see #getDayOfMonth
*/
public int getDay() {
return time.day;
}
/**
* Returns current day of month.
*
* @return current day of month
* @see #getDay
*/
public int getDayOfMonth() {
return time.day;
}
/**
* Returns current hour.
*
* @return current hour
*/
public int getHour() {
return time.hour;
}
/**
* Returns current minutes.
*
* @return current minutes
*/
public int getMinute() {
return time.minute;
}
/**
* Return current secodns. For an integer value, just cast the returned
* value.
*
* @return current seconds.
*/
public double getSecond() {
return time.second;
}
/**
* Returns current milliseconds.
*
* @return current milliseconds
*/
public int getMillisecond() {
return (int) ((time.second - (int)time.second) * 1000 + 1e-9);
}
// ---------------------------------------------------------------- other gets
/**
* Returns current day of week.
*
* @return current day of week
*/
public int getDayOfWeek() {
return dayofweek;
}
/**
* Returns current day of year.
*
* @return current day of year
*/
public int getDayOfYear() {
return dayofyear;
}
/**
* Returns current leap year flag.
*
* @return current leap year flag
*/
public boolean isLeap() {
return leap;
}
/**
* Returns current week of year.
*
* @return current week of year
*/
public int getWeekOfYear() {
return weekofyear;
}
public int getWeekOfMonth() {
return weekofmonth;
}
/**
* Length of months. Modified by main setJulianDate() method for February and
* leap years.
*/
private static final int MONTH_LENGTH[] = {0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/**
* Returns the length of the specified month in days.
*
* @return length of the specified month in days
*/
public int getMonthLength(int m) {
if ((m < 1) || (m > 12)) {
return -1;
}
return MONTH_LENGTH[m];
}
/**
* Returns the length of the current month in days.
*
* @return length of the current month in days
*/
public int getMonthLength() {
return MONTH_LENGTH[time.month];
}
// ---------------------------------------------------------------- setting the current time
/**
* Sets current local date and time.
*/
public void set() {
loadFrom(Calendar.getInstance());
}
/**
* Constructor that sets current local date and time.
*/
public JDateTime() {
this.set();
}
// ---------------------------------------------------------------- objects and instances
private static HashMap converters = new HashMap();
static {
registerDefaults();
}
/**
* Registers default converters.
*/
public static void registerDefaults() {
converters.clear();
register(JDateTime.class, new jodd.datetime.converters.JDateTimeConverter());
register(Calendar.class, new CalendarConverter());
register(java.util.GregorianCalendar.class, new GregorianCalendarConverter());
register(java.util.Date.class, new DateConverter());
register(java.sql.Date.class, new SqlDateConverter());
register(java.sql.Timestamp.class, new SqlTimestampConverter());
}
/**
* Registers a <code>JdtConvertor</code> 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. <p>
*
* 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).
*
* @param c class of an object that will be instanced and populated with time
* @param gtc converter
*/
public static void register(Class c, JdtConverter gtc) {
converters.put(c, gtc);
}
/**
* Loads time from an object by using registered converters.
*
* @param o object to read time from
*/
public void loadFrom(Object o) {
JdtConverter gtc = (JdtConverter) converters.get(o.getClass());
if (gtc != null) {
gtc.load(this, o);
}
}
/**
* Stores time to a new instance of desired class, by using converters.
*
* @param c class of new object
*
* @return new instance of specified class, populated with time
*/
public Object getInstance(Class c) {
JdtConverter gtc = (JdtConverter) converters.get(c);
if (gtc == null) {
return null;
}
return gtc.get(this);
}
/**
* Shortcut for getInstance() that builds and returns <code>Calendar</code>
* instance.
*
* @return Calendar instance
* @see #getInstance
*/
public java.util.Calendar getCalendarInstance() {
return (java.util.Calendar) getInstance(java.util.Calendar.class);
}
/**
* Shortcut for getInstance() that builds and returns <code>Date</code>
* instance.
*
* @return Date instance
* @see #getInstance
*/
public java.util.Date getDateInstance() {
return (java.util.Date) getInstance(java.util.Date.class);
}
/**
* Shortcut for getInstance() that builds and returns
* <code>GregorianCalendar</code> instance.
*
* @return GregorianCalendar instance
* @see #getInstance
*/
public java.util.GregorianCalendar getGregorianCalendarInstace() {
return (java.util.GregorianCalendar) getInstance(java.util.GregorianCalendar.class);
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -