-2

I have date time field. How to set to value to 8 A.M (if time value is between 16:00-00:00) and in the same time to set it for tomorrow date? I need to increase it with value for one day and set it to 8 A.M

I tried

Calendar calendar = Calendar.getInstance(); 
calendar.setTime(mydate); 
calendar.add(Calendar.DATE, 1);

date= calendar.getTime()

but I do not know how to check if mydate is in interval between 16:00 and 00:00 because than I must do the addition. Also do not know hot to set time value for static 8 A.M value

Please help, thank you

ja jaaaa
  • 115
  • 4
  • 15

2 Answers2

1

Have a look at Calendar class.Set the Date to it and add days and time,and get back the time again.

Calendar calendar = Calendar.getInstance(); 
calendar.setTime(yourdate); 
calendar.add(Calendar.DATE, 1);
c.set(Calendar.HOUR_OF_DAY, 8);;  
date= calendar.getTime();

You can use HOUR_OF_DAY for specific hour.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • calendar.add(Calendar.HOUR, 1); this is problematic part on example if I have date 5 may 17:00 I want to set it to 6 may to 8 A.M I am not doing any addition of hours I am always setting it to ONE THE SAME value - 8 A.M. And mydate filed is always different values – ja jaaaa May 31 '13 at 11:50
  • OK this is what I wanted. I am just missing some if clause to check if my dateField is between 16:00 and 00:00 if this is true than I am doing your code. Else I am not doing anything on my field. How to do this check on my field? tHANK YOU – ja jaaaa May 31 '13 at 12:05
  • I upvoted and I will mark it as an answer but do you know how to determine if time of my date field is between interval 16:00-00:00? Or you can not help me with this? – ja jaaaa May 31 '13 at 12:30
  • @jajaaaa This might be helpful http://stackoverflow.com/questions/4927856/how-to-calculate-time-difference-in-java – Suresh Atta May 31 '13 at 12:36
  • Sorry but I do not need difference. I just need something like this (if myDate is between 16:00 and 00:00) then ....your code... do not need any other calculations – ja jaaaa May 31 '13 at 12:51
  • can you help with this one? regards – ja jaaaa May 31 '13 at 14:13
  • check http://stackoverflow.com/questions/4569476/java-difference-between-two-times – Suresh Atta May 31 '13 at 14:15
  • This is the best you can get http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java/ – Suresh Atta May 31 '13 at 14:15
0

try the solution from this How to add one day to a date?

to add one day. Then just set the time to what you want.

Community
  • 1
  • 1
Panos
  • 7,227
  • 13
  • 60
  • 95
  • I know how to set date increase it with one value but how to set it to static value to time 8 A.M – ja jaaaa May 31 '13 at 11:53
  • Maybe late, just if someone else wants to know the difference is what needs to be used in cal.settime... – Naga Nov 16 '18 at 02:41