jsp 日期格式:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="javax.servlet.*,java.text.*" %>
<html>
<head>
<title>顯示當前時間與日期</title>
</head>
<body>
<h1>顯示當前時間與日期</h1>
<%
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
out.print( "<h2 align=\"center\">" + ft.format(dNow) + "</h2>");
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
out.print( "<h2 align=\"center\">" + ft.format(dNow) + "</h2>");
%>
</body>
</html>
sql 日期格式請參考:
http://sqlhints.com/2014/02/03/how-to-get-day-month-year-and-time-part-from-datetime-in-sql-server/
SQL的話~
你裡面下Year函數就會是只有年分
select year(欄位) as YearNum from 表格
SQL語法可以,或是用程式再去用也可以看你
MYSQL SELECT NOW(),DATE_FORMAT(NOW(),'%Y')
MSSQL SELECT CONVERT(varchar(4),GETDATE(), 126);
OracleSELECT to_char(sysdate,'YYYY') FROM DUAL;
https://stackoverflow.com/questions/6162401/convert-and-format-a-date-in-jsp
import java.util.Date;
import java.text.SimpleDateFormat;
public class HelloWorld{
public static void main(String []args){
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
String dateString = formatter.format(currentTime);
System.out.println(dateString);
}
}
應該就改成yyyy就有年了
記得
import java.text.SimpleDateFormat;