Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes

Various time classes

Solving all the date and time problems is a tough job (different calendar systems, time zones, date formats, date arithmetic, leap seconds, ...). You will find several classes useful for handling times and dates.

  • java.util.Date - A common representation of dates.
  • java.util.Calendar - From the Java documentation: "Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on." This is a very useful class, and you will often use this in preference to Date in my (limited) experience.
  • java.text.SimpleDateFormat - Useful for both parsing and formatting dates.

There are a lot of details in some areas and you will want to consult the JDK documentation for the specifics, but here are a few simple solutions to common problems.

Time in milliseconds since January 1, 1970

If you aren't familiar with Unix, this may sound a little strange, but it is the common Unix way of measuring time and it has be adopted by Java. If you're just measuring elapsed time, you can call System.currentTimeMillis(), which avoids the overhead of creating a Calendar object. This returns a long integer value. For example,
long startTime = System.currentTimeMillis();
This is not a useful way to get a time or date that is readable by humans, but is fine for figuring out how many seconds or milliseconds something took.

Getting a printable date and time

Simple, but ugly

To get a string which has the current date and time in a fairly readable format, use the default conversion to String.

String rightNow = "" + new Date();

This produces something like "Sun May 02 21:49:02 GMT-05:00 1999". It's not pretty, but it is a quick way to display the time.

Formatting the date

To format a date, use the java.text.SimpleDateFormat class. See the Java API documentation for a good description of this class.

Using the java.util.Calendar class

The Calendar class can be used to get the current date and time, represent dates and times for the past or future, do date arithmetic, determine the hour, day of the week, year, etc. This, as well as java.util.Date are the common classes to use.

   Calendar today = new GregorianCalendar(); // Current date and time.

Because Calendar is an abstract class, you must create a specific kind of calendar to assign to it. The only supported subclass is java.util.GregorianCalendar, which is the calendar that is commonly used

Using a Timer

You can use a javax.swing.Timer object to call a method of yours at regular intervals. This is useful for many things, including animation. The Timer class is easy to use - it calls its action listeners at specified intervals.
Ask programming questions?



Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

i have created various html forms how we can link them so that after login home page will displayed

Posted by prateek on Friday, 05.1.09 @ 17:59pm | #87320

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]