[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Jsp convert time to milliseconds

Posted on 2005-05-04
17
Medium Priority
?
971 Views
Last Modified: 2012-08-13
Hello experts,

If thereis a time format. e.g. 2005-05-04 5:10:10, how to convert this into milliseconds?

The current time can be got from  java.util.Date currentDate = new java.util.Date();
Do I have to convert it into an instance of Calendar first.

Thanks for your attention.
0
Comment
Question by:lilyyan
  • 9
  • 8
17 Comments
 
LVL 8

Expert Comment

by:koppcha
ID: 13931607
String date="2005-05-04 5:10:10";
SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
 Date myDate = sf.parse(date);
 System.out.println( myDate.getTime() );
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13931626
oops the pattern is yyyy-MM-dd HH:mm:ss
0
 

Author Comment

by:lilyyan
ID: 13936021
Apprexiate your reply.I will try your suggestion later.

Well, from Java API, the method has two parameters: parse(String source, ParsePosition pos).  
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:lilyyan
ID: 13936357
Please ingore my last post. I just found another format of parse method. parse(String source)
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13936370
the above parse statement is good enough
0
 

Author Comment

by:lilyyan
ID: 13936591
well, from API, the method parse() under Date has bee deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).

0
 
LVL 8

Expert Comment

by:koppcha
ID: 13936626
It is under SimplaDateFormat not Date
Just try the above in your code it will work no deprecated functions in it.
0
 

Author Comment

by:lilyyan
ID: 13936788
Hi, I wish I understand your idea now.

the parse(String s) method in above code is inherited from class DataFormat, a super class of SimpleDateFormat.
But this method will throw out exception, so I'm thinking it's better put the above code in a try block.
0
 
LVL 8

Accepted Solution

by:
koppcha earned 320 total points
ID: 13936825
you have to keep it in try catch block.It will throw ParseException.

try
{
String date="2005-05-04 5:10:10";
SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
 Date myDate = sf.parse(date);
 System.out.println( myDate.getTime() );
}
catch(ParseException e){
//Do the action
}
0
 

Author Comment

by:lilyyan
ID: 13936890
yeah, that's exactly what i'm doing. : )
0
 

Author Comment

by:lilyyan
ID: 13938128
hi, i have a question want to ask for.

timeDifference = (currDateMills - mailingDateMills) - 120000;  the data type in here is long (milliseconds)

if ( timeDifference >0 )  // is this valid? cause i got error in here, 0 is int , timeDifference is long
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13938281
Long can be compared to int. double check what you are doing
0
 

Author Comment

by:lilyyan
ID: 13938917
Hi, I find the reason now.

if ( timeDifference >0 )

do some process

else
{
do a query in db
}

---------------
when imeDifference > 0, I will not do query.but since I already delclare : PreparedStatement selectStmt  = null ; at the begining, and do selectStmt.close() at the end.

The exception is thrown out from selectStmt.close() .

0
 

Author Comment

by:lilyyan
ID: 13938925
How to solve this then ?
0
 
LVL 8

Assisted Solution

by:koppcha
koppcha earned 320 total points
ID: 13939702
If it is null pointer exception then before closing check for null.If it is not null then only close it...Depending on the rest of the code you can change the logic but with the information you provided this should work.
0
 

Author Comment

by:lilyyan
ID: 13940640
Hi, you are so great.

Now , everything works fine :)

Thanks a lot!
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13940660
Take it Easy
Good Luck :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this article I will be showing you how to subnet the easiest way possible for IPv4 (Internet Protocol version 4). This article does not cover IPv6. Keep in mind that subnetting requires lots of practice and time.
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 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