Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Time and Date class

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nasir Munir
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
    Bookmark Topic Watch Topic
  • New Topic