1

I need help with this string format: "08/13/2015 10:03:50"

I would like to compare the time within a range of 2 min. I get regex to a point but would not know how to go about this when the time is in string format and the cross over to another hour, (Ex. 10:59:30 would lap over to 11:01:30 not 10:61:30).

Code would be something like

string1 = "08/13/2015 10:03:50";
string2 = "08/13/2015 10:05:50, 08/13/2015 10:55:50, 08/13/2015 10:15:50, 
            08/13/2015 10:14:50, 08/13/2015 10:25:50, 08/13/2015 10:55:50";

if(string2.contains("string1(with plus or minus 2 of string1 min)"){
   //Pass
}

Code I am using to grab the date

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
String timeOfUpdate = dateFormat.format(cal.getTime());
System.out.println(timeOfUpdate);
9
  • 7
    This is not a job for a regex. Commented Aug 13, 2015 at 14:26
  • 4
    Just parse it as a Date, not as a String Commented Aug 13, 2015 at 14:27
  • What would it be then? I thought of hamming distance but that too is not what I would need. Commented Aug 13, 2015 at 14:27
  • I updated the question with the code I am using to grab the date. Commented Aug 13, 2015 at 14:29
  • 1
    ok, so split string2 by ',' into an array, parse each element as a Date, and compare each Date element to the Date that you wish (perhaps there is a more efficient approach) Commented Aug 13, 2015 at 14:38

3 Answers 3

4

Parse the Strings as Date like suggested in the comments, then retrieve the timestamps of both Date objects through the getTime() method. Subtract these two numbers from one another. This get's you the chronological distance of these times in milliseconds, which you can compare to your criteria converted to milliseconds.

Update regarding the date list addendum: To evaluate a String containing a list of dates, you have to split the string along the list separator into an array of strings by using String#split().

Then, you can parse each sub string into a Date object and retrieve the timestamp of each as per the #getTime() method mentioned in the first paragraph. Collect these timestamps into a list.

This list can be searched for entries within the expected range, for example using the Stream API's filter function (Java 8 and newer). Here's a short tutorial about that function.

Sign up to request clarification or add additional context in comments.

2 Comments

Would this work on a string with a list of multiple dates? I updated the code to show what I mean by that. I have a large number of dates I would like to check through all and determine if any have a date within 2 min of the searched date.
@MCD23 I updated my answer to include an algorithm regarding your string of list of dates edit.
2

Try executing the below steps:

  1. Convert String1 into Date Object and apply getTime() method, store the result in some variable
  2. Convert String2 into a array of Strings by using split on comma
  3. Run a loop on results retrieved in step#2, inside loop

    • Convert each value fetched into Date Object
    • Call getTime on date object converted
    • Subtract this number with the one fetched in Step#1
    • Now you will get the difference in milliseconds(1 min = 60000 miliseconds)
    • if the difference is either greater than -120000 or less than 120000 miliseconds than break the loop and set the flag to true.

    Let me know if you still face any issue.

1 Comment

Ugh so much work. Was really hoping it was as easy as searching with a regexed expression. Thanks for the help!
1

You can use date.getTime() for getting date in milliseconds since 1970 and split your date string arrays :

int offsetMinutes = 2;

String string1 = "08/13/2015 10:03:50, 08/14/2015 10:05:50, 08/14/2015 10:05:50";
String string2 = "08/13/2015 10:05:49, 08/14/2015 10:05:50, 08/15/2015 10:05:50";

String[] string1Array = string1.split(",");
String[] string2Array = string2.split(",");

DateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss", Locale.ENGLISH);

for (int j = 0 ; j< string1Array.length;j++)
{
    Date date1 = format.parse(string1Array[j].trim());

    for (int i = 0; i < string2Array.length; i++) {
        Date date2 = format.parse(string2Array[i].trim());
        if (date1.getTime() >= (date2.getTime() + offsetMinutes * 60 * 1000) || date1.getTime() <= (date2.getTime() - offsetMinutes * 60 * 1000)) {
            //Here we are
        }
    }
}

3 Comments

This looks promising. I'll give it a try and report back if it worked or not. Thanks a ton!
What would i need to change if string 2 was a list of multiple dates? see updated question above.
I would accept this as the answer, but it's not quite what I need which is not your fault. I guess I didn't give enough info out. I would not be able to use the delimiter "," because it is not even in the strings, I just used that as an example. In reality, each date also has an unpredictable length of text pended on. So say its a sale of an iPhone and iPad. It would be something like string1 = " iPhone sold using cash 8/13/2015 10:05:30 iPad sold using card 8/13/2015 10:06:30".

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.