Solved

Convert a string("dd-mm-yy") to type date and the reverse of .add method

Posted on 2004-09-19
7
186 Views
Last Modified: 2010-03-31

Is it possible to convert a string in this format : "dd-mm-yy" to type date so .add operation can be perfomed on it. As in:

String date = rs112.getString("expdate");
Calendar finishDate = Calendar.getInstance(date);
finishDate.add(Calendar.DAY_OF_YEAR, roll_amt);

Is there a reverse method of .add so that a date can be rolled back a certain number of days?
0
Comment
Question by:prograMNewbie
  • 3
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12097579
Use SimpleDateFormat


SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
finishDate.setTime(sdf.parsedate));
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 12097583
Sorry - that should be:

finishDate.setTime(sdf.parse(date));

>>Is there a reverse method of .add so that a date can be rolled back a certain number of days?

Make the parameter negative
0
 
LVL 92

Expert Comment

by:objects
ID: 12098044
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
String date = rs112.getString("expdate");
Calendar finishDate = Calendar.getInstance();
finishDate.setTime(sdf.parse(date));
finishDate.add(Calendar.DAY_OF_YEAR, roll_amt);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 12098050
> String date = rs112.getString("expdate");

If the database column is actually a Date, then you could use getDate() to get the date directly:

java.sql.Date date = rs112.getDate("expdate");
Calendar finishDate = Calendar.getInstance();
finishDate.setTime(date);
finishDate.add(Calendar.DAY_OF_YEAR, roll_amt);
0
 

Author Comment

by:prograMNewbie
ID: 12173571
Sorry about the delay, CEHZ: Your code worked perfect, thanks

Thanks objects for ur comments
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12173630
,8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12174779
Glad I could help :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Non-recursive backtracking, using a stack 1 98
Image decoding from Camera 3 72
Java Loop 6 48
oracle 11g 23 48
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now