Solved

Time comparison

Posted on 2011-09-11
Medium Priority
309 Views
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
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

LVL 47

Expert Comment

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");
``````

Output:
``````after
``````
0

Author Comment

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

LVL 47

Accepted Solution

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");
``````

Output:
``````false
``````
0

LVL 47

Expert Comment

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

ID: 36519955
0

LVL 47

Expert Comment

ID: 36519959

``````  System.out.println(ifItIsBefore("15:00"));
``````
`````` 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;

}
``````

``````true
``````
0

Featured Post

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used byâ€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importingâ€¦
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

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

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