📄 timeutil.java
字号:
final static Date fastDateCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day) { Calendar dateCal = cal; if (useGmtConversion) { if (gmtCalIfNeeded == null) { gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT")); } gmtCalIfNeeded.clear(); dateCal = gmtCalIfNeeded; } dateCal.clear(); dateCal.set(Calendar.MILLISECOND, 0); // why-oh-why is this different than java.util.date, // in the year part, but it still keeps the silly '0' // for the start month???? dateCal.set(year, month - 1, day, 0, 0, 0); long dateAsMillis = 0; try { dateAsMillis = dateCal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... dateAsMillis = dateCal.getTime().getTime(); } return new Date(dateAsMillis); } final static Date fastDateCreate(int year, int month, int day, Calendar targetCalendar) { Calendar dateCal = (targetCalendar == null) ? new GregorianCalendar() : targetCalendar; dateCal.clear(); // why-oh-why is this different than java.util.date, // in the year part, but it still keeps the silly '0' // for the start month???? dateCal.set(year, month - 1, day, 0, 0, 0); dateCal.set(Calendar.MILLISECOND, 0); long dateAsMillis = 0; try { dateAsMillis = dateCal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... dateAsMillis = dateCal.getTime().getTime(); } return new Date(dateAsMillis); } final static Time fastTimeCreate(Calendar cal, int hour, int minute, int second) throws SQLException { if (hour < 0 || hour > 24) { throw SQLError.createSQLException("Illegal hour value '" + hour + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (minute < 0 || minute > 59) { throw SQLError.createSQLException("Illegal minute value '" + minute + "'" + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (second < 0 || second > 59) { throw SQLError.createSQLException("Illegal minute value '" + second + "'" + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } cal.clear(); // Set 'date' to epoch of Jan 1, 1970 cal.set(1970, 0, 1, hour, minute, second); long timeAsMillis = 0; try { timeAsMillis = cal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... timeAsMillis = cal.getTime().getTime(); } return new Time(timeAsMillis); } final static Time fastTimeCreate(int hour, int minute, int second, Calendar targetCalendar) throws SQLException { if (hour < 0 || hour > 23) { throw SQLError.createSQLException("Illegal hour value '" + hour + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (minute < 0 || minute > 59) { throw SQLError.createSQLException("Illegal minute value '" + minute + "'" + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if (second < 0 || second > 59) { throw SQLError.createSQLException("Illegal minute value '" + second + "'" + "' for java.sql.Time type in value '" + timeFormattedString(hour, minute, second) + ".", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } Calendar cal = (targetCalendar == null) ? new GregorianCalendar() : targetCalendar; cal.clear(); // Set 'date' to epoch of Jan 1, 1970 cal.set(1970, 0, 1, hour, minute, second); long timeAsMillis = 0; try { timeAsMillis = cal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... timeAsMillis = cal.getTime().getTime(); } return new Time(timeAsMillis); } final static Timestamp fastTimestampCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day, int hour, int minute, int seconds, int secondsPart) { cal.clear(); // why-oh-why is this different than java.util.date, // in the year part, but it still keeps the silly '0' // for the start month???? cal.set(year, month - 1, day, hour, minute, seconds); int offsetDiff = 0; if (useGmtConversion) { int fromOffset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET); if (gmtCalIfNeeded == null) { gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT")); } gmtCalIfNeeded.clear(); gmtCalIfNeeded.setTimeInMillis(cal.getTimeInMillis()); int toOffset = gmtCalIfNeeded.get(Calendar.ZONE_OFFSET) + gmtCalIfNeeded.get(Calendar.DST_OFFSET); offsetDiff = fromOffset - toOffset; } if (secondsPart != 0) { cal.set(Calendar.MILLISECOND, secondsPart / 1000000); } long tsAsMillis = 0; try { tsAsMillis = cal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... tsAsMillis = cal.getTime().getTime(); } Timestamp ts = new Timestamp(tsAsMillis + offsetDiff); ts.setNanos(secondsPart); return ts; } final static Timestamp fastTimestampCreate(TimeZone tz, int year, int month, int day, int hour, int minute, int seconds, int secondsPart) { Calendar cal = (tz == null) ? new GregorianCalendar() : new GregorianCalendar(tz); cal.clear(); // why-oh-why is this different than java.util.date, // in the year part, but it still keeps the silly '0' // for the start month???? cal.set(year, month - 1, day, hour, minute, seconds); long tsAsMillis = 0; try { tsAsMillis = cal.getTimeInMillis(); } catch (IllegalAccessError iae) { // Must be on JDK-1.3.1 or older.... tsAsMillis = cal.getTime().getTime(); } Timestamp ts = new Timestamp(tsAsMillis); ts.setNanos(secondsPart); return ts; } /** * Returns the 'official' Java timezone name for the given timezone * * @param timezoneStr * the 'common' timezone name * * @return the Java timezone name for the given timezone * @throws SQLException * * @throws IllegalArgumentException * DOCUMENT ME! */ public static String getCanoncialTimezone(String timezoneStr) throws SQLException { if (timezoneStr == null) { return null; } timezoneStr = timezoneStr.trim(); // handle '+/-hh:mm' form ... if (timezoneStr.length() > 2) { if ((timezoneStr.charAt(0) == '+' || timezoneStr.charAt(0) == '-') && Character.isDigit(timezoneStr.charAt(1))) { return "GMT" + timezoneStr; } } // Fix windows Daylight/Standard shift JDK doesn't map these (doh) int daylightIndex = StringUtils.indexOfIgnoreCase(timezoneStr, "DAYLIGHT"); if (daylightIndex != -1) { StringBuffer timezoneBuf = new StringBuffer(); timezoneBuf.append(timezoneStr.substring(0, daylightIndex)); timezoneBuf.append("Standard"); timezoneBuf.append(timezoneStr.substring(daylightIndex + "DAYLIGHT".length(), timezoneStr.length())); timezoneStr = timezoneBuf.toString(); } String canonicalTz = (String) TIMEZONE_MAPPINGS.get(timezoneStr); // if we didn't find it, try abbreviated timezones if (canonicalTz == null) { String[] abbreviatedTimezone = (String[]) ABBREVIATED_TIMEZONES .get(timezoneStr); if (abbreviatedTimezone != null) { // If there's only one mapping use that if (abbreviatedTimezone.length == 1) { canonicalTz = abbreviatedTimezone[0]; } else { StringBuffer possibleTimezones = new StringBuffer(128); possibleTimezones.append(abbreviatedTimezone[0]); for (int i = 1; i < abbreviatedTimezone.length; i++) { possibleTimezones.append(", "); possibleTimezones.append(abbreviatedTimezone[i]); } throw SQLError.createSQLException(Messages.getString("TimeUtil.TooGenericTimezoneId", new Object[] {timezoneStr, possibleTimezones}), SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE); } } } return canonicalTz; } // we could use SimpleDateFormat, but it won't work when the time values // are out-of-bounds, and we're using this for error messages for exactly // that case // private static String timeFormattedString(int hours, int minutes, int seconds) { StringBuffer buf = new StringBuffer(8); if (hours < 10) { buf.append("0"); } buf.append(hours); buf.append(":"); if (minutes < 10) { buf.append("0"); } buf.append(minutes); buf.append(":"); if (seconds < 10) { buf.append("0"); } buf.append(seconds); return buf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -