0

this code below gets the current time and timezone of the area

    Date date = new Date();
    DateFormat df = new SimpleDateFormat("HH:mm:ss");

    df.setTimeZone(TimeZone.getDefault());

    System.out.println("Time: " + df.format(date)); 

right now its 1:01 pm (at the time of typing)

what i need help doing is implementing a feature in the code that checks if the current time has passed, for example 1:00PM

but I have no idea where to even start, can you help me out?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
dam1ne
  • 181
  • 1
  • 2
  • 17
  • Have you checked out the `Date` API (and followed the trail of docs to the current recommended methods)? What specifically is the issue? – Dave Newton Dec 06 '19 at 21:14
  • [This answer on another thread may help you, it returns a bool if the date is different between 2](https://stackoverflow.com/a/50598602/12493416) –  Dec 06 '19 at 21:27
  • 1
    I recommend you don’t use `Date` and `SimpleDateFormat`. Those classes are poorly designed and long outdated, the latter in particular notoriously troublesome. Instead use `LocalTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 07 '19 at 08:07

2 Answers2

4

Use the Java 8+ Time API class LocalTime:

LocalTime refTime = LocalTime.of(13, 0); // 1:00 PM
// Check if now > refTime, in default time zone
LocalTime now = LocalTime.now();
if (now.isAfter(refTime)) {
    // passed
}
// Check if now >= refTime, in pacific time zone
LocalTime now = LocalTime.now(ZoneId.of("America/Los_Angeles"))
if (now.compareTo(refTime) >= 0) {
    // passed
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

I see it has already answered with Time, but as a teaching point, if you really wanted to use Date, you could have done something like this:

public static void main(String[] args) {
    Date date = new Date();
    DateFormat df = new SimpleDateFormat("HH:mm:ss");
    df.setTimeZone(TimeZone.getDefault());
    System.out.println("Time: " + df.format(date));

    //If you print the date you'll see how it is formatted
    //System.out.println(date.toString());

    //So you can just split the string and use the segment you want
    String[] fullDate = date.toString().split(" ");

    String compareAgainstTime = "01:00PM";

    System.out.println(isPastTime(fullDate[3],compareAgainstTime));
    }

public static boolean isPastTime(String currentTime, String comparedTime) {
    //We need to make the comparison time into the same format as the current time: 24H instead of 12H:
    //then we'll just convert the time into only minutes to that we can more easily compare;
    int comparedHour = comparedTime[-2].equals("AM") ? String.valueOf(comparedTime[0:2]) : String.valueOf(comparedTime[0:2] + 12 );
    int comparedMin = String.valueOf(comparedTime[3:5]);
    int comparedT = comparedHour*60 + comparedMin;

    //obviously currentTime is alredy the correct format; just need to convert to minutes
    int currentHour = String.valueOf(currentTime[0:2]);
    int currentMin = String.valueOf(currentTime[3:5]);
    int currentT = currentHour*60 + currentMin;

    return (currentT > comparedT);
}

It's a bit messier, having to muddy into the Strings and whatnot, but it is possible. You would also have to be careful the zero-pad the comparedTime or just check for that in the function

  • 1
    You are using terrible date-time classes that years ago were supplanted by the modern *java.time* classes. See the proper [Answer by Andreas](https://stackoverflow.com/a/59220546/642706). Using `LocalTime.now().isAfter( LocalTime.of( 1 , 0 ) )` is so much simpler. – Basil Bourque Dec 07 '19 at 01:54
  • Again... as I mentioned in my answer, I only wrote that answer to show that it could be done a certain way, as he wasn’t sure how it was possible. I agree Andreas’ is the better answer – infinity8-room Dec 08 '19 at 00:07