Solved

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

Posted on 2004-09-19
7
187 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …

839 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