-1

I'm trying to parse time only but the app code includes the date and the year.

here is my code:

simpleDateFormat2 = new SimpleDateFormat("HH:mm");
int h = Integer.parseInt(traffic.alarmClocks.get(0).get(ApplicationConstants.HOUR)); 
int m = Integer.parseInt(traffic.alarmClocks.get(0).get(ApplicationConstants.MINUTE));
String  datex2 = h + ":" + m;
Date storedalarm = simpleDateFormat2.parse(datex2);

output of the datex2 is : 4:56

Output of StoredAlarm is this: https://i.stack.imgur.com/EQT06.jpg

The output of the datex2 is correct, but I need to make it into date because I am going to use it to compare times.

lelloman
  • 13,883
  • 5
  • 63
  • 85
  • Probably you stored like `hh`, not `HH`, so is `04`, not `16`. `HH` will save it like `24H` and `hh` with `12H`, with `AM` and `PM`. Also, check for time zone. Do you have 12 hours difference? – Cătălin Florescu Aug 04 '17 at 10:24
  • if you are ignoring year, month and day, why do you need a date for comparison? can't you just compare hours and minutes? – lelloman Aug 04 '17 at 10:26
  • @lelloman i'm using this reference: https://stackoverflow.com/questions/17697908/check-if-a-given-time-lies-between-two-times-regardless-of-date to compare times. it requires me to parse date. –  Aug 04 '17 at 10:31
  • show how you format a `storedalarm` with a `simpleDateFormat2` – Vladyslav Matviienko Aug 04 '17 at 10:31
  • @FlorescuGeorgeCătălin its still returning data with the date and year. –  Aug 04 '17 at 10:35
  • `SimpleDateFormat` is used for formatation (eg from time-stamp to something pretty). From text to date and reverse. You can't obtain a `Date` object with just hour and minute. You can compare strings. `"string".compareTo("otherString");` is the same thing. – Cătălin Florescu Aug 04 '17 at 10:42
  • @FlorescuGeorgeCătălin if you just need to compare hours a minutes you can set the date to 0, so all dates will have the same date 1 Jan 1970 and the comparison will be affected only by hours and minutes. but the easiest way to do this is to just compare hours and mintues. – lelloman Aug 04 '17 at 10:45

1 Answers1

1

you just need to compare times you could very easily combine hours and minutes this way:

int time = hours * 60 + minutes;

then you could just compare 2 integers.

or if you really want a Date object, you could initialize it with year, month and date to 0, and just pass hours and minutes

Date storedalarm = new Date(0, 0, 0, h, m);

in order to show just hours and minutes from your Date object you can use the same SimpleDateFormat you instantiated before

String formattedDate = simpleDateFormat2.format(storedalarm);
lelloman
  • 13,883
  • 5
  • 63
  • 85
  • i need to use datex2. –  Aug 04 '17 at 10:38
  • what do you mean? in the code you posted `datex2` created from `h` and `m` – lelloman Aug 04 '17 at 10:39
  • it's still showing the same error i'm experiencing, it still includes the month and the year. here's the full code: https://paste.ofcode.org/nxPE79bNmFjfssBwQPAvXR –  Aug 04 '17 at 10:54
  • 1
    of course it is showing month and year, it's a **Date** object. what is the problem? you want to show only hours and minutes from a date object? – lelloman Aug 04 '17 at 10:56