Servlet获取日期和时间 [英] Servlet get date and time

查看:576
本文介绍了Servlet获取日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来获取日期和时间

I'm using the following code to get the date and time

public class offer extends HttpServlet {


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet offer</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1> sv sugar mills</h1>");
        doPost(request,response);
        out.println("</body>");
        out.println("</html>");

    } finally {
        out.close();
    }
} 

 @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
     response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();
     java.sql.Date sqlDate=null,ofrmonth=null,ctime=null;

    dat="14/02/2013";
    try
    {
     java.util.Date date=new SimpleDateFormat("dd/MM/yyyy").parse(dat);
     //ctime=new java.sql.Date(date.getTime());
     sqlDate = new java.sql.Date(date.getTime());
     out.println(sqlDate);
     out.Println(ctime);
    }
    catch(Exception e)
    {
     out.println(e);
    }
 }
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

这里,util.Date变量日期显示为 Thu Feb 14 00:00:00 IST 2013。所以我把sql.Date容易地作为2013-02-14。另外,我想从中得到时间。在ctime变量中,我想得到时间并显示它。格式应为24小时格式。但我不知道如何得到它。

Here, the util.Date variable date is displayed as "Thu Feb 14 00:00:00 IST 2013". So I get the sql.Date easily as "2013-02-14". Also, I want to get the time from it. In the ctime variable I want to get the time and display it. The format should be 24hours format. But I don't know how to get it.

我使用过一些函数,如getHours()和getDate(),但是我找不到解决方案。有人可以帮我取得时间吗感谢提前

I've used some functions like getHours() and getDate(), but I couldn't found the solution. Can someone help me to get the time. Thanks in advance

我需要时间为00:00:00(hh:mm:ss)

I need the time as 00:00:00 (hh:mm:ss)

推荐答案

尝试这样:

Calendar c = Calendar.getInstance();
c.setTime(sqlDate);
System.out.println(c.get(Calendar.HOUR));
System.out.println(c.get(Calendar.MINUTE));
System.out.println(c.get(Calendar.SECOND));

除非为日历设置时区对象上述返回时间在你的机器的时区。

Unless you set a time zone for the Calendar object the above returns time in your machine's time zone.

如果你想要一个单一的 String HH:mm:ss 作为输出,您可以使用 SimpleDateFormat

And if you want a single String in the form of HH:mm:ss as output, you can use a SimpleDateFormat as pointed out by @zvzdhk.

片段:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(sdf.format(sqlDate));

请注意, TimeZone.getTimeZone 允许参数GMT + 5:30PST等等阅读文档了解更多信息。

Note that TimeZone.getTimeZone allows argument like "GMT+5:30", "PST" etc. Read the documentation for more.

这篇关于Servlet获取日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆