1

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

1/1/2013 12:00:00 AM_5/31/2013 12:00:00 AM
1/1/2013 12:00:00 AM_
_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

String dbFormat = AppConstant.DB_DATE_FORMAT;
String format = AppConstant.DATE_FORMAT;
SimpleDateFormat df = new SimpleDateFormat(format);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String dates[] = searchValue.split("_", -1);
String start = "".equals(dates[0]) ? df.format(new Date(0L)) : dates[0];
String end = "".equals(dates[1]) ? df.format(new Date()) : dates[1];
filters.add(String.format("%s between to_date('%s', '%s') and to_date('%s', '%s')", columnKey, start, dbFormat, end, dbFormat));

The part that I need help on is,the date for the variable start and end should be in UTC format.As you guys can see, I've managed to convert new Date(0L) and new Date() to UTC format. But I cant seem to format dates[0] and dates[1] to UTC.

Any ideas how to convert dates[0] and dates[1] into UTC time format? Can it be done in the same line.

I tried df.format(dates[0]) but its not working.

I'm sorry if I may sound like a nood, but I'm new to java and I'm learning each day.

Thanks alot in advance.

1 Answer 1

1

Parse those string input dates as a different format using the parse function on a new SimpleDateFormat (using format MM/dd/yyyy hh:mm:ss a) and that will give you a Date to then pass into format for the SimpleDateFormat you already have.

String dbFormat = AppConstant.DB_DATE_FORMAT;
String format = AppConstant.DATE_FORMAT;
SimpleDateFormat df = new SimpleDateFormat(format);
SimpleDateFormat df2 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String dates[] = searchValue.split("_", -1);
String start = "".equals(dates[0]) ? df.format(new Date(0L)) : df.format(df2.parse(dates[0]));
String end = "".equals(dates[1]) ? df.format(new Date()) : df.format(df2.parse(dates[1]));
filters.add(String.format("%s between to_date('%s', '%s') and to_date('%s', '%s')", columnKey, start, dbFormat, end, dbFormat))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.