File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes DateTime Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DateTime Problem" Watch "DateTime Problem" New topic
Author

DateTime Problem

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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

Joined: Oct 12, 2001
Posts: 1179

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


Regards, Rene Larsen
Dropbox Invite
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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

Joined: Oct 12, 2001
Posts: 1179

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

Joined: Sep 13, 2002
Posts: 331
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"
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

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

Joined: Jul 12, 2002
Posts: 1034
Thanks Rene and Blake for the fantastic replies!
Much appreciated!!!
-- Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DateTime Problem
 
Similar Threads
TimeZone formatation
Total Time duration
Time.toString() format
Date API
Unix OS on Windows Vista ?