I'm writing a calendar application where the user enters a meeting start time and duration. I need to take this start time (say 12/28/2002 11:00) and add the duration (in hours) to it so I can do a lookup to make sure the person is available the entire time). Is there an easy way to add a number of hours to a user-entered datetime string? I can convert the string to a datetime, but that doesn't seem to simplify anything either. My question is: given 12/28/2002 11:00 can I quickly come up with, if duration is 3 hours, 12/28/2002 2:00 (just added 3 hours to the start time). Thanks for anyone's reply! -- Mike
Hi Rene, Yes that's helpful, but really doesn't solve the problem. The problem is that given a text string for the datetime (e.g., "12/28/2002 11:00"), I was wondering if there were a way to get this text into a GregorianCalendar and then do the add(), THEN get it back out into a text field (as, say "12/28/2002 2:00"). I need to use the text as part of a SQL where clause. The basic issue is how to easily convert to gc from a text entry and back to text after doing the add() methods. Can you elaborate on your excellent post you referred me to? Thanks much. -- Mike
I was wondering if there were a way to get this text into a GregorianCalendar and then do the add(), THEN get it back out into a text field (as, say "12/28/2002 2:00")
The only way I know of to do the string-to-date-to-string conversion you need is like this: 1. Use SimpleDateFormat to parse the string into a java.util.Date object. 2. Create an instance of a Calendar object and initialize it to your date using setTime(). 3. Use the Calendar.add() method to increment your date/time as necessary. 4. Use Calendar.getTime() to get your new Date and then use the SimpleDateFormat again to format that date back into a String. Hope that helps... if you get stuck I can probably find some sample code I've used before to do a similar task.
Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
and to get it back to a text string you need to use the get method. e.g. (gc.get(GregorianCalendar.MONTH) + 1) + "/" + gc.get(GregorianCalendar.DAY_OF_MONTH) + "/" + gc.get(GregorianCalendar.YEAR)..... PS. remember to add a zero to the day and month if it is less then 10, and when you read the month from the calender object you need to add 1 to it (like my sample) Rene
Joined: Jul 12, 2002
Thanks Rene and Blake for the fantastic replies! Much appreciated!!! -- Mike