📄 datetime.java
字号:
{ String hrs[] = new String[hr24? 24 : 12]; for (int i = 0; i < hrs.length; i++) { hrs[i] = String.valueOf(i); } return hrs; } /** *** Gets a String array of minutes in a day *** (used as a VComboBox item list) *** @return A String array of minutes in a day **/ public static String[] getMinutes() { String min[] = new String[60]; for (int i = 0; i < min.length; i++) { min[i] = String.valueOf(i); } return min; } // ------------------------------------------------------------------------ /** *** Returns a String representation of the specified epoch time seconds *** @param timeSec The Epoch time seconds *** @return The String representation **/ public static String toString(long timeSec) { return (new java.util.Date(timeSec * 1000L)).toString(); } /** *** Returns the current Epoch time in seconds since January 1, 1970 00:00:00 GMT *** @return The current Epoch time in seconds **/ public static long getCurrentTimeSec() { // Number of seconds since the 'epoch' January 1, 1970, 00:00:00 GMT return getCurrentTimeMillis() / 1000L; } /** *** Returns the current Epoch time in milliseconds since January 1, 1970 00:00:00 GMT *** @return The current Epoch time in milliseconds **/ public static long getCurrentTimeMillis() { // Number of milliseconds since the 'epoch' January 1, 1970, 00:00:00 GMT return System.currentTimeMillis(); } /** *** Returns true if the specified time is within the specified lapsed time *** @param timeSec An Epoch time in seconds *** @param lapseSec The time duration to test *** @return True if the specified time is within the specified lapsed time **/ public static boolean isRecentSec(long timeSec, long lapseSec) { Print.logDebug("timeSec: " + timeSec); Print.logDebug("getCurrentTimeSec: " + DateTime.getCurrentTimeSec()); return (timeSec >= (DateTime.getCurrentTimeSec() - lapseSec)); } // ------------------------------------------------------------------------ /** *** Gets the minimum acceptable DateTime *** @return The minimum acceptable DateTime **/ public static DateTime getMinDate() { return DateTime.getMinDate(null); } /** *** Gets the minimum acceptable DateTime *** @param tz The TimeZone (not that it really matters) *** @return The minimum acceptable DateTime **/ public static DateTime getMinDate(TimeZone tz) { return new DateTime(1, tz); } /** *** Gets the maximum acceptable DateTime *** @return The maximum acceptable DateTime **/ public static DateTime getMaxDate() { return DateTime.getMaxDate(null); } /** *** Gets the maximum acceptable DateTime *** @param tz The TimeZone (not that it really matters) *** @return The maximum acceptable DateTime **/ public static DateTime getMaxDate(TimeZone tz) { return new DateTime(tz, 2020, 12, 31); } // ------------------------------------------------------------------------ /** *** Parse specified String into a DateTime instance. *** This method parses a date which has been provided as an argument to a command-line tool *** or in a URL request string. *** Date Format: YYYY/MM[/DD[/hh[:mm[:ss]]]] (ie. YYYY/MM/DD/hh:mm:ss) *** @param dateStr The date String to parse *** @param tz The TimeZone *** @param isToDate True to default to an end-of-day time if the time is not specified *** @return The parse DateTime instance, or null if unable to parse the specified date/time string **/ public static DateTime parseArgumentDate(String dateStr, TimeZone tz, boolean isToDate) { DateTime date = null; String dateFld[] = !dateStr.equals("")? StringTools.parseString(dateStr, "/:") : null; if ((dateFld == null) || (dateFld.length == 0)) { date = null; } else if (dateFld.length == 1) { // parse as 'Epoch' time long epoch = StringTools.parseLong(dateFld[0], DateTime.getCurrentTimeSec()); date = new DateTime(epoch, tz); } else { // (dateFld.length >= 2) DateTime now = new DateTime(tz); int YY = StringTools.parseInt(dateFld[0], now.getYear()); int MM = StringTools.parseInt(dateFld[1], now.getMonth1()); int DD = 0; int hh = isToDate? 23 : 0; // default to end of day int mm = isToDate? 59 : 0; // default to end of hour int ss = isToDate? 59 : 0; // default to end of minute if (dateFld.length >= 3) { // at least YYYY/MM/DD provided DD = StringTools.parseInt(dateFld[2], now.getDayOfMonth()); if (dateFld.length >= 4) { hh = StringTools.parseInt(dateFld[3], hh); } if (dateFld.length >= 5) { mm = StringTools.parseInt(dateFld[4], mm); } if (dateFld.length >= 6) { ss = StringTools.parseInt(dateFld[5], ss); } } else { // only YYYY/MM provided DD = isToDate? DateTime.getDaysInMonth(tz, MM, YY) : 1; // default to end of month } date = new DateTime(tz, YY, MM, DD, hh, mm, ss); } return date; } // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ private TimeZone timeZone = null; private long timeMillis = 0L; // ms since January 1, 1970, 00:00:00 GMT /** *** Default constructor. *** Initialize with the current Epoch time. **/ public DateTime() { this.setTimeMillis(getCurrentTimeMillis()); } /** *** Constructor. *** Initialize with the current Epoch time. *** @param tz The TimeZone **/ public DateTime(TimeZone tz) { this(); this.setTimeZone(tz); } /** *** Constructor. *** @param date The Date object used to initialize this DateTime **/ public DateTime(Date date) { this.setTimeMillis(date.getTime()); } /** *** Constructor. *** @param date The Date object used to initialize this DateTime *** @param tz The TimeZone **/ public DateTime(Date date, TimeZone tz) { this.setTimeMillis(date.getTime()); this.setTimeZone(tz); } /** *** Constructor. *** @param tz The TimeZone *** @param year The year *** @param month1 The 1-based month index [1..12] *** @param day The day **/ public DateTime(TimeZone tz, int year, int month1, int day) { this.setDate(tz, year, month1, day); } /** *** Constructor. *** @param tz The TimeZone *** @param year The year *** @param month1 The 1-based month index [1..12] *** @param day The day *** @param hour24 The hour of the day [24 hour clock] *** @param minute The minute of the hour *** @param second The second of the minute **/ public DateTime(TimeZone tz, int year, int month1, int day, int hour24, int minute, int second) { this.setDate(tz, year, month1, day, hour24, minute, second); } /** *** Constructor. *** @param timeSec The Epoch time in seconds **/ public DateTime(long timeSec) { this.setTimeSec(timeSec); } /** *** Constructor. *** @param timeSec The Epoch time in seconds *** @param tz The TimeZone **/ public DateTime(long timeSec, TimeZone tz) { this.setTimeSec(timeSec); this.setTimeZone(tz); } /** *** Constructor. *** @param d A date/time String representation **/ public DateTime(String d) throws ParseException { this.setDate(d); } /** *** Copy constructor *** @param dt Another DateTime instance **/ public DateTime(DateTime dt) { this.timeMillis = dt.timeMillis; this.timeZone = dt.timeZone; } /** *** Copy constructor with delta offset time *** @param dt Another DateTime instance *** @param deltaOffsetSec +/- offset from time specified in DateTime instance **/ public DateTime(DateTime dt, long deltaOffsetSec) { this.timeMillis = dt.timeMillis + (deltaOffsetSec * 1000L); this.timeZone = dt.timeZone; } // ------------------------------------------------------------------------ /** *** Gets a Date object based on the time in this DateTime object *** @return A Date instance **/ public java.util.Date getDate() { return new java.util.Date(this.getTimeMillis()); } /** *** Sets the current time of this instance *** @param tz The TimeZone *** @param year The year *** @param month1 The 1-based month index [1..12] *** @param day The day **/ public void setDate(TimeZone tz, int year, int month1, int day) { this.setDate(tz, year, month1, day, 12, 0, 0); } /** *** Sets the current time of this instance *** @param tz The TimeZone *** @param year The year *** @param month1 The 1-based month index [1..12] *** @param day The day *** @param hour24 The hour of the day [24 hour clock] *** @param minute The minute of the hour *** @param second The second of the minute **/ public void setDate(TimeZone tz, int year, int month1, int day, int hour24, int minute, int second) { int month0 = month1 - 1; this.setTimeZone(tz); Calendar cal = new GregorianCalendar(this._timeZone(tz)); cal.set(year, month0, day, hour24, minute, second); Date date = cal.getTime(); this.setTimeMillis(date.getTime()); } /** *** Sets the current time of this instance *** @param d The String representation of a date/time **/ public void setDate(String d) throws ParseException { this.setDate(d, (TimeZone)null); } /** *** Sets the current time of this instance *** @param d The String representation of a date/time *** @param dftTMZ The TimeZone used if no timezone was parsed from the String **/ public void setDate(String d, TimeZone dftTMZ) throws ParseException { String ds[] = StringTools.parseString(d, " _"); String dt = (ds.length > 0)? ds[0] : ""; String tm = (ds.length > 1)? ds[1] : ""; String tz = (ds.length > 2)? ds[2] : ""; // Valid format: "YYYY/MM/DD [HH:MM:SS] [PST]" //Print.logInfo("Parsing " + dt + " " + tm + " " + tz); /* time-zone */ TimeZone timeZone = null; if (ds.length > 2) { if (tz.equals("+0000") || tz.equals("-0000")) { timeZone = DateTime.getGMTTimeZone(); } else if (DateTime.isValidTimeZone(tz)) { timeZone = DateTime.getTimeZone(tz); } else { throw new ParseException("Invalid TimeZone: " + tz, 0); } } else if ((ds.length > 1) && DateTime.isValidTimeZone(tm)) { tz = tm; tm = ""; timeZone = DateTime.getTimeZone(tz); } else { timeZone = (dftTMZ != null)? dftTMZ : this.getTimeZone(); } //Print.logInfo("Timezone = " + timeZone); this.setTimeZone(timeZone); Calendar calendar = new GregorianCalendar(timeZone); /* date */ int yr = -1, mo = -1, dy = -1; int d1 = dt.indexOf('/'), d2 = (d1 > 0)? dt.indexOf('/', d1 + 1) : -1; if ((d1 > 0) && (d2 > d1)) { /* year */ String YR = dt.substring(0, d1); yr = StringTools.parseInt(YR, -1); if ((yr >= 0) && (yr <= 49)) { yr += 2000; } else if ((yr >= 50) && (yr <= 99)) { yr += 1900; } if ((yr < 1900) || (yr > 2100)) { throw new ParseException("Date/Year out of range: " + YR, 0); } /* month */ String MO = dt.substring(d1 + 1, d2); mo = StringTools.parseInt(MO, -1) - 1; // 0 indexed if ((mo < 0) || (mo > 11)) { throw new ParseException("Date/Month out of range: " + MO, 0); } /* seconds */ String DY = dt.substring(d2 + 1); dy = StringTools.parseInt(DY, -1); if ((dy < 1) || (dy > 31)) { throw new ParseException("Date/Day out of range: " + DY, 0); } } else { throw new ParseException("Invalid date format (Y/M/D): " + dt, 0); } /* time */ if (tm.equals("")) { //Print.logInfo("Just using YMD:" + yr + "/" + mo + "/" + dy); calendar.set(yr, mo, dy); } else { int hr = -1, mn = -1, sc = -1; int t1 = tm.indexOf(':'), t2 = (t1 > 0)? tm.indexOf(':', t1 + 1) : -1; if (t1 > 0) { /* hour */ String HR = tm.substring(0, t1); hr = StringTools.parseInt(HR, -1); if ((hr < 0) || (hr > 23)) { throw new ParseException("Time/Hour out of range: " + HR, 0); } if (t2 > t1) { /* minute */ String MN = tm.substring(t1 + 1, t2); mn = StringTools.parseInt(MN, -1); if ((mn < 0) || (mn > 59)) { throw new ParseException("Time/Minute out of range: " + MN, 0); } /* second */ String SC = tm.substring(t2 + 1); sc = StringTools.parseInt(SC, -1); if ((sc < 0) || (sc > 59)) { throw new ParseException("Time/Second out of range: " + SC, 0); } //Print.logInfo("Setting YMDHMS:" + yr + "/" + mo + "/" + dy+ " " + hr + ":" + mn + ":" + sc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -