Results 1 to 4 of 4
  1. #1
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default new Date().toString comparison to another String

    I am trying to compare my current Date and Time, converted to a String, to a provided Date and Time from a JTextField. I am running a while loop to loop as long as the new Date().toString doesn't equals an empty set "". Which this should continue to run because there should always be some string returned. I'm running an if condition to actually run my code based on whether the formatted new Date().toString equals the provided time which is a String also. When I run my application and provide the needed information, it just sits there and runs. Which leads me to believe that it is just continuing to loop and the if condition is never returning true. Obviously there is a problem with my condition statement. Can anyone else see?

    Java Code:
    public void timedStart()
        {
            SimpleDateFormat ft = new SimpleDateFormat("M/dd/yy   hh:mm:ss a"); //3 spaces between
            String time = timebx.getText();
                    
            while(new Date().toString() != "")
                if(ft.format(new Date()).toString().equals(time))
                {
                    tStartTime = time;
                    testName = testNambx.getText();
                    
                    //combine the sourcePath with each video file name to create 
                    //source file
                    for(int i = 0; i < VidFilesNam.size(); i++)
                    {
                        VidFiles.add(String.format(sourcePath + slash + 
                                VidFilesNam.get(i), "\\s+"));            
                    }
            
                    //checking the paths in VidFiles for spaces then adding to vidFiles
                    for (int i = 0; i < VidFiles.size(); i++)
                    {
                        if(VidFiles.get(i).contains(" "))
                        {
                            vidFiles.add(VidFiles.get(i).replace(" ", ""));
                        }  
                        else 
                        {
                            // If it not contains " ", add it as it is to vidFiles
                            vidFiles.add(VidFiles.get(i));
                        } 
                    }
                    
                    //start a copy command for each video file in VidFiles ArrayList
                    for(int i = 0; i < vidFiles.size(); i++)
                    {            
                        Path sPath = Paths.get(vidFiles.get(i));
                        Path dPath = Paths.get(destpath + testName + slash + 
                                VidFilesNam.get(i));
                        String tfile = dPath.toString();
                        File videoFile = new File(tfile);
                
                        //checks to see if testName folder exist under destination path
                        if(!videoFile.exists())
                        {
                            videoFile.mkdirs();
                        }
                           
                        try
                        {
                            fStartTime = ft.format(new Date());
                            Files.copy(sPath, dPath, REPLACE_EXISTING);
                            fStopTime = ft.format(new Date());
                        }
                        catch(IOException e)
                        {
                        JOptionPane.showMessageDialog(null, "There was a problem.");                
                        }            
                
                        long filesize = videoFile.length();        
                        size = Long.toString(filesize / 1024);
                
                        s1 += newline + VidFilesNam.get(i) + "   |   " + fStartTime + 
                        "   |   " + fStopTime + "   |   " + size + " KB";          
                    }
            
                    tStopTime = ft.format(new Date());
                }
        }

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default Re: new Date().toString comparison to another String

    Your loop condition:

    Java Code:
    new Date().toString() != ""
    will always evaluate to true, since new Date().toString() returns a value that is not an empty String. It returns something like:

    Fri Jun 21 12:00:23 EDT 2013

    So essentially, the loop will continue to run until you explicitly break out of it using the break statement, and nowhere have you done that. Why exactly are you running a loop, what is your goal?
    Last edited by sehudson; 06-21-2013 at 08:25 PM.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    732
    Rep Power
    2

    Default Re: new Date().toString comparison to another String

    Your condition is wrong in two places:
    1) new Date().toString() will never be empty so the loop will run forever. If you want to exit the loop prematurely, use a break statement. Still, the loop construct smells, even with the said break.
    2) Testing contents of a String should not be done with != or ==, but with equals(), equalsIgnoreCase() or isEmpty().

  4. #4
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: new Date().toString comparison to another String

    I want the loop to continue. What I am trying to accomplish is to start my file copy at a certain time. My logic is to continue to check current time to see if its "formatted" string equals a provided string. At the time when the condition is true, I want my copy to run. I'm using equals(String) to compare. Well that is what I hope to achieve. After it runs my code I want it to break the loop. Which I see I need to do that at least.

Similar Threads

  1. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  2. String Comparison
    By evant8950 in forum Java Applets
    Replies: 6
    Last Post: 04-22-2009, 08:11 AM
  3. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:47 AM
  4. Date comparison
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-19-2007, 06:34 PM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 12:16 AM

Posting Permissions

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