how to generate unique integer id in jsp using current date and time
how to generate unique integer id开发者_高级运维 in jsp using current date and time.
i want something like this 20110414440 where 2011 is year 04 is month 14 is date 4 is current hr 40 is current min. even though itseems simple i am unable to get it can anyone help me with the code..? i want to display it in a text box. i am using jsp and servletsTry this:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmm");
Date date = new Date();
String str = format.format(date);
or simply use date.getTime()
or Calendar.getInstance().getTimeInMillis()
it will also be unique in most cases.
Update:-
<%
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmm");
java.util.Date date = new java.util.Date();
String str = format.format(date);
%>
<input type="text" value="<%= str %>"/>
or
<input type="text" value="<%= (new Date()).getTime() %>"/>
or
<input type="text" value="<%= Calendar.getInstance().getTimeInMillis() %>"/>
or
<input type="text" value="<%= (new SimpleDateFormat("yyyyMMddhhmm")).format(new java.util.Date()) %>"/>
精彩评论