Solved

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

Posted on 2004-09-19
7
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
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…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

636 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