dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: How do I correctly compare Strings and Times?

Hybrid View

  1. #1
    Join Date
    Jan 2019
    Location
    Java 1.8
    Posts
    12

    How do I correctly compare Strings and Times?

    I'm having difficulty comparing Strings and Times (as in TimeStamp e.g. "hh:mm:ss"). Below are two snippets of the culprit code.

    How the code is setup and expectation:
    When a user queries a database (embedded SQLite), the database field data captured is dumped into an ObservableList<String> array for each field. This data is then populated to corresponding JavaFX TextFields and TextAreas. The intention is to allow the user to look at the data and make changes to any of the fields as necessary. When the user clicks an "Update" button, the Update method will check each TextField and TextArea and compare that data to the original stored in the array it was populated from. If that data is different, then the user field data is passed to a method that updates the corresponding field in the database. Now this approach seems to work with comparisons of TextFields when it is 'text only', but fails when trying to compare TextAreas or Time strings.

    This works when it is a single line text string and not a TextArea or TimeStamp string:

    Code:
    if(!this.tbxSearchQuestion.getText().equals(this.CurrentSelectionData.get(0))){
                search.UpdateField(CurrentNoteID, "Question", "QuestionID", "Question", tbxSearchQuestion.getText());
            }
    Problem:
    1) Strings (TextArea): I've tried a number of different ways of comparison and used suggestions from other sites or forums. A number of them vary to comparing length(), pass into Builders, etc nothing seems to be working. This issue is comparing TextArea fields. I'm resetting the StringBuilder (sb), dumping the user field into the 'sb' and then comparing to the original array source. Comparing as I do above doesn't work.

    Code:
    str.setLength(0);
            str.append(this.tbxSearchComment.getText());
            if(str.toString().equals(this.CurrentSelectionData.get(8))){
                search.UpdateField(CurrentNoteID, "Comment", "CommentID","Comment", tbxSearchComment.getText());
            }
    2) TimeStamp (TextField): At first, I was simply comparing as the first example above. It would not work though they were different. The same comparison.. user field vs original source. Here, only Videos and Audio require TimeStamps. And it is required input. However, a video sample may be at the beginning and not mid-stream, so "00:00:00" would be the start, but would also fail. So I tried using the DateTime, Date and SimpleDateFormat as recommended on a couple sites, but that fails too:


    Code:
    try {
                if (this.CurrentSourceType.equals("Video") || this.CurrentSourceType.equals("Audio")) {
                    DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
                    //this.tbxTimeStamp.setText(CheckTimeStamp(tbxTimeStamp.getText()));
                    Date sTime = sdf.parse(this.tbxSearchTimeStamp.getText());
                    Date dTime = sdf.parse(this.CurrentSelectionData.get(10));
    
                    if (sTime != dTime) {
                        search.UpdateField(CurrentNoteID, "Comment", "CommentID", "TimeStamp", tbxSearchTimeStamp.getText());
                    }
                }
            }catch(ParseException pe){
                    pe.printStackTrace();
            }
    Attached Images Attached Images  
    Last edited by SVOhio; January 25th, 2019 at 08:44 AM. Reason: Clarification

  2. #2
    Join Date
    Jan 2019
    Location
    Java 1.8
    Posts
    12

    Re: How do I correctly compare Strings and Times?

    Ok, the TextArea comparison works now because I'm an idiot and was missing the "!" to indicate 'not equal'... so no issue there. However, a TimeStamp text string comparison still does not. Sorry for my stupidity there.

    Code:
    try {
                if (this.CurrentSourceType.equals("Video") || this.CurrentSourceType.equals("Audio")) {
                    DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
                    //this.tbxTimeStamp.setText(CheckTimeStamp(tbxTimeStamp.getText()));
                    Date sTime = sdf.parse(this.tbxSearchTimeStamp.getText());
                    Date dTime = sdf.parse(this.CurrentSelectionData.get(10));
    
                    if (sTime != dTime) {
                        search.UpdateField(CurrentNoteID, "Comment", "CommentID", "TimeStamp", tbxSearchTimeStamp.getText());
                    }
                }
            }catch(ParseException pe){
                    pe.printStackTrace();
            }

  3. #3
    Join Date
    Jan 2019
    Location
    Java 1.8
    Posts
    12

    Re: How do I correctly compare Strings and Times?

    I may have solved my problem, though I'm not sure if it is the correct way. In essence, I pass my data objects as generic "Objects" into a return method called "ValuesAreDifferent(Object, Object)" and let it handle the conversion and comparing - defaulting conversions to a String. If the values are different, then it will pass a 'true' to indicate updating the database. This solved my TextArea and Time String issues.

    The method call:

    Code:
    if(ValuesAreDifferent(tbxSearchQuestion.getText(), CurrentSelectionData.get(0))){
                    search.UpdateField(CurrentNoteID, "Question", "QuestionID", "Question", tbxSearchQuestion.getText());
                }
    The method evaluation:

    Code:
    private boolean ValuesAreDifferent(Object obj1, Object obj2){
            if(obj1 == null || obj1.toString().isEmpty() || obj1.toString().trim().isEmpty()){obj1 = "";}
            if(obj2 == null || obj2.toString().isEmpty() || obj2.toString().trim().isEmpty()){obj2 = "";}
            if(obj1.equals(obj2)){return false;}else{return true;}
        }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)