📄 datetime.java
字号:
calendar.set(yr, mo, dy, hr, mn, sc); } else { /* minute */ String MN = tm.substring(t1 + 1); mn = StringTools.parseInt(MN, -1); if ((mn < 0) || (mn > 59)) { throw new ParseException("Time/Minute out of range: " + MN, 0); } //Print.logInfo("Setting YMDHM:" + yr + "/" + mo + "/" + dy+ " " + hr + ":" + mn); calendar.set(yr, mo, dy, hr, mn); } } else { throw new ParseException("Invalid time format (H:M:S): " + tm, 0); } } /* ok */ this.setTimeMillis(calendar.getTime().getTime()); } /** *** Does not work, do not use **/ private boolean setParsedDate_formatted(String d) // does not work! { try { java.util.Date date = DateFormat.getDateInstance().parse(d); this.setTimeMillis(date.getTime()); return true; } catch (ParseException pe) { Print.logStackTrace("Unable to parse date: " + d, pe); this.setTimeMillis(0L); return false; } } // ------------------------------------------------------------------------ /** *** Gets a value from a GregorianCalendar based on the Epoch time of this instance *** @param tz The TimeZone *** @param value The value number to return **/ private int _get(TimeZone tz, int value) { return this.getCalendar(tz).get(value); } /** *** Gets a GregorianCalendar calendar instance *** @param tz The TimeZone *** @return The Calendar object **/ public Calendar getCalendar(TimeZone tz) { Calendar c = new GregorianCalendar(this._timeZone(tz)); c.setTimeInMillis(this.getTimeMillis()); return c; } /** *** Gets a GregorianCalendar calendar instance *** @return The Calendar object **/ public Calendar getCalendar() { return this.getCalendar(null); } /** *** Gets the 0-based index of the month represented by this DateTime instance *** @param tz The TimeZone used when calculating the 0-based month index *** @return The 0-based month index **/ public int getMonth0(TimeZone tz) { // return 0..11 return this._get(tz, Calendar.MONTH); // 0 indexed } /** *** Gets the 0-based index of the month represented by this DateTime instance *** @return The 0-based month index **/ public int getMonth0() { return this.getMonth0(null); } /** *** Gets the 1-based index of the month represented by this DateTime instance *** @param tz The TimeZone used when calculating the 0-based month index *** @return The 1-based month index **/ public int getMonth1(TimeZone tz) { // return 1..12 return this.getMonth0(tz) + 1; } /** *** Gets the 1-based index of the month represented by this DateTime instance *** @return The 1-based month index **/ public int getMonth1() { return this.getMonth1(null); } /** *** Gets the day of the month represented by this DateTime instance *** @param tz The TimeZone used when calculating the day of the month *** @return The day of the month **/ public int getDayOfMonth(TimeZone tz) { // return 1..31 return this._get(tz, Calendar.DAY_OF_MONTH); } /** *** Gets the day of the month represented by this DateTime instance *** @return The day of the month **/ public int getDayOfMonth() { return this.getDayOfMonth(null); } /** *** Gets the number of days in the month represented by this DateTime instance *** @param tz The TimeZone used when calculating the number of days in the month *** @return The number of days in the month **/ public int getDaysInMonth(TimeZone tz) { return DateTime.getDaysInMonth(tz, this.getMonth1(tz), this.getYear(tz)); } /** *** Gets the number of days in the month represented by this DateTime instance *** @return The number of days in the month **/ public int getDaysInMonth() { return this.getDaysInMonth(null); } public int getDayOfWeek(TimeZone tz) { // return 0..6 return this._get(tz, Calendar.DAY_OF_WEEK) - Calendar.SUNDAY; } public int getDayOfWeek() { return this.getDayOfWeek(null); } public int getYear(TimeZone tz) { return this._get(tz, Calendar.YEAR); } public int getYear() { return this.getYear(null); } public boolean isLeapYear(TimeZone tz) { GregorianCalendar gc = (GregorianCalendar)this.getCalendar(tz); return gc.isLeapYear(gc.get(Calendar.YEAR)); } public boolean isLeapYear() { return this.isLeapYear(null); } public int getHour24(TimeZone tz) { return this._get(tz, Calendar.HOUR_OF_DAY); } public int getHour24() { return this.getHour24(null); } public int getHour12(TimeZone tz) { return this._get(tz, Calendar.HOUR); } public int getHour12() { return this.getHour12(null); } public boolean isAM(TimeZone tz) { return this._get(tz, Calendar.AM_PM) == Calendar.AM; } public boolean isAM() { return this.isAM(null); } public boolean isPM(TimeZone tz) { return this._get(tz, Calendar.AM_PM) == Calendar.PM; } public boolean isPM() { return this.isPM(null); } public int getMinute(TimeZone tz) { return this._get(tz, Calendar.MINUTE); } public int getMinute() { return this.getMinute(null); } public int getSecond(TimeZone tz) { return this._get(tz, Calendar.SECOND); } public int getSecond() { return this.getSecond(null); } public boolean isDaylightSavings(TimeZone tz) { return _timeZone(tz).inDaylightTime(this.getDate()); } public boolean isDaylightSavings() { return this.isDaylightSavings(null); } // ------------------------------------------------------------------------ /** *** Gets the Epoch time in seconds represented by this instance *** @return The Epoch time in seconds **/ public long getTimeSec() { return this.getTimeMillis() / 1000L; } /** *** Sets the Epoch time in seconds represented by this instance *** @param timeSec Epoch time in seconds **/ public void setTimeSec(long timeSec) { this.timeMillis = timeSec * 1000L; } // ------------------------------------------------------------------------ /** *** Gets the Epoch time in milliseconds represented by this instance *** @return The Epoch time in milliseconds **/ public long getTimeMillis() { return this.timeMillis; } /** *** Sets the Epoch time in milliseconds represented by this instance *** @param timeMillis Epoch time in milliseconds **/ public void setTimeMillis(long timeMillis) { this.timeMillis = timeMillis; } // ------------------------------------------------------------------------ /** *** Returns an Epoch time in seconds which is the beginning of the day *** represented by this instance. *** @param tz The TimeZone *** @return An Epoch time which is at the beginning of the day represented *** by this instance. **/ public long getDayStart(TimeZone tz) { if (tz == null) { tz = _timeZone(tz); } Calendar c = this.getCalendar(tz); Calendar nc = new GregorianCalendar(tz); nc.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0); return nc.getTime().getTime() / 1000L; } /** *** Returns an Epoch time in seconds which is the beginning of the day *** represented by this instance. *** @return An Epoch time which is at the beginning of the day represented *** by this instance. **/ public long getDayStart() { return this.getDayStart(null); } /** *** Returns an Epoch time in seconds which is the end of the day *** represented by this instance. *** @param tz The TimeZone *** @return An Epoch time which is at the end of the day represented *** by this instance. **/ public long getDayEnd(TimeZone tz) { if (tz == null) { tz = _timeZone(tz); } Calendar c = this.getCalendar(tz); Calendar nc = new GregorianCalendar(tz); nc.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 23, 59, 59); return nc.getTime().getTime() / 1000L; } /** *** Returns an Epoch time in seconds which is the end of the day *** represented by this instance. *** @return An Epoch time which is at the end of the day represented *** by this instance. **/ public long getDayEnd() { return this.getDayEnd(null); } // ------------------------------------------------------------------------ /* return time at start of month */ public long getMonthStart(TimeZone tz, int deltaMo) { if (tz == null) { tz = _timeZone(tz); } Calendar c = this.getCalendar(tz); int YY = c.get(Calendar.YEAR); int MM = c.get(Calendar.MONTH); // 0..11 if (deltaMo != 0) { MM += deltaMo; if (MM < 0) { YY += (MM - 11) / 12; MM %= 12; if (MM < 0) { MM += 12; } } else { YY += MM / 12; MM %= 12; } } int DD = 1; Calendar nc = new GregorianCalendar(tz); nc.set(YY, MM, DD, 0, 0, 0); return nc.getTime().getTime() / 1000L; } public long getMonthStart(TimeZone tz) { return this.getMonthStart(tz, 0); } public long getMonthStart(int deltaMo) { return this.getMonthStart(null, deltaMo); } public long getMonthStart() { return this.getMonthStart(null, 0); } /* return time at end of month */ public long getMonthEnd(TimeZone tz, int deltaMo) { if (tz == null) { tz = _timeZone(tz); } Calendar c = this.getCalendar(tz); int YY = c.get(Calendar.YEAR); int MM = c.get(Calendar.MONTH); // 0..11 if (deltaMo != 0) { MM += deltaMo; if (MM < 0) { YY += (MM - 11) / 12; MM %= 12; if (MM < 0) { MM += 12; } } else { YY += MM / 12; MM %= 12; } } int DD = DateTime.getDaysInMonth(tz, MM + 1, YY); Calendar nc = new GregorianCalendar(tz); nc.set(YY, MM, DD, 23, 59, 59); return nc.getTime().getTime() / 1000L; } public long getMonthEnd(TimeZone tz) { return this.getMonthEnd(tz, 0); } public long getMonthEnd(int deltaMo) { return this.getMonthEnd(null, deltaMo); } public long getMonthEnd() { return this.getMonthEnd(null, 0); } // ------------------------------------------------------------------------ public long getDayStartGMT() { // GMT TimeZone return (this.getTimeSec() / SECONDS_PER_DAY) * SECONDS_PER_DAY; } public long getDayEndGMT() { // GMT TimeZone return this.getDayStartGMT() + SECONDS_PER_DAY - 1L; } // ------------------------------------------------------------------------ public boolean after(DateTime dt)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -