string to java.util.Date and then to java.sql.Date

Posted on 2005-04-11
Medium Priority
Last Modified: 2012-06-22

i have a simple question:

1. receive a string in the format, yyyymmdd
2. add ONE day to it, i.e. yyyymmdd+1 =>20050411+1 => 20050412
3. change it to java.sql.Date and then pass to PreparedStatement's setDate

PreparedStatement stmt = connection.prepareStatement(sql);           
stmt.setDate(1, newSQLDate);

how to do it?
Question by:mscprojhk

Accepted Solution

ricjava earned 100 total points
ID: 13759822


Calendar cal = Calendar.getInstance();

int yr = Integer.parseInt(dateStr.substring(0, 4));
int mth = Integer.parseInt(dateStr.substring(4, 6));
int dy = Integer.parseInt(dateStr.substring(6, 8));

cal.set(yr, mth, dy);
cal.add(Calendar.DATA, 1);

String newDateStr = cal.get(Calendar.YEAR) + cal.get(Calendar.MONTH) + cal.get(Calendar.DATE);

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//put to your newSQLDate

newSQLDate = sdf.parse(newDateStr);
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 13760316
LVL 86

Expert Comment

ID: 13760574
Put the following in a try/catch block:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
cal.add(Calendar.DATE, 1);
stmt.setDate(1, new java.sql.Date(cal.getTimeInMillis());

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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