Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
9b8c9f5b
提交
9b8c9f5b
authored
8月 20, 2008
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MySQL date functions
上级
7cf83ef5
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
177 行增加
和
0 行删除
+177
-0
FunctionsMySQL.java
h2/src/tools/org/h2/mode/FunctionsMySQL.java
+162
-0
package.html
h2/src/tools/org/h2/mode/package.html
+15
-0
没有找到文件。
h2/src/tools/org/h2/mode/FunctionsMySQL.java
0 → 100644
浏览文件 @
9b8c9f5b
/*
* Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: Jason Brittain (jason.brittain at gmail.com)
*/
package
org
.
h2
.
mode
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
org.h2.util.StringUtils
;
/**
* This class implements some MySQL-specific functions.
*
* @author Jason Brittain
* @author Thomas Mueller
*/
public
class
FunctionsMySQL
{
/**
* The date format of a MySQL formatted date/time.
* Example: 2008-09-25 08:40:59
*/
private
static
final
String
DATE_TIME_FORMAT
=
"yyyy-MM-dd HH:mm:ss"
;
/**
* Format replacements for MySQL date formats.
* See
* http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
*/
private
static
final
String
[]
FORMAT_REPLACE
=
new
String
[]
{
"%a"
,
"EEE"
,
"%b"
,
"MMM"
,
"%c"
,
"MM"
,
"%d"
,
"dd"
,
"%e"
,
"d"
,
"%H"
,
"HH"
,
"%h"
,
"hh"
,
"%I"
,
"hh"
,
"%i"
,
"mm"
,
"%j"
,
"DDD"
,
"%k"
,
"H"
,
"%l"
,
"h"
,
"%M"
,
"MMMM"
,
"%m"
,
"MM"
,
"%p"
,
"a"
,
"%r"
,
"hh:mm:ss a"
,
"%S"
,
"ss"
,
"%s"
,
"ss"
,
"%T"
,
"HH:mm:ss"
,
"%W"
,
"EEEE"
,
"%w"
,
"F"
,
"%Y"
,
"yyyy"
,
"%y"
,
"yy"
,
"%%"
,
"%"
,
};
/**
* Register the functionality in the database.
* Nothing happens if the functions are already registered.
*
* @param conn the connection
*/
public
static
void
register
(
Connection
conn
)
throws
SQLException
{
String
[]
init
=
new
String
[]
{
"UNIX_TIMESTAMP"
,
"unixTimestamp"
,
"FROM_UNIXTIME"
,
"fromUnixTime"
,
"DATE"
,
"date"
,
};
Statement
stat
=
conn
.
createStatement
();
for
(
int
i
=
0
;
i
<
init
.
length
;
i
+=
2
)
{
String
alias
=
init
[
i
],
method
=
init
[
i
+
1
];
stat
.
execute
(
"CREATE ALIAS IF NOT EXISTS "
+
alias
+
" FOR \""
+
FunctionsMySQL
.
class
.
getName
()
+
"."
+
method
+
"\""
);
}
}
/**
* Get the seconds since 1970-01-01 00:00:00 UTC.
* See
* http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp
*
* @return the current timestamp in seconds (not milliseconds).
*/
public
static
int
unixTimestamp
()
{
return
(
int
)
(
System
.
currentTimeMillis
()
/
1000L
);
}
/**
* Get the seconds since 1970-01-01 00:00:00 UTC of the given timestamp.
* See
* http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp
*
* @param timestamp the timestamp
* @return the current timestamp in seconds (not milliseconds).
*/
public
static
int
unixTimestamp
(
java
.
sql
.
Timestamp
timestamp
)
throws
SQLException
{
return
(
int
)
(
timestamp
.
getTime
()
/
1000L
);
}
/**
* See
* http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
*
* @param seconds The current timestamp in seconds.
* @return a formatted date/time String in the format "yyyy-MM-dd HH:mm:ss".
*/
public
static
String
fromUnixTime
(
int
seconds
)
{
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
DATE_TIME_FORMAT
);
return
formatter
.
format
(
new
Date
(
seconds
*
1000L
));
}
/**
* See
* http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
*
* @param seconds The current timestamp in seconds.
* @param format The format of the date/time String to return.
* @return a formatted date/time String in the given format.
*/
public
static
String
fromUnixTime
(
int
seconds
,
String
format
)
{
format
=
convertToSimpleDateFormat
(
format
);
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
format
);
return
formatter
.
format
(
new
Date
(
seconds
*
1000L
));
}
private
static
String
convertToSimpleDateFormat
(
String
format
)
{
String
[]
replace
=
FORMAT_REPLACE
;
for
(
int
i
=
0
;
i
<
replace
.
length
;
i
+=
2
)
{
format
=
StringUtils
.
replaceAll
(
format
,
replace
[
i
],
replace
[
i
+
1
]);
}
return
format
;
}
/**
* See
* http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date
* This function is dependent on the exact formatting of the MySQL date/time
* string.
*
* @param dateTime The date/time String from which to extract just the date
* part.
* @return the date part of the given date/time String argument.
*/
public
static
String
date
(
String
dateTime
)
{
if
(
dateTime
==
null
)
{
return
null
;
}
int
index
=
dateTime
.
indexOf
(
' '
);
if
(
index
!=
-
1
)
{
return
dateTime
.
substring
(
0
,
index
);
}
return
dateTime
;
}
}
h2/src/tools/org/h2/mode/package.html
0 → 100644
浏览文件 @
9b8c9f5b
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License, Version 1.0,,
and under the Eclipse Public License, Version 1.0
(http://h2database.com/html/license.html).
Initial Developer: H2 Group
-->
<html
xmlns=
"http://www.w3.org/1999/xhtml"
lang=
"en"
xml:lang=
"en"
>
<head><meta
http-equiv=
"Content-Type"
content=
"text/html;charset=utf-8"
/><title>
Javadoc package documentation
</title></head><body
style=
"font: 9pt/130% Tahoma, Arial, Helvetica, sans-serif; font-weight: normal;"
>
Utility classes for compatibility with other database, for example MySQL.
</body></html>
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论