-1

What I want to do is:

  • if the time is 10:00:00 proc.process will be executed
  • if the time is 11:00:00 proc.process1 will be executed

String TimeVal = "11:00:00";
String TimeVal1 = "12:00:00";

DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
Date date1 = sdf.parse(TimeVal);
Date date2 = sdf2.parse(TimeVal1);

if (date1 == date) {
    proc.process(variable);    
}
if (date1 == date) {
    proc.process1(variable);     

    proc.process2(variable);
    proc.process3(variable);
    proc.process4(variable);
    proc.process5(variable);
    proc.process6(variable);
    proc.process7(variable);
    proc.process8(variable);
    proc.process9(variable);
    proc.process10(variable);
    proc.process11(variable);
f-CJ
  • 4,235
  • 2
  • 30
  • 28
  • Why you don't use java 8 way ? –  Sep 21 '16 at 09:26
  • The very first thing to understand: == tells you if two **references** are pointing at the **same** object. When you parse two strings, you get **two** date objects; so your check will **always** be false. You have to use **equals()** instead. And seriously: if you are failing on such super basics, then you should step back and forget what you try to do right now. Because then you have almost no clue of nothing; and you better spent some weeks learning those **basics**. Otherwise, you will just run from one frustrating moment into the next one. Because **nothing** you try will work. – GhostCat Sep 21 '16 at 09:29
  • Do you any recommendation what basic do i need to study GhostCat – Manuel Perez Sep 21 '16 at 10:56
  • OCA Java Book study –  Sep 21 '16 at 12:31

3 Answers3

0

Why don't you simply use the java 8 date api ?

Like :

 LocalTime firstTime = LocalTime.of(11, 0, 0); 
 LocalTime secondTime = LocalTime.of(12, 0, 0);
 LocalTime now = LocalTime.now();

 if (now.isAfter(startTime) &&
     now.isBefore(endTime)){
     //process
 }
  • corrected, i couldnt check as i dont have java 8 on my current computer –  Sep 27 '16 at 17:00
  • For Half-Open approach where beginning is inclusive while ending is exclusive, that comparison would be: `( ! now.isBefore( startTime ) ) && now.isBefore( endTime )` – Basil Bourque Sep 27 '16 at 17:32
0

If you want to stick to your Date() objects, you could compare the millis:

if(date1.getTime() == date.getTime()) { 
// ...
}
Valentin Grégoire
  • 1,110
  • 2
  • 12
  • 29
0

tl;dr

LocalTime now = LocalTime.now( ZoneId.of( "America/Montreal" ) );
Boolean isNowWithinRange =
    ( 
        ( ! now.isBefore( LocalTime.parse( "11:00:00" ) ) ) 
        && now.isBefore( LocalTime.parse( "12:00:00" ) 
    ) ;

Details

The accepted answer is a good attempt but has multiple flaws.

Bad practice to call now twice, as you end up making invalid comparisons with two different moments. Call once, and assign to a variable.

Do not ignore the crucial issue of time zone. Better to explicitly pass the optional time zone argument to now( ZoneId ) than rely implicitly on the JVM’s current default which can change at any moment.

You can ask for the device’s current default time zone. But if vital, you should ask/confirm with the user.

ZoneId z = ZoneId.of( "America/Montreal" ); 
LocalTime now = LocalTime.now( z );

You can parse the input strings directly as LocalTime objects.

LocalTime start = LocalTime.parse( "11:00:00" );
LocalTime stop = LocalTime.parse( "12:00:00" );

Common practice in date-time work is to use the Half-Open approach for spans of time. The beginning is inclusive while the ending is exclusive. Using this approach consistently makes your code more understandable and less error-prone.

Tip: Saying “not before start” is a briefer way of saying “is equal to or later than start”.

Boolean isNowWithinRange = ( ( ! now.isBefore( start ) ) && now.isBefore( stop ) ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154