1

I am making a program that executes something every time the time reaches an hour.

Here is the code:

import java.text.*;
import java.util.Date;
class NMAP {
 public static void main(String[] args) {
  DateFormat dateFormat = new SimpleDateFormat("HH");
  Date date = new Date();
  if (dateFormat.format(date).equals(17)) {
   System.out.println(dateFormat.format(date));
  }
 }
}

It should be outputting 17 (since it is 17:00), but it doesn't output anything.

Why not?

Jacob Collins
  • 159
  • 1
  • 2
  • 7

1 Answers1

3

you are comparing dateFormat.equals(17) a string against an integer

that returns false no matter what because they are different types.

you have to do dateFormat.equals("17")


And because some when you will need the power of java8:

LocalTime now = LocalTime.now();
System.out.println(now);
System.out.println(now.getHour() == 17);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97