java Servlet 获取日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14868537/
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
Servlet get date and time
提问by Lingasamy Sakthivel
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>
}
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.
此处,util.Date 变量日期显示为“Thu Feb 14 00:00:00 IST 2013”。所以我很容易得到 sql.Date 为“2013-02-14”。另外,我想从中获得时间。在 ctime 变量中,我想获取时间并显示它。格式应为 24 小时格式。但我不知道如何得到它。
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
我使用了一些函数,如 getHours() 和 getDate(),但我找不到解决方案。有人可以帮我争取时间吗?提前致谢
I need the time as 00:00:00 (hh:mm:ss)
我需要时间为 00:00:00 (hh:mm:ss)
回答by hthserhs
Try this:
试试这个:
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.
除非您为Calendar
对象设置了时区,否则上述返回时间为您机器的时区。
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.
如果你想要一个作为输出String
形式的单曲HH:mm:ss
,你可以使用SimpleDateFormat
@zvzdhk 指出的 a 。
Snippet:
片段:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(sdf.format(sqlDate));
Note that TimeZone.getTimeZone
allows argument like "GMT+5:30"
, "PST"
etc. Read the documentationfor more.
需要注意的是TimeZone.getTimeZone
允许的说法一样"GMT+5:30"
,"PST"
等阅读文档为多。