Guilherme
Guilherme

Reputation: 7949

Get current Date with custom time

I have some time Strings such as "09:00" and "17:30" and I need to check if the current time is between that range.

I thought I could make this comparison:

SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date now = new Date();
Date begin;
Date end;

begin = format.parse(begin_string);
end = format.parse(end_string);

if (now.compareTo(begin) > 0 && end.compareTo(now) > 0)
    return true;
else
    return false;

Turns out that when I parse the strings, the times are parsed correctly, but the date is set to Jan 1st 1970. This way, the code will always return false.

I'd like to know how can I set begin and end to the current date, but with the times from their strings.

Upvotes: 1

Views: 3891

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338256

The bundled java.util.Date & .Calendar classes are notoriously troublesome. Avoid them. Use either Joda-Time library or the new java.time package found in Java 8 (inspired by Joda-Time, defined by JSR 310).

If you truly do not care about time zone or date, use either the Joda-Time LocalTime class or the java.time LocalTime class.

Caution: Naïve programmers often think they need only local time and can therefore ignore time zones, but then live to regret that position.

Joda-Time

If your times are in proper ISO 8601 format (24-hours, correct number of digits), then you can directly pass the string inputs to the constructor of LocalTime without bothering to parse. That class has a built-in ISO 8601 style parser.

String inputStart = "09:00";
String inputStop = "17:30";

LocalTime start = new LocalTime( inputStart );
LocalTime stop = new LocalTime( inputStop );

LocalTime now = LocalTime.now();

// Comparing using Half-Open logic, where beginning is inclusive and ending is exclusive.
boolean isNowContainedWithinInterval = ( ( now.isEqual( start ) ) || ( now.isAfter( start ) ) ) && ( now.isBefore( stop ) );

Dump to console…

System.out.println( "start: " + start );
System.out.println( "stop: " + stop );
System.out.println( "now: " + now );
System.out.println( "isNowContainedWithinInterval: " + isNowContainedWithinInterval );

When run…

start: 09:00:00.000
stop: 17:30:00.000
now: 12:42:06.567
isNowContainedWithinInterval: true

In the real-world, I would add an assertion test proving the stop time is later than the start time, to validate inputs.

Upvotes: 0

Meno Hochschild
Meno Hochschild

Reputation: 44061

You could also just reuse your format object for current time like this way:

SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date now = new Date();
String time = format.format(now); // format to wall time loosing current date
System.out.println(time);
now = format.parse(time); // reparse wall time
System.out.println(now);

So you transform now to 1970 using implicitly the standard time zone of your system and can then use it for direct comparisons with begin and end.

Date begin = format.parse("09:00");
Date end = format.parse("21:30");
return (begin.before(now) && end.after(now)); // open-bounded interval

Upvotes: 4

Robby Pond
Robby Pond

Reputation: 73484

You should really use a Calendar. Then you can individually set the hours and minutes from values parsed from the string. Then get the time in milliseconds and compare those.

Date now = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.set(Calendar.HOUR_OF_DAY, hours);
cal.set(Calendar.MINUTE, minutes);
long time = cal.getTimeInMillis();

You could also use the wonderful Joda library. In my opinion Joda is a much better way to work with Dates and Times.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240860

  • Get current time, Calendar.getInstance();
  • Get another 2 instance of current time, and set time fields based on your input

For example:

Calendar.set(Calendar.HOUR_OF_DAY, 1);
  • and invoke compare() on the boundry of time

Upvotes: 2

Related Questions