Reputation: 149
I have the following code:
String dateCompleted = "4/24/2009 2:38:44 PM";
DateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
java.util.Date tempDate = format.parse(dateCompleted);
java.sql.Date completionDate = new java.sql.Date(tempDate.getTime());
I want to compare completionDate
to a DATE value in an Oracle database table that does not include time. For example, I want to compare it to the value '4/24/2009' and have it return true, because these dates are the same.
How do I truncate the time value from my variable in order to compare it to the database value? Thanks in advance!
Upvotes: 3
Views: 8355
Reputation: 110
Try this:
String dateCompleted = "4/24/2009 2:38:44 PM";
DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date tempDate = format.parse(dateCompleted);
java.sql.Date completionDate = new java.sql.Date(tempDate.getTime());
same as mentioned by @sgpalit.
Upvotes: 0
Reputation: 69339
Apache Commons Lang has a date truncation method you can use for this:
java.util.Date truncated = DateUtils.truncate(tempDate, Calendar.DATE);
see the Javadocs for DateUtils.truncate
for further details.
Upvotes: 5
Reputation: 302
define the SimpleDateFormat objects as follows,
DateFormat format=new SimpleDateFormat("MM/dd/YYYY");
Upvotes: 1
Reputation: 3767
private final Pattern timeSection = Pattern.compile("\\s+.+?:.*");
String stripTime(String dateStringWithTime){
Matcher m = timeSection.matcher(dateStringWithTime);
if(m.find()){
return m.replaceAll("");
}
return dateStringWithTime;
}
Upvotes: 0