Results 1 to 4 of 4
  1. #1
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default how to compare time

    Hello All,

    How to compare time? .. (currentTime and timetoCompare)

    Java Code:
    import java.util.TimerTask;
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    
    Class1 cls1 = new Class1();
        @Override
        public void run() {
            String timetoCompare = "21:13";
            String currentTime = getTime();
    
           // how to do time compare here??
           if (currentTime > timetoCompare) {
    
            }
    
        }
    
    private String getTime() {
            DateFormat dateFormat = new SimpleDateFormat("HH:mm");
            Date date = new Date();
            return dateFormat.format(date);
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't deal with Strings here but rather deal directly with Date objects as this class conveniently implements Comparable<Date> and so has a compareTo method that will work well for you.

  3. #3
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    noted and thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're quite welcome. I wish you much luck with this project!

Similar Threads

  1. How compare two String?
    By artemff in forum New To Java
    Replies: 8
    Last Post: 08-29-2011, 08:00 AM
  2. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  3. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  4. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  5. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 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
  •