Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DateTime Problem

 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this post: http://www.coderanch.com/t/281712/JSP/java/Rolling-Dates-java-util-GregorianCalendar
and use:

when you add e.g. an hour to the time.
Rene
 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well... you could try look at the API for the 'Calender' class, witch the 'GregorianCalendar' is extending from. In this class there are some 'set(..) methods.
Rene
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rene and Blake for the fantastic replies!
Much appreciated!!!
-- Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic