importjava.sql.Timestamp;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;public classDateUtil {/*** set方法中是timestamp类型
* new Timestamp(s)
*@returns*/
publicTimestamp newStampTime(){long s=System.currentTimeMillis();return newTimestamp(s);
}/*** timeStamp转换为String
*@return
*/
publicString timeStampToString(){
SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
String str =df.format(now);returnstr;
}/*** String 转换为timestamp
*@return
*/
publicTimestamp StringTotimeStamp(){
SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= df.format(newDate());
Timestamp ts=Timestamp.valueOf(time);returnts;
}publicDate myStringTotimeStamp(String dateString){int length=dateString.length();if(length>10){
DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
Date today=newDate();try{
today= df.parse("2009-11-11");//String ts=df.format(today);
} catch(ParseException e) {
e.printStackTrace();
}returntoday;
}else{
DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
Date today=newDate();try{
today= df.parse("2009-11-11");//String ts=df.format(today);
} catch(ParseException e) {
e.printStackTrace();
}returntoday;
}
}/*** String 转换为timestamp
* String "2016-5-25" 转换为 Timestamp
*@return
*/
publicTimestamp StringTotimeStamp(String dateString){
SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
StringBuffer ds=newStringBuffer(dateString);if(dateString.length()>10){
Timestamp ts=Timestamp.valueOf(dateString);returnts;
}else{
ds.append(" 00:00:00");
Timestamp ts=Timestamp.valueOf(ds.toString());returnts;
}
}/*** 获取当前时间字符串
* eg:2016-10-11 16:57:52
**/
publicString formateDateString(){
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd H:m:s");
String dateString=format.format(newDate());returndateString;
}/*** 指定时间和当前时间比较大小*/
public booleancompareTime(String t1){if(t1.length()<=10){
t1=t1+" 23:59:59";
}
Date currentTime= new Date();//当前时间
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowtime=formatter.format(currentTime);
Calendar now=Calendar.getInstance();
Calendar c1=Calendar.getInstance();//String t1 = "2016-02-29 00:30:00";
try{
now.setTime(formatter.parse(nowtime));
c1.setTime(formatter.parse(t1));
}catch(ParseException e) {
e.printStackTrace();
}int result1 = now.compareTo(c1);//比开始时间小,未开始
System.out.println("结果:"+result1);if(result1>0){return false;
}else{return true;
}
}
}