[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

JSP - Create a Date String with Random numbers

Posted on 2008-01-27
6
Medium Priority
?
1,614 Views
Last Modified: 2013-12-02
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!
0
Comment
Question by:jedistar
  • 4
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
ysnky earned 600 total points
ID: 20754760
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

0
 
LVL 9

Expert Comment

by:ysnky
ID: 20754775
>>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%>">
0
 

Author Comment

by:jedistar
ID: 20756406
Sorry I forgot to include time as well after the date, how could i do that?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 9

Expert Comment

by:ysnky
ID: 20757174
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
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20757190
with milisecond try this;
String DATE_FORMAT = "yyyyMMddHHmmssS";
0
 

Author Closing Comment

by:jedistar
ID: 31425452
Thanks!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question