开发者

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 servlets


Try 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()) %>"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜