📄 hsqldatetime.java
字号:
/**
* Sets the time in the given Calendar using the given milliseconds value; wrapper method to
* allow use of more efficient JDK1.4 method on JDK1.4 (was protected in earlier versions).
*
* @param cal the Calendar
* @param millis the time value in milliseconds
*/
private static void setTimeInMillis(Calendar cal, long millis) {
//#ifdef JDBC3
// Use method directly
cal.setTimeInMillis(millis);
//#else
/*
// Have to go indirect
synchronized (tempDate) {
tempDate.setTime(millis);
cal.setTime(tempDate);
}
*/
//#endif JDBC3
}
public static long getTimeInMillis(java.util.Date dt, Calendar source,
Calendar target) {
if (source == null) {
source = tempCalDefault;
}
if (target == null) {
target = tempCalDefault;
}
synchronized (tempCal) {
tempCal.setTimeZone(source.getTimeZone());
tempCal.setTime(dt);
tempCal.setTimeZone(target.getTimeZone());
return getTimeInMillis(tempCal);
}
}
/**
* Gets the time from the given Calendar as a milliseconds value; wrapper method to
* allow use of more efficient JDK1.4 method on JDK1.4 (was protected in earlier versions).
*
* @param cal the Calendar
* @return the time value in milliseconds
*/
public static long getTimeInMillis(Calendar cal) {
//#ifdef JDBC3
// Use method directly
return (cal.getTimeInMillis());
//#else
/*
// Have to go indirect
return (cal.getTime().getTime());
*/
//#endif JDBC3
}
public static long getNormalisedTime(long t) {
synchronized (tempCalDefault) {
setTimeInMillis(tempCalDefault, t);
resetToTime(tempCalDefault);
return getTimeInMillis(tempCalDefault);
}
}
public static Time getNormalisedTime(Time t) {
return new Time(getNormalisedTime(t.getTime()));
}
public static Time getNormalisedTime(Timestamp ts) {
return new Time(getNormalisedTime(ts.getTime()));
}
public static long getNormalisedDate(long d) {
synchronized (tempCalDefault) {
setTimeInMillis(tempCalDefault, d);
resetToDate(tempCalDefault);
return getTimeInMillis(tempCalDefault);
}
}
public static Date getNormalisedDate(Timestamp ts) {
synchronized (tempCalDefault) {
setTimeInMillis(tempCalDefault, ts.getTime());
resetToDate(tempCalDefault);
long value = getTimeInMillis(tempCalDefault);
return new Date(value);
}
}
public static Date getNormalisedDate(Date d) {
synchronized (tempCalDefault) {
setTimeInMillis(tempCalDefault, d.getTime());
resetToDate(tempCalDefault);
long value = getTimeInMillis(tempCalDefault);
return new Date(value);
}
}
public static Timestamp getNormalisedTimestamp(Time t) {
synchronized (tempCalDefault) {
setTimeInMillis(tempCalDefault, System.currentTimeMillis());
resetToDate(tempCalDefault);
long value = getTimeInMillis(tempCalDefault) + t.getTime();
return new Timestamp(value);
}
}
public static Timestamp getNormalisedTimestamp(Date d) {
synchronized (tempCalDefault) {
setTimeInMillis(tempCalDefault, d.getTime());
resetToDate(tempCalDefault);
long value = getTimeInMillis(tempCalDefault);
return new Timestamp(value);
}
}
/**
* Returns the indicated part of the given <code>java.util.Date</code> object.
* @param d the <code>Date</code> object from which to extract the indicated part
* @param part an integer code corresponding to the desired date part
* @return the indicated part of the given <code>java.util.Date</code> object
*/
static int getDateTimePart(java.util.Date d, int part) {
synchronized (tempCalDefault) {
tempCalDefault.setTime(d);
return tempCalDefault.get(part);
}
}
private final static char[][] dateTokens = {
{
'R', 'R', 'R', 'R'
}, {
'I', 'Y', 'Y', 'Y'
}, {
'Y', 'Y', 'Y', 'Y'
}, {
'I', 'Y'
}, {
'Y', 'Y'
}, {
'B', 'C'
}, {
'B', '.', 'C', '.'
}, {
'A', 'D'
}, {
'A', '.', 'D', '.'
}, {
'M', 'O', 'N'
}, {
'M', 'O', 'N', 'T', 'H'
}, { 'D' }, {
'I', 'W'
}, {
'D', 'D'
}, {
'D', 'D', 'D'
}, {
'H', 'H', '2', '4'
}, {
'H', 'H', '1', '2'
}, {
'H', 'H'
}, {
'M', 'I',
}, {
'S', 'S'
}, {
'A', 'M'
}, {
'P', 'M',
}, {
'A', '.', 'M', '.'
}, {
'P', '.', 'M', '.'
}
};
private final static String[] javaDateTokens = {
"yyyy", "yyyy", "yyyy", "yy", "yy", "G", "G", "G", "G", "MMM",
"MMMMM", "E", "w", "dd", "D", "k", "K", "K", "mm", "ss", "aaa", "aaa",
"aaa", "aaa"
};
/** Indicates end-of-input */
public static final char e = 0xffff;
/**
* Converts the given format into a pattern accepted by <code>java.text.SimpleDataFormat</code>
* @param format
* @return
*/
public static String toJavaDatePattern(String format) {
int len = format.length();
char ch;
StringBuffer pattern = new StringBuffer(len);
Tokenizer tokenizer = new Tokenizer();
for (int i = 0; i <= len; i++) {
ch = (i == len) ? e
: format.charAt(i);
if (!tokenizer.next(ch, dateTokens)) {
int index = tokenizer.getLastMatch();
if (index >= 0) {
pattern.setLength(pattern.length() - tokenizer.length());
pattern.append(javaDateTokens[index]);
}
tokenizer.reset();
if (tokenizer.isConsumed()) {
continue;
}
}
pattern.append(ch);
}
return pattern.substring(0, pattern.length() - 1);
}
/**
* This class can match 64 tokens at maximum.
*/
static class Tokenizer {
private int last;
private int offset;
private long state;
private boolean consumed;
public Tokenizer() {
reset();
}
/**
* Resets for next reuse.
*
*/
public void reset() {
last = -1;
offset = -1;
state = 0;
}
/**
* Returns a length of a token to match.
* @return
*/
public int length() {
return offset;
}
/**
* Returns an index of the last matched token.
* @return
*/
public int getLastMatch() {
return last;
}
/**
* Indicates whethe the last character has been consumed by the matcher.
* @return
*/
public boolean isConsumed() {
return consumed;
}
/**
* Checks whether the specified bit is not set.
* @param bit
* @return
*/
private boolean isZeroBit(int bit) {
return (state & (1L << bit)) == 0;
}
/**
* Sets the specified bit.
* @param bit
*/
private void setBit(int bit) {
state |= (1L << bit);
}
/**
* Matches the specified character against tokens.
* @param ch
* @param tokens
* @return
*/
public boolean next(char ch, char[][] tokens) {
// Use local variable for performance
int index = ++offset;
int len = offset + 1;
int left = 0;
consumed = false;
for (int i = tokens.length; --i >= 0; ) {
if (isZeroBit(i)) {
if (tokens[i][index] == ch) {
consumed = true;
if (tokens[i].length == len) {
setBit(i);
last = i;
} else {
++left;
}
} else {
setBit(i);
}
}
}
return left > 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -