0

I have REPORTDATE field where I want to take the value just of the hours and minutes. reportdate.getTime() returns this for example: 1439967368798

I want to compare if TIME from reportdate (hours and minutes) are between:

if reportdate>=06:45 and reportdate<13:45 then a=1;
if reportdate>=13:45 and reportdate<20:45 then a=2;
if reportdate>=20:45 and reportdate<06:45 then a=3;

I did not succeed to find some instructions on web. Should I try with this by using the value that I am getting from getTime (1439967368798) or with some other method?

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
Veljko
  • 1,708
  • 12
  • 40
  • 80
  • You should probably use a [`Calendar`](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) for this. – Dragondraikk Aug 19 '15 at 07:20
  • Either use JodaTime or Java 8's Time API, for example `LocalTime lt = new Date(1439967368798L).toInstant().atZone(ZoneId.systemDefault()).toLocalTime();`, this will then allow you to use `before` and `after` (and `equals`) to compare other instances of `LocalTime` – MadProgrammer Aug 19 '15 at 07:21

2 Answers2

0

I assume that reportDate is an instance of Date. The getTime() function returns the underlying long of the Date, thats why you see this long number - miliseconds since January 1, 1970, 00:00:00 GMT.

What you need to do is convert the date to a calendar and then use the get() Method with the appropriate field constant.

This information is for Java 7 without any libraries. It would be wise to use a library like JodaTime or the Java8 Time API.

Here is an example how to do that.

morpheus05
  • 4,772
  • 2
  • 32
  • 47
  • Hi I cannot use JodaTime because I am programming this in some IBM application with limited Java framework where I cannot include some additional libraries – Veljko Aug 19 '15 at 07:29
0

Like this?

Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
if (date.after(format.parse("06:45")) && date.before(format.parse("13:45"))) {
 a=1;
}
Les
  • 242
  • 1
  • 4
  • 13
  • yes will it work also when comparing 20:45 and 06;45? – Veljko Aug 19 '15 at 07:29
  • where are you comparing with value from REPORTDATE field? – Veljko Aug 19 '15 at 07:34
  • @Dejan Sorry, I show principle, not a ready solution. Report date could be inserted here Date date = reportDate; – Les Aug 19 '15 at 07:57
  • @Dejan "comparing 20:45 and 06;45". You meant, does this works `format.parse("20:45").after(format.parse("06:45"))`? Yes. – Les Aug 19 '15 at 07:59