File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes how to get date month year time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to get date month year time " Watch "how to get date month year time " New topic

how to get date month year time

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
could you kindly tell me how to get the "date, month, year, time of the system".

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you create a date (little d) object, it will contain the current system date & time. Then you can query the year, month, day, hours, etc.
Here's the JavaDoc for one date/time class: GregorianCalendar There are also Date classes in java.util and java.sql. They have different query methods.
Let's ask the rest of the gang how to choose which one to use.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Which to use depends on what you want to do with it. Mostly you should use java.util.Calendar (of which GregorianCalendar is a subclass, but this should be entirely transparent.) To get the current system date, use:

This will return a Calendar initialized to the current date/time for the Local that you are in (for most in the Western World, this will be an instance of the GregorianCalendar).
Note that you cannot create a new Calendar directly. (new Calendar() is not permitted).
You can do the same thing with a java.util.Date:

This, to, will return the current system time.
The difference between Calendars and Dates is that Dates are conceptually immutable. (They used to be changeable, but this functionality was depricated in Java 1.2, I think) If you need to do date calculations, use Calendars. If you are storing the dates, use Dates (You can convert between the two through the Calendar's .getTime() and .setTime(Date ) methods). Note that java.sql.Date is a subclass of java.util.Date.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: how to get date month year time
It's not a secret anymore!