[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Time comparison

Posted on 2011-09-11
6
Medium Priority
?
313 Views
Last Modified: 2012-05-12
Given a string representation of a time slot  say "08:00", I want to compare it with the current time and return true of if it less than the current time.

If possible, please provide some sample code. Thanks!
0
Comment
Question by:dshrenik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36519924
 SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdff1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        java.util.Date ddd = new java.util.Date();
        String dss = sdff.format(ddd);
        String s8 = "08:00";

           java.util.Date ddd1 = sdff1.parse(dss + " " + s8, new ParsePosition(0) );

        if(ddd.after(ddd1)){
            
            System.out.println("after");
        }   else    System.out.println("before");

Open in new window


Output:
after

Open in new window

0
 

Author Comment

by:dshrenik
ID: 36519932
Can we do this comparison between SimpleDateFormat  and a String like "08:00" or "15:00"? Thanks!
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36519936
ths one will return true or false as you want
 SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdff1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        java.util.Date ddd = new java.util.Date();
        String dss = sdff.format(ddd);
        String s8 = "08:00";

           java.util.Date ddd1 = sdff1.parse(dss + " " + s8, new ParsePosition(0) );

        if(ddd.before(ddd1)){
            
            System.out.println("true");
        }   else    System.out.println("false");

Open in new window


Output:
false

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36519941
Don't understand your question - I used s = "08:00"
- you can pass this strng as aparaemeter to the nmethod and it can be "08:00" or "15:00" - whatwever you feed
0
 

Author Comment

by:dshrenik
ID: 36519955
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519959



  System.out.println(ifItIsBefore("15:00"));

Open in new window

 public static boolean ifItIsBefore(String s8){


        SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd");
              SimpleDateFormat sdff1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

              java.util.Date ddd = new java.util.Date();
              String dss = sdff.format(ddd);
            //  String s8 = "08:00";

                 java.util.Date ddd1 = sdff1.parse(dss + " " + s8, new ParsePosition(0) );

              if(ddd.before(ddd1)){

                  return  true;
              }   else    return false;


    }

Open in new window


true

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question