Solved

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

Posted on 2004-09-19
7
185 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

23 Experts available now in Live!

Get 1:1 Help Now