获得当前系统时间的字符串格式
2018-08-19 本文已影响0人
神坛下的我
StringUtil.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class StringUtil {
public StringUtil(){}
private String timeStr1;
private String timeStr2;
public String getTimeStr1() {
Calendar c = Calendar.getInstance();
int year = c.get(c.YEAR);
int month = c.get(c.MONTH)+1;
int date = c.get(c.DAY_OF_MONTH);
int hour = c.get(c.HOUR_OF_DAY);
int minute = c.get(c.MINUTE);
int second = c.get(c.SECOND);
timeStr1 = year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
return timeStr1;
}
public void setTimeStr1(String timeStr1) {
this.timeStr1 = timeStr1;
}
public String getTimeStr2() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
timeStr2 = format.format(date);//格式化当前时间的Date对象
return timeStr2;
}
public void setTimeStr2(String timeStr2) {
this.timeStr2 = timeStr2;
}
}
index.jsp
<jsp:useBean id="strBean" class="com.count.StringUtil"></jsp:useBean>
<table>
<tr>
<td align="right">使用Calendar对象获得的当前时间:</td>
<td>
<jsp:getProperty property="timeStr1" name="strBean"/>
</td>
</tr>
<tr>
<td align="right">使用Date对象获得的当前时间:</td>
<td>
<jsp:getProperty property="timeStr2" name="strBean"/>
</td>
</tr>
</table>
14.PNG
15.PNG