• 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

Getting current date

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I am trying to figure out how to retrieve the current system date and display it as a JLabel.
I've looked at the API's for Calendar,DateFormat, and SimpleDateFormat, but I am still very confused. I don't really understand how these classes work. I'm not sure which one of these I should be using. Suns docs do not have any examples that helped me out any. (It was a lot easier in RPG when I could just code 'SYSDAT' and voila!)
Any help would be greatly appreciated!
Thanks a bunch
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the API for the Calendar class a bit closer.

(for jdk 1.3.1)... the third paragraph shows you how to obtain a GregorianCalendar object with the current date and time.

Then look in the GregorianCalendar API, there is a HUGE code listing, showing you how to obtain any part of that date.

If that all seems like way to much work... there is a 'quick and dirty' way of obtaining the current date and time.

It involves the java.util.Date class. Check the API, and read what it says for the constructor.
(it says it makes a Date object with the current system time). Then look at its toString() method. It says that it converts the Date object to a formatted String.

If this format is unacceptable, then you would use this Date object in combination with a SimpleDateFormat object, to convert it into whatever format you want.

I admit the formatter is a bit confusing, so here is an example:In this example, d is a java.util.Date object. In the API for SimpleDateFormat, it explains what all the dd MMM yyyy, etc... means.
[ February 20, 2002: Message edited by: Mike Curwen ]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is one way:
import java.util.*;
import java.text.*;
public class DateTest1 {
static public void DisplayDate() {
Date today;
String result;
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("MM/dd/yyyy");
today = new Date();
result = formatter.format(today);
System.out.println(result);
}
static public void main(String[] args) {
DisplayDate();
}
}
Note: There is a calander class for this as well, but here is what I understood you wanted.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jennifer,
This may be a start:

Also take a look at the System class' methods (e.g getProperty)
Erik Dark
[ February 20, 2002: Message edited by: Erik Dark ]
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your help.
All of you guys are great!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic