how to break a java String into three differnt Strings

Posted on 2006-04-10
Last Modified: 2010-04-01
I am getting date as a parameter in this format 20060924. What I need is to seperate the day mmonth and year from this String.
It will always be in this format.

Any help will be greatly appeciated

Question by:royalcyber
    LVL 14

    Expert Comment

    You can etiher use substring or DateFormat.

        String date = "20060924";
        String year = date.substring(0, 4);
        String month = date.substring(4, 6);
        String day = date.substring(6, 8);

        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);

    Author Comment

    Thankyou boonleg for your prompt response. I was actually trying to compare the date which i get as a request parameter to the date/time field in MSSQL,
    would you know how would I be able to do that. I am getting the date in this format date = "20060924"; Thanks
    LVL 14

    Accepted Solution

    Use use DateFormat to parse string to date object and use date.compareTo(anotherDate) to compare the date retrieve rom database.

    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    Date date = df.parse("20060924");

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
    Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 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

    12 Experts available now in Live!

    Get 1:1 Help Now