0

I'm new at coding and I want my program to do something if the current time is between 2 times.

Example: Current time = 8:25AM If current time is between 8:00AM and 8:50AM, print 'something' into console.

Is there a way for my program to get the current time automatically? Calendar Class? Please Help! Matthew

  • Check here: http://stackoverflow.com/questions/833768/java-code-for-getting-current-time – bgse Oct 07 '15 at 00:43

2 Answers2

0

You can get current time instantiating a new java.util.Object Date current = new Date();

Then you can play with after() and before() methods

Hope this helps, Alberto

Alberto Saito
  • 271
  • 1
  • 6
0
String starttime = "08:00:00";
String endtime = "08:50:00";

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = format.parse(starttime);
Date date2 = format.parse(endtime);

Calendar cal = Calendar.getInstance();

String currenttime = format.format(cal.getTime());
Date datenow = format.parse(currenttime)

if (date1.getTime() < datnow.getTime() && date2.getTIme() > datenow.getTime())
System.out.println("Time is within defined borders.");

I dont have a compiler now . This should work fine .

sean
  • 717
  • 2
  • 9
  • 23