开发者

how to generate unique integer id in jsp using current date and time

开发者 https://www.devze.com 2023-02-25 00:19 出处:网络
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.

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

精彩评论

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