Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-19
7
Medium Priority
?
206 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 400 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
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.

 
LVL 92

Assisted Solution

by:objects
objects earned 200 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

927 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