时间戳转化为Date(or String)
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Long time=new Long(445555555);
String d = format.format(time);
Date date=format.parse(d);
System.out.println("Format To String(Date):"+d);
System.out.println("Format To Date:"+date);
运行结果:
Format To String(Date):1970-01-06 11:45:55
Format To Date:Tue Jan 06 11:45:55 CST 1970
Date(or String)转化为时间戳
SimpleDateFormat format = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
String time=" 1970-01-06 11:45:55 ";//注:改正后这里前后也加了空格
Date date = format.parse(time);
System.out.print("Format To times:"+date.getTime());
运行结果:
Format To times:445555000
获取当前时间
1.通过Date类来获取时间 (常用)
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String str=sdf.format(new Date());
System.out.println("当前时间是:"+str);
2.通过System类中的currentTimeMillis方法来获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String str=sdf.format(System.currentTimeMillis());
System.out.println("当前时间是:"+str);
3.通过Calendar类来获取当前时间
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "-" + month + "-" + date + " " +hour + ":" +minute + ":" + second);
(注意:月份从0开始所以要+1)
4. LocalDateTime类来获取当前时间
LocalDateTime dt = LocalDateTime.now();
int year = dt.getYear();
int month = dt.getMonthValue();
int date =dt.getDayOfMonth();
int hour = dt.getHour();
int minute = dt.getMinute();
int second = dt.getSecond();
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
例子:
public class Test {
public static void main(String[] args) throws ParseException {
//获取当前时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String str=sdf.format(new Date());
System.out.println("当前时间是:"+str);
//将Date转换成时间戳
Date date = sdf.parse(str);
System.out.print("Format To times:"+date.getTime());
}
}
项目中时间过滤:
//时间过滤
String opTime1 = this.getProperty("opTime1");//开始时间
String opTime2 = this.getProperty("opTime2");//结束时间
if(opTime1!=null&&!"".equals(opTime1)){
params.addParam("fillDate",Timestamp.valueOf(opTime1),QueryParam.RELATION_GE);
}
if(opTime2!=null&&!"".equals(opTime2)){
Timestamp endDate = CommonUtil.addDay(Timestamp.valueOf(opTime2), 1);
params.addParam("fillDate",endDate,QueryParam.RELATION_LE);
}
//时间排序
SortParamList sortParamList =new SortParamList();
sortParamList.addParam("fillDate", SortParam.SORT_TYPE_DESCENDING);
//Jsp
<td width="60" align="right">会议时间</td>
<td width="180" align="left"><hy:date id="wzModel" upload="true" cssStyle="width:100%;height:18px" editor="date"/></td>
<td width="40" align="right">到</td>
<td align="left" width="15%"><hy:date id="_fillDate2" editor="date" cssStyle="width:100%"upload="true" />
知识点:editor 日期格式:month—年月,date--年月日,datetime-年月日时分秒,默认为date