{"id":224852,"date":"2021-09-03T09:44:54","date_gmt":"2021-09-03T01:44:54","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=224852"},"modified":"2021-08-23T08:47:38","modified_gmt":"2021-08-23T00:47:38","slug":"jsp-date-handling","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/jsp-date-handling.html","title":{"rendered":"JSP \u65e5\u671f\u5904\u7406\u6982\u8ff0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4f7f\u7528JSP\u6700\u91cd\u8981\u7684\u4f18\u52bf\u4e4b\u4e00\uff0c\u5c31\u662f\u53ef\u4ee5\u4f7f\u7528\u6240\u6709Java API\u3002\u672c\u7ae0\u5c06\u4f1a\u8be6\u7ec6\u5730\u8bb2\u8ff0Java\u4e2d\u7684Date\u7c7b\uff0c\u5b83\u5728java.util\u5305\u4e0b\uff0c\u5c01\u88c5\u4e86\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

Date\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u3002\u7b2c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u4f7f\u7528\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u3002<\/p>\n

Date( )<\/pre>\n

\u7b2c\u4e8c\u4e2a\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u8868\u793a\u4ece1970\u5e741\u67081\u65e5\u51cc\u6668\u81f3\u6240\u8981\u8868\u793a\u65f6\u95f4\u7684\u6beb\u79d2\u6570\u3002<\/p>\n

Date(long millisec)<\/pre>\n

\u83b7\u53d6Date\u5bf9\u8c61\u540e\uff0c\u60a8\u5c31\u80fd\u591f\u4f7f\u7528\u4e0b\u8868\u5217\u51fa\u7684\u6240\u6709\u65b9\u6cd5\uff1a<\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\t\t\t\t\u5e8f\u53f7<\/strong><\/th>\n\n\t\t\t\t\u65b9\u6cd5<\/strong> & <\/strong>\u63cf\u8ff0<\/strong><\/th>\n<\/tr>\n
\n\t\t\t\t1<\/td>\n\n\t\t\t\tboolean after(Date date)<\/strong><\/p>\n

\t\t\t\t\u5982\u679c\u6bd4\u7ed9\u5b9a\u7684\u65e5\u671f\u665a\uff0c\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse<\/td>\n<\/tr>\n

\n\t\t\t\t2<\/td>\n\n\t\t\t\tboolean before(Date date)<\/strong><\/p>\n

\t\t\t\t\u5982\u679c\u6bd4\u7ed9\u5b9a\u7684\u65e5\u671f\u65e9\uff0c\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse<\/td>\n<\/tr>\n

\n\t\t\t\t3<\/td>\n\n\t\t\t\tObject clone( )<\/strong><\/p>\n

\t\t\t\t\u83b7\u53d6\u5f53\u524d\u5bf9\u8c61\u7684\u4e00\u4e2a\u526f\u672c<\/td>\n<\/tr>\n

\n\t\t\t\t4<\/td>\n\n\t\t\t\tint compareTo(Date date)<\/strong><\/p>\n

\t\t\t\t\u5982\u679c\u4e0e\u7ed9\u5b9a\u65e5\u671f\u76f8\u7b49\uff0c\u5219\u8fd4\u56de0\uff0c\u5982\u679c\u6bd4\u7ed9\u5b9a\u65e5\u671f\u65e9\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff0c\u5982\u679c\u6bd4\u7ed9\u5b9a\u65e5\u671f\u665a\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u6b63\u6570<\/td>\n<\/tr>\n

\n\t\t\t\t5<\/td>\n\n\t\t\t\tint compareTo(Object obj)<\/strong><\/p>\n

\t\t\t\t\u4e0e compareTo(Date) \u65b9\u6cd5\u76f8\u540c\uff0c\u5982\u679c obj \u4e0d\u662fDate\u7c7b\u6216\u5176\u5b50\u7c7b\u7684\u5bf9\u8c61\uff0c\u629b\u51faClassCastException\u5f02\u5e38<\/td>\n<\/tr>\n

\n\t\t\t\t6<\/td>\n\n\t\t\t\tboolean equals(Object date)<\/strong><\/p>\n

\t\t\t\t\u5982\u679c\u4e0e\u7ed9\u5b9a\u65e5\u671f\u76f8\u540c\uff0c\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse<\/td>\n<\/tr>\n

\n\t\t\t\t7<\/td>\n\n\t\t\t\tlong getTime( )<\/strong><\/p>\n

\t\t\t\t\u8fd4\u56de\u4ece1970\u5e741\u67081\u65e5\u51cc\u6668\u81f3\u6b64\u5bf9\u8c61\u6240\u8868\u793a\u65f6\u95f4\u7684\u6beb\u79d2\u6570<\/td>\n<\/tr>\n

\n\t\t\t\t8<\/td>\n\n\t\t\t\tint hashCode( )<\/strong><\/p>\n

\t\t\t\t\u8fd4\u56de\u6b64\u5bf9\u8c61\u7684\u54c8\u5e0c\u7801<\/td>\n<\/tr>\n

\n\t\t\t\t9<\/td>\n\n\t\t\t\tvoid setTime(long time)<\/strong><\/p>\n

\t\t\t\t\u4f7f\u7528\u7ed9\u5b9a\u53c2\u6570\u8bbe\u7f6e\u65f6\u95f4\u548c\u65e5\u671f\uff0c\u53c2\u6570time\u8868\u793a\u4ece1970\u5e741\u67081\u65e5\u51cc\u6668\u81f3time\u6240\u7ecf\u8fc7\u7684\u6beb\u79d2\u6570<\/td>\n<\/tr>\n

\n\t\t\t\t10<\/td>\n\n\t\t\t\tString toString( )<\/strong><\/p>\n

\t\t\t\t\u5c06\u6b64\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u8fd9\u4e2a\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/div>\n

\u4f7f\u7528JSP\u7f16\u7a0b\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u53ea\u8981\u4f7f\u7528Date\u5bf9\u8c61\u7684toString()\u65b9\u6cd5\u5c31\u884c\u4e86\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n

<%@<\/span> page language<\/span>=<\/span>\"java\"<\/span> contentType<\/span>=<\/span>\"text\/html; charset=UTF-8\"<\/span>\r\n    pageEncoding<\/span>=<\/span>\"UTF-8\"<\/span>%>\r\n<\/span><%@<\/span> page <\/span>import<\/span>=<\/span>\"java.io.*,java.util.*, javax.servlet.*\"<\/span> %>\r\n<\/span><html><\/span>\r\n<\/span><head><\/span>\r\n<\/span><title><\/span>\u663e\u793a\u5f53\u524d\u65f6\u95f4\u4e0e\u65e5\u671f<\/span><\/title><\/span>\r\n<\/span><\/head><\/span>\r\n<\/span><body><\/span>\r\n\r\n<\/span><h1><\/span>\u663e\u793a\u5f53\u524d\u65f6\u95f4\u4e0e\u65e5\u671f<\/span><\/h1><\/span>\r\n\r\n<\/span><%<\/span>\r\n   <\/span>Date<\/span> date <\/span>=<\/span> <\/span>new<\/span> <\/span>Date<\/span>();<\/span>\r\n   <\/span>out<\/span>.<\/span>print<\/span>(<\/span> <\/span>\"<h2 align=\\\"center\\\">\"<\/span> <\/span>+<\/span>date<\/span>.<\/span>toString<\/span>()+<\/span>\"<\/h2>\"<\/span>);<\/span>\r\n%>\r\n<\/span><\/body><\/span>\r\n<\/span><\/html><\/span><\/pre>\n

\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5728 main.jsp \u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u8bbf\u95ee http:\/\/localhost:8080\/testjsp\/main.jsp\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\u663e\u793a\u5f53\u524d\u65f6\u95f4\u4e0e\u65e5\u671f<\/span>\r\n\r\n<\/span>Sat<\/span> <\/span>Jun<\/span> <\/span>25<\/span> <\/span>17<\/span>:<\/span>54<\/span>:<\/span>34<\/span> CST <\/span>2016<\/span><\/pre>\n

\u5237\u65b0 http:\/\/localhost:8080\/testjsp\/main.jsp\uff0c\u5c31\u53ef\u4ee5\u53d1\u73b0\u6bcf\u6b21\u5237\u65b0\u6240\u5f97\u5230\u7684\u79d2\u6570\u90fd\u4e0d\u76f8\u540c\u3002<\/p>\n

\u65e5\u671f\u6bd4\u8f83<\/strong><\/div>\n

\u5c31\u50cf\u6211\u5728\u5f00\u5934\u6240\u63d0\u5230\u7684\uff0c\u60a8\u53ef\u4ee5\u5728JSP\u811a\u672c\u4e2d\u4f7f\u7528\u4efb\u4f55Java\u65b9\u6cd5\u3002\u5982\u679c\u60a8\u60f3\u8981\u6bd4\u8f83\u4e24\u4e2a\u65e5\u671f\uff0c<\/p>\n

\u53ef\u4ee5\u53c2\u7167\u4e0b\u9762\u7684\u65b9\u6cd5\u6765\u505a\uff1a<\/p>\n

    \n
  1. \u4f7f\u7528getTime()\u65b9\u6cd5\u5f97\u5230\u6beb\u79d2\u6570\uff0c\u7136\u540e\u6bd4\u8f83\u6beb\u79d2\u6570\u5c31\u884c\u4e86\u3002<\/li>\n
  2. \u4f7f\u7528before()\uff0cafter()\uff0cequals()\u65b9\u6cd5\u3002\u6bd4\u5982\uff0cnew Date(99,2,12).before(new Date(99,2,18))\u8fd4\u56detrue\u3002<\/li>\n
  3. \u4f7f\u7528compareTo()\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5728Comparable\u63a5\u53e3\u4e2d\u5b9a\u4e49\uff0c\u5728Date\u4e2d\u5b9e\u73b0\u3002<\/li>\n<\/ol>\n
    \u4f7f\u7528SimpleDateFormat\u683c\u5f0f\u5316\u65e5\u671f<\/strong><\/div>\n

    SimpleDateFormat\u4f7f\u7528\u4e00\u79cd\u5730\u533a\u654f\u611f\u7684\u65b9\u5f0f\u6765\u683c\u5f0f\u5316\u548c\u89e3\u6790\u65e5\u671f\uff0c\u5b83\u5141\u8bb8\u60a8\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6a21\u5f0f\u6765\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n

    \u5bf9CurrentDate.jsp\u7a0d\u4f5c\u4fee\u6539\uff0c\u5f97\u5230\u5982\u4e0b\u4fee\u6539\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n

    <%@<\/span> page language<\/span>=<\/span>\"java\"<\/span> contentType<\/span>=<\/span>\"text\/html; charset=UTF-8\"<\/span>\r\n    pageEncoding<\/span>=<\/span>\"UTF-8\"<\/span>%>\r\n<\/span><%@<\/span> page <\/span>import<\/span>=<\/span>\"java.io.*,java.util.*\"<\/span> %>\r\n<\/span><%@<\/span> page <\/span>import<\/span>=<\/span>\"javax.servlet.*,java.text.*\"<\/span> %>\r\n<\/span><html><\/span>\r\n<\/span><head><\/span>\r\n<\/span><title><\/span>\u663e\u793a\u5f53\u524d\u65f6\u95f4\u4e0e\u65e5\u671f<\/span><\/title><\/span>\r\n<\/span><\/head><\/span>\r\n<\/span><body><\/span>\r\n\r\n<\/span><h1><\/span>\u663e\u793a\u5f53\u524d\u65f6\u95f4\u4e0e\u65e5\u671f<\/span><\/h1><\/span>\r\n\r\n<\/span><%<\/span>\r\n   <\/span>Date<\/span> dNow <\/span>=<\/span> <\/span>new<\/span> <\/span>Date<\/span>(<\/span> <\/span>);<\/span>\r\n   <\/span>SimpleDateFormat<\/span> ft <\/span>=<\/span> \r\n   <\/span>new<\/span> <\/span>SimpleDateFormat<\/span> <\/span>(<\/span>\"yyyy-MM-dd HH:mm:ss\"<\/span>);<\/span>\r\n   <\/span>out<\/span>.<\/span>print<\/span>(<\/span> <\/span>\"<h2 align=\\\"center\\\">\"<\/span> <\/span>+<\/span> ft<\/span>.<\/span>format<\/span>(<\/span>dNow<\/span>)<\/span> <\/span>+<\/span> <\/span>\"<\/h2>\"<\/span>);<\/span>\r\n%>\r\n\r\n<\/span><\/body><\/span>\r\n<\/span><\/html><\/span><\/pre>\n

    \u518d\u6b21\u7f16\u8bd1 main.jsp\uff0c\u7136\u540e\u8bbf\u95ee http:\/\/localhost:8080\/testjsp\/main.jsp\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n

    \u663e\u793a\u5f53\u524d\u65f6\u95f4\u4e0e\u65e5\u671f<\/span>\r\n\r\n<\/span>2016<\/span>-<\/span>06<\/span>-<\/span>25<\/span> <\/span>17<\/span>:<\/span>57<\/span>:<\/span>53<\/span><\/pre>\n
    SimpleDateFormat\u683c\u5f0f\u7801<\/strong><\/div>\n

    \u8981\u6307\u5b9a\u6a21\u5f0f\u5b57\u7b26\u4e32\uff0c\u9700\u8981\u4f7f\u7528\u4e0b\u8868\u5217\u51fa\u7684\u683c\u5f0f\u7801\uff1a<\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
    \n\t\t\t\t\u5b57\u7b26<\/strong><\/th>\n\n\t\t\t\t\u63cf\u8ff0<\/strong><\/th>\n\n\t\t\t\t\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n
    \n\t\t\t\tG<\/td>\n\n\t\t\t\t\u65f6\u4ee3\u6807\u8bc6\u7b26<\/td>\n\n\t\t\t\tAD<\/td>\n<\/tr>\n
    \n\t\t\t\ty<\/td>\n\n\t\t\t\t4\u4f4d\u6570\u5e74\u4efd<\/td>\n\n\t\t\t\t2001<\/td>\n<\/tr>\n
    \n\t\t\t\tM<\/td>\n\n\t\t\t\t\u6708<\/td>\n\n\t\t\t\tJuly or 07<\/td>\n<\/tr>\n
    \n\t\t\t\td<\/td>\n\n\t\t\t\t\u65e5<\/td>\n\n\t\t\t\t10<\/td>\n<\/tr>\n
    \n\t\t\t\th<\/td>\n\n\t\t\t\t12\u5c0f\u65f6\u5236\uff0c A.M.\/P.M. (1~12)<\/td>\n\n\t\t\t\t12<\/td>\n<\/tr>\n
    \n\t\t\t\tH<\/td>\n\n\t\t\t\t24\u5c0f\u65f6\u5236<\/td>\n\n\t\t\t\t22<\/td>\n<\/tr>\n
    \n\t\t\t\tm<\/td>\n\n\t\t\t\t\u5206\u949f<\/td>\n\n\t\t\t\t30<\/td>\n<\/tr>\n
    \n\t\t\t\ts<\/td>\n\n\t\t\t\t\u79d2<\/td>\n\n\t\t\t\t55<\/td>\n<\/tr>\n
    \n\t\t\t\tS<\/td>\n\n\t\t\t\t\u6beb\u79d2<\/td>\n\n\t\t\t\t234<\/td>\n<\/tr>\n
    \n\t\t\t\tE<\/td>\n\n\t\t\t\t\u661f\u671f<\/td>\n\n\t\t\t\tTuesday<\/td>\n<\/tr>\n
    \n\t\t\t\tD<\/td>\n\n\t\t\t\t\u4e00\u5e74\u4e2d\u7684\u67d0\u5929<\/td>\n\n\t\t\t\t360<\/td>\n<\/tr>\n
    \n\t\t\t\tF<\/td>\n\n\t\t\t\t\u4e00\u4e2a\u6708\u4e2d\u67d0\u661f\u671f\u7684\u67d0\u5929<\/td>\n\n\t\t\t\t2 (second Wed. in July)<\/td>\n<\/tr>\n
    \n\t\t\t\tw<\/td>\n\n\t\t\t\t\u4e00\u5e74\u4e2d\u7684\u67d0\u661f\u671f<\/td>\n\n\t\t\t\t40<\/td>\n<\/tr>\n
    \n\t\t\t\tW<\/td>\n\n\t\t\t\t\u4e00\u4e2a\u6708\u4e2d\u7684\u67d0\u661f\u671f<\/td>\n\n\t\t\t\t1<\/td>\n<\/tr>\n
    \n\t\t\t\ta<\/td>\n\n\t\t\t\tA.M.\/P.M. \u6807\u8bb0<\/td>\n\n\t\t\t\tPM<\/td>\n<\/tr>\n
    \n\t\t\t\tk<\/td>\n\n\t\t\t\t\u4e00\u5929\u4e2d\u7684\u67d0\u4e2a\u5c0f\u65f6 (1~24)<\/td>\n\n\t\t\t\t24<\/td>\n<\/tr>\n
    \n\t\t\t\tK<\/td>\n\n\t\t\t\t\u4e00\u5929\u4e2d\u7684\u67d0\u4e2a\u5c0f\u65f6\uff0cA.M.\/P.M. (0~11)<\/td>\n\n\t\t\t\t10<\/td>\n<\/tr>\n
    \n\t\t\t\tz<\/td>\n\n\t\t\t\t\u65f6\u533a<\/td>\n\n\t\t\t\tEastern Standard Time<\/td>\n<\/tr>\n
    \n\t\t\t\t'<\/td>\n\n\t\t\t\t\u6587\u672c\u5206\u9694<\/td>\n\n\t\t\t\tDelimiter<\/td>\n<\/tr>\n
    \n\t\t\t\t\"<\/td>\n\n\t\t\t\t\u5355\u5f15\u53f7<\/td>\n\n\t\t\t\t`<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

    \u66f4\u591a\u5173\u4e8eDate\u7c7b\u7684\u8be6\u7ec6\u4fe1\u606f\u8bf7\u67e5\u9605Java API\u6587\u6863\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    Date\u7c7b\u6709\u4e24\u4e2a\u6784\u9020\u51fd\u6570\u3002\u7b2c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u4f7f\u7528\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u3002 Date( ) \u7b2c\u4e8c\u4e2a\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e00 […]<\/p>\n","protected":false},"author":1481,"featured_media":224810,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-224852","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=224852"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224852\/revisions"}],"predecessor-version":[{"id":224855,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224852\/revisions\/224855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/224810"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=224852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=224852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=224852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}