Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: Various time classes

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz


Bookmark and Share

Java: 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.
 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy