Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's the scenario. User enters 3 types of inputs:

  1. 1/1/2013 12:00:00 AM_5/31/2013 12:00:00 AM
  2. 1/1/2013 12:00:00 AM_
  3. _5/31/2013 12:00:00 AM

The input is for date range query.

1st input: Split by the _ delimiter: Broken into Start date and end date.

2nd input: Split by the _ delimiter: Input is only the start date.

3rd input: Split by the _ delimiter: Input is only the end date.

Input is retrieved by generic return type.

if (StringUtils.endsWithIgnoreCase(searchKey, "date")) {
    String dates[] = searchValue.split("_",-1);

    if (dates [0] == null)
    {
        long epoch = System.currentTimeMillis()/1000;
        String format = "MM/dd/yyyy HH:mm:ss aaa";
        dates[0] = new     
        java.text.SimpleDateFormat(format).format(new java.util.Date (epoch*1000));
    }
    else if (dates [1] == null)
    {
        Date dNow = new Date( );
        dates[1] = DateFormat.getInstance().format(dNow);
    }

    String start = dates[0];
    String end = dates[1];
    String format = AppConstant.DATE_FORMAT;
    filters.add(String.format("%s between to_date('%s', '%s') and to_date('%s', '%s')", columnKey, start, format, end, format));
} 

As you can see the whole idea is:

  1. if there is no start date is entered by the user, the epoch date will be used as the start date.
  2. if there is no last date is entered by the user,the current date n time will be used as the end date.

But unfortunately,this coding does not work. I'm guessing the problem is Split doesn't store a NULL value hence the if statement is not working.

I'm still relatively new to java and any help is very much appreciated.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

dates won't have null entries, it will have empty string entries.

System.out.println("_foo".split("_", -1)[0].equals(""));//true

So, test for empty string using the String.equals method

if ("".equals(dates[0]) { ...
share|improve this answer
    
Thanks a lot Chris!!! Really helped!!! –  captain jack Jun 17 '13 at 1:19
    
Chris just a another thing i would like to ask, how do you format an array element? For example how to format dates[0]? i've created the simpledateformat object and i was trying to do something like df.format(dates[0]); but its not working. Sorry if i might sound like a noob but as i said i'm new to java and im learning my my mistakes.Thanks :) –  captain jack Jun 19 '13 at 16:15

Frankly, I think there's a simpler way to approach this problem:

1. if searchValue ends with '_'
   then end date = now, parse substring from 0 to length - 1 into date
2. if searchValue starts with '_'
   then start date = epoch, parse substring from 1 to length into date
3. else, split on '_',
   then require two pieces returned by split, parse into dates

I'll leave it to you to translate into Java.

share|improve this answer

If you have to receive user input in that way (with the three possible patterns as you describe) then I recommend using a regular expression (regex) to match the parts of the pattern.

For instance:

String user_supplied_date_range_string =
        "1/1/2013 12:00:00 AM_5/31/2013 12:00:00 AM";
Pattern date_range_pattern = Pattern.compile(
        "^([0-9]{1,2}/[0-9]{1,2}/[0-9]{4} "
        + "[0-9]{1,2}:[0-9]{2}:[0-9]{2} (?:A|P)M)?_"
        + "([0-9]{1,2}/[0-9]{1,2}/[0-9]{4} "
        + "[0-9]{1,2}:[0-9]{2}:[0-9]{2} (?:A|P)M)?$");
Matcher matcher = date_range_pattern.matcher(
        user_supplied_date_range_string);

This will create a regex pattern which fits the three patterns you describe, and then run the regex matcher to process the user-supplied date-range string to see whether the user's input fits.

Now you can check the result to see whether the first and/or second parts of the pattern matched:

if (matcher.matches()) {
    if (matcher.group(1) != null) {
        System.out.println("Start date found: " + matcher.group(1));
    }
    if (matcher.group(2) != null) {
        System.out.println("End date found: " + matcher.group(2));
    }
} else {
    System.out.println("User input does not match date range pattern.");
}

If both group one and two are null, then the user input did not fit the pattern at all.

See the java.util.regex package docs for more details.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.