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

Posted on 2005-04-11
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



    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

    LVL 86

    Expert Comment

    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());

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    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…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now