• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

date and time

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
vipin
 
mangesh lele
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the 'toString()' method is always automatically called when an object is printed out.
Rene
 
mangesh lele
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rene
it dint work on my machine with jdk 1.3
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read about the method 'println(Object)' in the JDK 1.3 API
PS. follow the links in the API..
Rene
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mangesh, what did you try to do (exactly)? What error message(s) did you receive?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic