Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 961161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:17:55+00:00 2026-05-16T01:17:55+00:00

I thinking what is the best way in Java to parse the String with

  • 0

I thinking what is the best way in Java to parse the String with this format
dd/MM/yyyy [to dd/MM/yyyy]. The string with the [] are optional and dd stand for the 2 digit presentation of dates, MM is the 2 digit presentation of month and yyyy is the 4 digit presentation of year.


Update

Thanks guys for the fast response, however I forgot to tell you the [] is to symbolize optional, there is no [] in the string a sample String might be

  • 22/01/2010
  • 22/01/2010 to 23/01/2010
  • null

Current I wrote the code this way, work but is ugly =(

String _daterange = (String) request.getParameter("daterange");
    Date startDate = null, endDate = null;
    // Format of incoming dateRange is 
    if (InputValidator.requiredValidator(_daterange)) {
        String[] _dateRanges = _daterange.toUpperCase().split("TO");
        try {
            startDate = (_dateRanges.length > 0) ? sdf.parse(_dateRanges[0]) : null;
            try{
                endDate = (_dateRanges.length > 1) ? sdf.parse(_dateRanges[1]) : null;
            }catch(Exception e){
                endDate = null;
            }
        } catch (Exception e) {
            startDate = null;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:17:56+00:00Added an answer on May 16, 2026 at 1:17 am

    Use java.text.DateFormat and java.text.SimpleDateFormat to do it.

    DateFormat sourceFormat = new SimpleDateFormat("dd/MM/yyyy");
    String dateAsString = "25/12/2010";
    Date date = sourceFormat.parse(dateAsString);
    

    UPDATE:

    If you have two Dates hiding in that String, you’ll have to break them into two parts. I think others have pointed out the “split” idea. I’d just break at whitespace and throw the “TO” away.

    Don’t worry about efficiency. Your app is likely to be riddled with inefficiencies much worse than this. Make it work correctly and refactor it only if profiling tells you that this snippet is the worst offender.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im thinking this is correct, but probably WAY off. I have a string formatted
I'm thinking about the best way to do two things in Java: merge two
Just thinking about the best way to build an Order form that would (from
What is the best way of tracking responses for email campaigns? I was thinking
I am new to rails and am wondering if this is the best way
Trying to figure out the best way to do this (And without crossing any
which is the best way to write a bidimensional hashmap efficiently in Java? Just
I'm trying to figure out the best way to install my Java app on
I'm not sure if this is the best way to ask this question but
What is the best way to represent in java a choice namespace tag? ie.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.