在 Java (JSP) 中获取日期的 dd-MM-yyyy 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22983392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:46:14  来源:igfitidea点击:

Get the dd-MM-yyyy format of date in Java (JSP)

javadatedate-formatsimpledateformat

提问by Nihad KH

I'm using an input=date calendar, and i want to output this format of date (dd-MM-yyyy) in my jsp ,when i choose the 2nd day of april 2014 in the calendar given by the input ,I have this output in my jsp:

我正在使用 input=date 日历,我想在我的 jsp 中输出这种格式的日期(dd-MM-yyyy),当我在输入给出的日历中选择 2014 年 4 月的第 2 天时,我有这个我的jsp中的输出:

Wed Apr 02 00:00:00 WET 2014

2014 年 4 月 2 日星期三 00:00:00 WET

Here you are the code:

这是代码:

index.jsp:

索引.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form name="datepickeer" action="showdates.jsp" method="POST">
            <table>
                <tr><td>Date début :</td> <td><input type = "date" name = "datedebut">
                    </td><tr>
                <tr><td><input type = "submit" name = "submit" value = "submit">
                    </td></tr>
            </table>
        </form>
    </body>
</html>

showdates.jsp:

显示日期.jsp

<%@ page import="java.util.Date,java.text.SimpleDateFormat,java.text.ParseException"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <% String dateStr = request.getParameter("datedebut");
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
            Date result = formater.parse(dateStr);
            out.println(result);
        %>
    </body>
</html>

What's the problem with my code?

我的代码有什么问题?

采纳答案by user3091530

String dateStr = request.getParameter("datedebut");
  SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
  Date result = formater.parse(dateStr);
  SimpleDateFormat AppDateFormat = new SimpleDateFormat("dd-MM-yyyy");
  out.println(AppDateFormat.format(result));

回答by Subhrajyoti Majumder

Try this -

尝试这个 -

<% 
     String dateStr = request.getParameter("datedebut");
     SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
     Date result = formater.parse(dateStr);
     SimpleDateFormat newFormater = new SimpleDateFormat("dd-MM-yyyy");
     out.println(newFormater.format(result));
%>

回答by bgth

You have converted the input from index.jsp into Date using formater.parse. Now to show it on the output in showdates.jsp, you have to again convert into String using formater.format(result) to yyyy-MM-dd. Actually what you are doing is correct. Whatever you get from input you should first parse and then print because you will be sure that the input which came fits into what you require. Use this.

您已使用 formater.parse 将 index.jsp 中的输入转换为 Date。现在要在 showdates.jsp 的输出中显示它,您必须再次使用 formater.format(result) 将其转换为 String 到 yyyy-MM-dd。其实你的做法是对的。无论您从输入中获得什么,您都应该首先解析然后打印,因为您将确保输入的内容符合您的要求。用这个。

Date result = formater.parse(dateStr);
SimpleDateFormat formaterForOut = new SimpleDateFormat("dd-MM-yyyy");
String resultString = formaterForOut.format(result); 
out.println(resultString);

回答by antelove

<% 

  String dateStr = request.getParameter("datedebut");

  java.text.SimpleDateFormat oldFormater = new SimpleDateFormat("dd-mm-yyyy");
  java.util.Date resultInDate = oldFormater.parse(dateStr);

  java.text.SimpleDateFormat newFormater = new SimpleDateFormat("dd-mm-yyyy");
  String resultInString = newFormater.format(resultInDate);

  out.println(resultInDate); // Wed Apr 02 00:00:00 WET 2014
  out.println(resultInString); // 02-04-2014

  /* convert String resultInString into java.sql.Date resultInDateSql */
  java.sql.Date resultInDateSql = java.sql.Date.valueOf(resultInString);

  /* set in query sql */
  preparedStatement.setDate(1, resultInDateSql);

%>