JAVA常用时间类及ORACLE的时间格式
to_char(t.datelastupdated,'yyyy-mm-dd hh24:mi:ss:ff3')
,to_char(systimestamp,'YYYY-MM-DD HH24:MI:SS.FF3')
ORACLE的时间格式. 注意数据库中date类型和timestamp类型的区别
where
to_char(t.discharge_time,'yyyy-MM-dd HH24:mi:ss') >='2007-12-08 10:00:00'
and to_char(t.discharge_time,'yyyy-MM-dd HH24:mi:ss')<= '2007-12-24 10:30:00'
//
t.discharge_time >= to_date('1899-12-30 1:00:00','yyyy-MM-dd HH24:mi:ss')
and t.discharge_time <= to_date('2008-11-24 00:00:00', 'yyyy-MM-dd HH24:mi:ss')
select 1 from dba_tables t WHERE t.table_name = 'TBL_SWIFTS_OPER_INFO' and t.tablespace_name='USERS';
JAVA常用时间类
package saptac.core.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DateUtil {
protected final static Log log = LogFactory.getLog(DateUtil.class);
private static String defaultDatePattern = null;
private static String timePattern = "HH:mm";
public final static String DATE_Pattern = "yyyy-MM-dd hh:mm:ss";
public static final String DATE_Pattern_2 = "yyyy-MM-dd hh:mm:ss.S";
public static final void main (String a[]) {
java.util.Date d= new java.util.Date();
java.sql.Timestamp t = new Timestamp(d.getTime());
String ret = convertTimestampToString(t,DATE_Pattern);
System.out.println(ret);
java.util.Date d2= new java.util.Date();
java.sql.Date t2 = new java.sql.Date(d2.getTime());
String ret2 = convertSqlDateToString(t2, DATE_Pattern);
System.out.println(ret2);
String ret3 = convertSqlDateToString(t2, DATE_Pattern_2);
System.out.println(ret3);
SimpleDateFormat df = new SimpleDateFormat(DATE_Pattern);
String ret4 = df.format(t2);
java.sql.Date t5 = new java.sql.Date(d2.getTime());
java.util.Date t6 = (java.util.Date)t5;
String ret6 = getDateTime(DATE_Pattern,t6);
System.out.println(ret6);
}
/**
* 把java.sql.Date 转换成String
* @param timestamp
* @return
*/
public static String convertSqlDateToString(java.sql.Date date, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
String ret = df.format(date);
return ret;
}
/**
* 把java.sql.Timestamp 转换成String
* @param timestamp
* @return
*/
public static String convertTimestampToString(java.sql.Timestamp timestamp, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
String ret = df.format(timestamp);
return ret;
}
/**
* 获取当前时间
* @return yyyy
*/
public static String getCurrYear()
{
String ret = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy");
ret = df.format(new Timestamp(System.currentTimeMillis()));
return ret;
}
/**
* 获取当前时间
* @return date
*/
public static Date getNowDate()
{
return new Date(System.currentTimeMillis());
}
/**
* 获取当前时间
* @return MM
*
*/
public static String getCurrMonth()
{
String ret = "";
SimpleDateFormat df = new SimpleDateFormat("MM");
ret = df.format(new Timestamp(System.currentTimeMillis()));
return ret;
}
/**
* 获取下月时间
* @input yyyyMMdd
* @return date
*
*/
public static Date getEndYearMonth(String inputDate)
{
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Calendar ca= Calendar.getInstance();
try {
ca.setTime(df.parse(inputDate));
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
ca.set(Calendar.HOUR_OF_DAY, 23);
ca.set(Calendar.MINUTE, 59);
ca.set(Calendar.SECOND, 59);
} catch (ParseException e) {
e.printStackTrace();
}
return ca.getTime();
}
/**
* 获取当月时间
* @input yyyyMMdd
* @return date
* @throws Exception
*
*/
public static Date getYearMonth(String inputDate) throws Exception
{
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Calendar ca= Calendar.getInstance();
try {
Date date=df.parse(inputDate);
ca.set(Calendar.HOUR_OF_DAY, 0);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
ca.setTime(date);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
return ca.getTime();
}
/**
* 下月时间
* @return yyyyMM
*
*/
public static String getNextMonth()
{
Calendar ca= Calendar.getInstance();
ca.add(Calendar.MONTH, 1);
SimpleDateFormat df = new SimpleDateFormat("MM");
String ret = "";
ret = df.format(ca.getTime());
return ret;
}
/**
* 获取当前时间
* @return yyyy-MM-dd hh:mm:ss
*/
public static String getCurrTime()
{
String ret = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
ret = df.format(new Timestamp(System.currentTimeMillis()));
return ret;
}
/**固定日期格式
*
* @return
*/
public static synchronized String getDatePattern() {
defaultDatePattern = "yyyy-MM-dd";
return defaultDatePattern;
}
/**
* 固定日期+时间格式
* @return
*/
public static String getDateTimePattern() {
return DateUtil.getDatePattern() + " HH:mm:ss.S";
}
/**
* 转换时间格式
*
* @param aDate
* date from database as a string
* @return formatted string for the ui
*/
public static final String getDate(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(getDatePattern());
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* 转换字符转日期
*
* @param aMask
* the date pattern the string is in
* @param strDate
* a string representation of a date
* @return a converted Date object
* @see java.text.SimpleDateFormat
* @throws ParseException
*/
public static final Date convertStringToDate(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '"
+ aMask + "'");
}
try {
date = df.parse(strDate);
} catch (ParseException pe) {
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return (date);
}
/**
* 转换时间格式
* @param theTime
* the current time
* @return the current date/time
*/
public static String getTimeNow(Date theTime) {
return getDateTime(timePattern, theTime);
}
/**
* @return the current date
* @throws ParseException
*/
public static Calendar getToday() throws ParseException {
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
String todayAsString = df.format(today);
Calendar cal = new GregorianCalendar();
cal.setTime(convertStringToDate(todayAsString));
return cal;
}
/**
* 转换时间
* @param aMask
* the date pattern the string is in
* @param aDate
* a date object
* @return a formatted string representation of the date
*
* @see java.text.SimpleDateFormat
*/
public static final String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
log.error("aDate is null!");
} else {
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
*
* @param aDate
* A date to convert
* @return a string representation of the date
*/
public static final String convertDateToString(Date aDate) {
return getDateTime(getDatePattern(), aDate);
}
/**
* 字符转换时间
* @param strDate
* the date to convert (in format MM/dd/yyyy)
* @return a date object
*
* @throws ParseException
*/
public static Date convertStringToDate(String strDate)
{
Date aDate = null;
try {
if (log.isDebugEnabled()) {
log.debug("converting date with pattern: " + getDatePattern());
}
aDate = convertStringToDate(getDatePattern(), strDate);
} catch (ParseException pe) {
log.error("Could not convert '" + strDate
+ "' to a date, throwing exception");
pe.printStackTrace();
}
return aDate;
}
/**
* 用于将string转换成所需要的格式
* @param strDate 格式为YYYYMMDD的string
* @return str 格式为YYYY-MM-DD的string
*/
public static Timestamp converStringToSsNeedDate(String strDate)
{
Timestamp date=null;
String str = null;
if(strDate.length()==8){
str = strDate.substring(0,4)+"-"+strDate.substring(4,6)+"-"+strDate.substring(6,8)+" 00:00:00";
date=Timestamp.valueOf(str);
}
else{
str="2006-12-26 00:00:00";
date=Timestamp.valueOf(str);
}
return date;
}
}