This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes date and time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "date and time" Watch "date and time" New topic

date and time

mangesh lele

Joined: Jan 17, 2003
Posts: 18
can anybody tell me how to get current system time and date as output?
i looked up the docs and found an example that requires setting timezone, using class GregorianCalendar
there must be a less complicated way!
VipinJCP Dube

Joined: Jan 02, 2003
Posts: 4
Hi Mangnesh
Ofcourse , Java has a simple wayout to you problem.
For accessing current Date and Time , you will have to create an object of Date class and access the date and time, like
System.out.println(new java.util.Date());

and GregorianCalendar class is used to change the date format. So try out these also
GregorianCalendar d1 = new GregorianCalendar(1988, 05, 06); // Jun 6
GregorianCalendar d2 = new GregorianCalendar();// today
Calendar d3 = Calendar.getInstance();// today
System.out.println("It was then " + d1.getTime());
System.out.println("It is now " + d2.getTime());
System.out.println("It is now " + d3.getTime());
d3.set(Calendar.YEAR, 1915);
d3.set(Calendar.MONTH, Calendar.APRIL);
d3.set(Calendar.DAY_OF_MONTH, 12);
System.out.println("D3 set to " + d3.getTime());
SimpleDateFormat formatter
= new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("It is " + formatter.format(dNow));

all the best,
mangesh lele

Joined: Jan 17, 2003
Posts: 18
thanx very much vipin
btw i think that instead of : -
System.out.println(new java.util.Date());

it should be : -
System.out.println((new java.util.Date()).toString());
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

the 'toString()' method is always automatically called when an object is printed out.

Regards, Rene Larsen
Dropbox Invite
mangesh lele

Joined: Jan 17, 2003
Posts: 18
it dint work on my machine with jdk 1.3
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You can read about the method 'println(Object)' in the JDK 1.3 API
PS. follow the links in the API..
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
mangesh, what did you try to do (exactly)? What error message(s) did you receive?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
It is sorta covered in the JavaRanch Style Guide.
subject: date and time
Similar Threads
advanced calculator
What is the Data Type for GMT Date and Time?
displaying time
Date question
how to convert String into Date format