/*
* DateUtil Class : 在于提供关于时间操作的一系列公用操作方法
* Create Date : 2004-2-9
* Version 1.0
*/
import java.sql.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class DateUtil {
public DateUtil() {
}
/**
* 根据格式获得日期字符串
* @param sFormat
* @return
*/
public static String getDateStr(String sFormat) {
Calendar tCal = Calendar.getInstance();
Timestamp ts = new Timestamp(tCal.getTime().getTime());
java.util.Date date = new java.util.Date(ts.getTime());
SimpleDateFormat formatter = new SimpleDateFormat(sFormat);
String tmpStr = formatter.format(date);
return (tmpStr);
}
/**
* 根据给定格式获取特定时间的格式化显示
* @param ts
* @param sFormat
* @return
*/
public static String getDateFormat(Timestamp ts, String sFormat) {
Date date = new Date(ts.getTime());
SimpleDateFormat formatter
= new SimpleDateFormat(sFormat);
String tmpStr = formatter.format(date);
return tmpStr;
}
/**
* 格式化日期
* @param ts
* @return
*/
public static String getSDate(Timestamp ts) {
Date date = new Date(ts.getTime());
SimpleDateFormat formatter
= new SimpleDateFormat("yyyy-MM-dd");
String tmpStr = formatter.format(date);
return tmpStr;
}
/**
* 将String类型的日期转换为时间
* @param dt
* @return
*/
public static Timestamp getTs(String dt) {
Date date = java.sql.Date.valueOf(dt);
Calendar tCal = Calendar.getInstance();
tCal.setTime(date);
Timestamp ts = new Timestamp(tCal.getTime().getTime());
return ts;
}
/**
* 建议获得短日期的处理方式
* 例如: getShortDate(2004-10-10 10:10:10.123) = 2004-10-10
* @param dt
* @return
*/
public static String getShortDate(String dt) {
try {
return dt.substring(0, dt.indexOf(" "));
}
catch (Exception e) {
return dt;
}
}
/**
*
* 取得当前日期时间
* @return
*/
public static Timestamp getCurrDateTime() {
Calendar tCal = Calendar.getInstance();
Timestamp createDate = new Timestamp(tCal.getTime().getTime());
return createDate;
}
/**
* 获得最常见的日期格式内容 : 年-月-日 小时-分钟-秒
* @param ts
* @return
*/
public static String getSDateTime(Timestamp ts) {
return getDateFormat(ts, "yyyy-mm-dd HH:mm:ss");
}
/*格式化日期*/
public static String getSTime(Timestamp ts) {
return getDateFormat(ts, "HH:mm:ss");
}
/**
* 获取当天的日期
* @return
*/
public static String getToday() {
java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(ts);
}
// 根据时间获得随机数
public static String getRnd()
{
Calendar tCal = Calendar.getInstance();
Timestamp ts = new Timestamp(tCal.getTime().getTime());
java.util.Date date = new java.util.Date(ts.getTime());
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddhhmmss");
String tmpStr = formatter.format(date)+Math.round(Math.random()*1000+1);
return (tmpStr);
}
/**
* 计算日期之间的差值 2004-3-25 增加
* @param dt1
* @param dt2
* @return
*/
public static int dateDiff(Timestamp dt1, Timestamp dt2) {
long ldate1 = dt1.getTime();
long ldate2 = dt2.getTime();
return (int)((ldate2-ldate1) / (1000*60*60*24));
}
/**
* 获取明天的日期
* @return
*/
public static String getTomorrow() {
return getNextDay(getToday());
}
/**
* 获得当前日期的下一天
* @param date
* @return
*/
public static String getNextDay(String date) {
if (date == null || date.trim().length() == 0) {
return "";
}
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(f.parse(date));
}
catch (ParseException ex) {
return date;
}
calendar.add(5, 1);
return f.format(calendar.getTime());
}
/**
* LST
* num为正:当前日期后num天是返回值
* num为负:当前日期前num天是返回值
* 返回的日期的格式:yyyy-MM-dd
*/
public static String getTheDay(int num){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar gc = new GregorianCalendar();
gc.add(GregorianCalendar.DATE, num);
Date theday =gc.getTime();
return sdf.format(theday);
}
}