第一种方式:
1 <% 2 java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat( 3 "yyyy-MM-dd HH:mm:ss"); 4 java.util.Date currentTime = new java.util.Date(); 5 String time = simpleDateFormat.format(currentTime).toString(); 6 out.println("当前时间为:"+time); 7 %>
第二种方式
1 <%! String formatDate(Date d){ 2 java.text.SimpleDateFormat formater= 3 new java.text.SimpleDateFormat("yyyy年MM月dd日"); 4 return formater.format(d); 5 } 6 %> 7 <%=formatDate(new Date()) %>
完整代码:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <%-- <% 25 java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat( 26 "yyyy-MM-dd HH:mm:ss"); 27 java.util.Date currentTime = new java.util.Date(); 28 String time = simpleDateFormat.format(currentTime).toString(); 29 out.println("当前时间为:"+time); 30 %> --%> 31 <%! String formatDate(Date d){ 32 java.text.SimpleDateFormat formater= 33 new java.text.SimpleDateFormat("yyyy年MM月dd日"); 34 return formater.format(d); 35 } 36 %> 37 <%=formatDate(new Date()) %> 38 </body> 39 </html>