This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
How can we pass a specific time as a parameter to a method ? For instance if I have to set time to this : "00:00:00.0000000-05:00"
Time wishTime = new Time() ; wishTime.setTime(long lg) ; I can't use 00:00:00.0000000-05:00 in setting time as it is expecting long. Is there a way of doing that ? I have a similar question for date as well. How would you set a specific date, for example "03-03-2008" Date wishDate = new Date(); wishDate.setDate(int i) ; I can't use my date here, as it is expecting an integer value.
can I get some help here? thanks [ March 05, 2008: Message edited by: Nasir Munir ]
If you check the API for Date, you will see that much of its functionality is now deprecated, with the java.util.Calendar class preferred. You should check the API for all the different options -- including getting times down to the millisecond -- but basically you get an instance of Calendar by calling a static method, Calendar.getInstance(). This is set to the time the instance was created. To change that, you can call set. For example...
Calendar cal = Calendar.getInstance(); cal.set(1964, 4, 7);
This version of set uses ints for year, month, and date (where January is 0). For output, you will probably want an instance of java.text.DateFormat. For example...
Here I am getting a DateFormat instance using a "LONG" format and a US Locale. In the next line, I'm calling getTime on my Calendar instance (which returns a Date), and then formating that Date by calling format.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: May 31, 2002
I got date part right, but am having problems with setting the required time as "00:00:00.0000000-05:00" The instance of Calendar gives me the option of setTime(milliseconds), but there is no option of entering hours, mins, sec, and also the time zone as -05:00. Secondly, if I have to set number of time and dates, do I need to create as many instances of calendar class? It is looking awkward, I must be wrong. For example, setting start date, and end date (2 instances) similarly start time and end time (2 instances) Need some help here, thanks.
For most of these settings, I think the method you want is set(int field, int value), using Calendar's constants to specify the field. For example, to set the milliseconds to 379...
Note that you can also use this method with Calendar.ZONE_OFFSET for the field and "raw offset from GMT in milliseconds" for the value.
Or you can use Calendar's setTimeZone method.
If you're using a DateFormat instance, this will also adjust the time zone. (So even if your Calendar instance is storing it the way you want it, your DateFormat might change it if you're not careful.)
TimeZone tz = TimeZone.getTimeZone("GMT-5:00"); //create the desired TimeZone instance df.setTimeZone(tz);
Joined: May 31, 2002
another problem: If I change the date format it returns a string. I have to pass the argument as date. Mon Mar 03 00:00:00 AST 2008 I can format that to: 2008-03-03 but I cannot pass it as a parameter because now it is a string. If I use parse method of Date, it changes back to Mon Mar 03 00:00:00 AST 2008. Any idea ? Thanks