wood burning stoves 2.0*
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
Author

how to get date month year time

Ransika deSilva
Ranch Hand

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


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
Bartender

Joined: Aug 20, 2001
Posts: 1817

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: http://aspose.com/file-tools
 
subject: how to get date month year time
 
Similar Threads
How to get day, month, and year seperate from date
dateformat in textfield
Question on Gregorian Calendar
date dateformat
Date for last month