📄 hsqldatetime.java
字号:
/* Copyright (c) 2001-2008, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.TimeZone;// fredt@users 20020130 - patch 1.7.0 by fredt - new class// replaces patch by deforest@users// fredt@users 20020414 - patch 517028 by peterhudson@users - use of calendar// fredt@users 20020414 - patch 828957 by tjcrowder@users - JDK 1.3 compatibility// fredt@users 20040105 - patch 870957 by Gerhard Hiller - JDK bug workaround/** * collection of static methods to convert Date, Time and Timestamp strings * into corresponding Java objects. Also accepts SQL literals such as NOW, * TODAY as valid strings and returns the current date / time / datetime. * Compatible with jdk 1.1.x.<p> * * Was reviewed for 1.7.2 resulting in centralising all DATETIME related * operstions.<p> * * HSQLDB uses the client and server's default timezone for all DATETIME * operations. It stores the DATETIME values in .log and .script files using * the default locale of the server. The same values are stored as binary * UTC timestamps in .data files. If the database is trasported from one * timezone to another, then the DATETIME values in cached tables will be * handled as UTC but those in other tables will be treated as local. So * a timestamp representing 12 noon stored in Tokyo timezone will be treated * as 9 pm in London when stored in a cached table but the same value stored * in a memory table will be treated as 12 noon. * * @author fredt@users * @version 1.7.2 * @since 1.7.0 */public class HsqlDateTime { /** * A reusable static value for today's date. Should only be accessed * by getToday() */ private static Calendar today = new GregorianCalendar(); private static Calendar tempCal = new GregorianCalendar(); private static Calendar tempCalDefault = new GregorianCalendar(); private static Calendar tempCalGMT = new GregorianCalendar(TimeZone.getTimeZone("GMT")); private static Date tempDate = new Date(0); private static Date currentDate; static { resetToday(System.currentTimeMillis()); } static final String zerodatetime = "1970-01-01 00:00:00.000000000"; static final String zeronanos = "000000000"; /** * Converts a string in JDBC timestamp escape format to a * <code>Timestamp</code> value. * * @param s timestamp in format <code>yyyy-mm-dd hh:mm:ss.fffffffff</code> * where end part can be omitted, or "NOW" (case insensitive) * @return corresponding <code>Timestamp</code> value * @exception java.lang.IllegalArgumentException if the given argument * does not have the format <code>yyyy-mm-dd hh:mm:ss.fffffffff</code> */ public static Timestamp timestampValue(String s) throws HsqlException { if (s == null) { throw Trace.error(Trace.HsqlDateTime_null_string); } if (s.length() > zerodatetime.length()) { throw Trace.error(Trace.STRING_DATA_TRUNCATION); } s = s + zerodatetime.substring(s.length()); return Timestamp.valueOf(s); } /** * For use with .script file, simpler than above */ public static Timestamp simpleTimestampValue(String s) { return Timestamp.valueOf(s); } /** * @param time milliseconds * @param nano nanoseconds * @return Timestamp object */ public static Timestamp timestampValue(long time, int nano) { Timestamp ts = new Timestamp(time); ts.setNanos(nano); return ts; } /** * Converts a string in JDBC date escape format to a <code>Date</code> * value. Also accepts Timestamp values. * * @param s date in format <code>yyyy-mm-dd</code>, * @return corresponding <code>Date</code> value * @exception java.lang.IllegalArgumentException if the given argument * does not have the format <code>yyyy-mm-dd</code> */ public static Date dateValue(String s) throws HsqlException { if (s == null) { throw Trace.error(Trace.HsqlDateTime_null_string); } if (s.length() > sdfdPattern.length()) { s = s.substring(0, sdfdPattern.length()); } return Date.valueOf(s); } /** * Converts a string in JDBC date escape format to a * <code>Time</code> value. * * @param s date in format <code>hh:mm:ss</code> * @return corresponding <code>Time</code> value * @exception java.lang.IllegalArgumentException if the given argument * does not have the format <code>hh:mm:ss</code> */ public static Time timeValue(String s) { if (s == null) { throw new java.lang.IllegalArgumentException( Trace.getMessage(Trace.HsqlDateTime_null_string)); } return Time.valueOf(s); } static int compare(Date a, Date b) { long atime = a.getTime(); long btime = b.getTime(); if (atime == btime) { return 0; } return atime > btime ? 1 : -1; } static int compare(Time a, Time b) { long atime = a.getTime(); long btime = b.getTime(); if (atime == btime) { return 0; } return atime > btime ? 1 : -1; } static int compare(Timestamp a, Timestamp b) { long atime = a.getTime(); long btime = b.getTime(); if (atime == btime) { if (a.getNanos() == b.getNanos()) { return 0; } return a.getNanos() > b.getNanos() ? 1 : -1; } return atime > btime ? 1 : -1; } public static synchronized Date getCurrentDate(long millis) { getToday(millis); return currentDate; } public static Timestamp getTimestamp(long millis) { return new Timestamp(millis); } private static final String sdftPattern = "HH:mm:ss"; private static final String sdfdPattern = "yyyy-MM-dd"; private static final String sdftsPattern = "yyyy-MM-dd HH:mm:ss."; private static final String sdftsSysPattern = "yyyy-MM-dd HH:mm:ss.SSS"; static SimpleDateFormat sdfd = new SimpleDateFormat(sdfdPattern); static SimpleDateFormat sdft = new SimpleDateFormat(sdftPattern); static SimpleDateFormat sdfts = new SimpleDateFormat(sdftsPattern); static SimpleDateFormat sdftsSys = new SimpleDateFormat(sdftsSysPattern); /** * Creates a valid timestamp string - jre 1.3 returns incorrect date part * for Timestamp.toString(); */ public static String getTimestampString(Timestamp x) { synchronized (sdfts) { sdfts.setCalendar(tempCalDefault); String n = String.valueOf(x.getNanos()); return sdfts.format(x) + zeronanos.substring(n.length()) + n; } } /** * Creates a full length timestamp string, with 9 digist for nanos */ public static String getTimestampString(Timestamp x, Calendar cal) { synchronized (sdfts) { sdfts.setCalendar(cal == null ? tempCalDefault : cal); String n = String.valueOf(x.getNanos()); return sdfts.format(x) + zeronanos.substring(n.length()) + n; } } private static java.util.Date sysDate = new java.util.Date(); public static String getSytemTimeString() { synchronized (sdftsSys) { sysDate.setTime(System.currentTimeMillis()); return sdftsSys.format(sysDate); } } public static String getTimestampString(long timestamp) { synchronized (sdftsSys) { sysDate.setTime(timestamp); return sdftsSys.format(sysDate); } } public static String getTimeString(java.util.Date x, Calendar cal) { synchronized (sdft) { sdft.setCalendar(cal == null ? tempCalDefault : cal); return sdft.format(x); } } public static String getDateString(java.util.Date x, Calendar cal) { synchronized (sdfd) { sdfd.setCalendar(cal == null ? tempCalDefault : cal); return sdfd.format(x); } } /** * Returns the same Date Object. This object should be treated as * read-only. */ static synchronized Calendar getToday(long millis) { if (millis - getTimeInMillis(today) >= 24 * 3600 * 1000) { resetToday(millis); } return today; } public static void resetToDate(Calendar cal) { cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); } public static void resetToTime(Calendar cal) { cal.set(Calendar.YEAR, 1970); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DATE, 1); cal.set(Calendar.MILLISECOND, 0); } /** * resets the static reusable value today */ private static synchronized void resetToday(long millis) {//#ifdef JAVA4 // Use method directly today.setTimeInMillis(millis);//#else/* // Have to go indirect tempDate.setTime(millis); today.setTime(tempDate);*///#endif JAVA4 resetToDate(today); currentDate = new Date(getTimeInMillis(today));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -