We help IT Professionals succeed at work.

JSP - Create a Date String with Random numbers

jedistar
jedistar asked
on
Hi guys,

I have a .jsp file in tomcat which has a form. How do i create a string which contains date and random numbers..

i.e

200801280100922

2008 = year
01 = month
28 = day
0100 = time in 24 hr clock
922 = random 3 numbers

How do i put it in a label field and also submit it as a hidden input type..

Thanks!
Comment
Watch Question

Commented:
here it is;
		Random rand = new Random();	    
	    // Random integers
	    int i = rand.nextInt(899) + 100;
	    
		Calendar cal = Calendar.getInstance(TimeZone.getDefault()); 
		String DATE_FORMAT = "yyyyMMddHHmm"; 
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
		sdf.setTimeZone(TimeZone.getDefault()); 
		String currentTime = sdf.format(cal.getTime());
		
		String myStr = currentTime + Integer.toString(i);
		
		System.out.println(myStr);

Open in new window

Commented:
>>How do i put it in a label field and also submit it as a hidden input type..
in jsp;

<p><%=myStr%></p>
<input type=hidden name="my_hidden" value="<%=myStr%>">

Author

Commented:
Sorry I forgot to include time as well after the date, how could i do that?

Commented:
so replace
String DATE_FORMAT = "yyyyMMddHHmm";
with
String DATE_FORMAT = "yyyyMMddHHmmss";


"HHmmss"  for time

for more;
http://www.exampledepot.com/egs/java.text/FormatDate.html

Commented:
with milisecond try this;
String DATE_FORMAT = "yyyyMMddHHmmssS";

Author

Commented:
Thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.