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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Time and Date class" Watch "Time and Date class" New topic
Author

Time and Date class

Nasir Munir
Greenhorn

Joined: May 31, 2002
Posts: 22
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.

someMethod(wishTime);
otherMethod(wishDate);

can I get some help here? thanks
[ March 05, 2008: Message edited by: Nasir Munir ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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...

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
System.out.println(df.format(cal.getTime()));

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
Nasir Munir
Greenhorn

Joined: May 31, 2002
Posts: 22
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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...

cal.set(Calendar.MILLISECONDS, 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);
Nasir Munir
Greenhorn

Joined: May 31, 2002
Posts: 22
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I'm not sure I'm following.

Rather than converting between a Date and a String and then back again, can't you just keep using the same Date reference and only format to a String when you need formatted output?
 
 
subject: Time and Date class