问题
I am getting surprise result in java servlet. I am passing input parameter(a date) from a jsp to servlet like this:
<input name='date_allow_empty' type='text' value='' class='date picker' />
Date picker is here: http://jsfiddle.net/cBwEK/
let's say i choosed : 05-04-2012, when i passed this date to servlet then i am getting:
1333620371
But i should get 05-04-2012 in servlet
Servlet:
String t= request.getParameter("date_allow_empty");
out.println(t); //displaying 1333620371 in stead of 05-04-2012
Why this type of result is being displayed in servlet?
回答1:
That value, 1333620371, is the number of seconds since The Epoch (Jan 1st, 1970). To make a Java Date out of it, use the Date(long) constructor, which expects milliseconds since The Epoch (so you multiply by 1,000):
Date dt = new Date(value * 1000);
If you're getting the value as a String, you'll need to parseLong it first, e.g.:
Date dt = new Date(Long.parseLong(value, 10) * 1000);
回答2:
You're getting the number of seconds since January 1, 1970, 00:00:00 GMT, see: http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime()
To convert it to a java.util.Date object, simply use :
String t= request.getParameter("date_allow_empty");
Date theDate = new Date(Long.valueOf(t) * 1000);
(http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#Date(long))
EDIT : It is in seconds, not milliseconds :(
来源:https://stackoverflow.com/questions/10381965/surprising-result-coming-in-java-servlet