Google Answers Logo
View Question
 
Q: java programming ( Answered 1 out of 5 stars,   2 Comments )
Question  
Subject: java programming
Category: Computers > Programming
Asked by: nisao-ga
List Price: $2.00
Posted: 27 Jan 2003 21:52 PST
Expires: 26 Feb 2003 21:52 PST
Question ID: 149396
HOw to get crrent date from computer
"Jan 15, 2003"
Answer  
Subject: Re: java programming
Answered By: answerguru-ga on 27 Jan 2003 22:10 PST
Rated:1 out of 5 stars
 
Hi nisao-ga,

It is actually fairly simple to obtain the date from within a java
program - all that is required is the java.util.Date and
java.text.DateFormat classes which can be imported as follows:

import java.util.Date;
import java.text.DateFormat;

There is also a tutorial available from Sun that show you how to
customize the output date/time information to suit various locales:

http://java.sun.com/docs/books/tutorial/i18n/format/dateFormat.html

This may be the most useful section for you from that tutorial:

"Formatting dates with the DateFormat class is a two-step process.
First, you create a formatter with the getDateInstance method. Second,
you invoke the format method, which returns a String containing the
formatted date. The following example formats today's date by calling
these two methods:

Date today;
String dateOut;
DateFormat dateFormatter;

dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,
					   currentLocale);
today = new Date();
dateOut = dateFormatter.format(today);

System.out.println(dateOut + " " + currentLocale.toString());

The output generated by this code follows. Notice that the formats of
the dates vary with Locale. Since DateFormat is locale-sensitive, it
takes care of the formatting details for each Locale.

9 avr 98	 fr_FR
9.4.1998	 de_DE
09-Apr-98	 en_US

The preceding code example specified the DEFAULT formatting style. The
DEFAULT style is just one of the predefined formatting styles that the
DateFormat class provides, as follows:

DEFAULT 
SHORT 
MEDIUM 
LONG 
FULL"

For further information about these classes you may want to visit the
Java API pages:

http://java.sun.com/j2se/1.4/docs/api/java/util/Date.html
http://java.sun.com/j2se/1.4/docs/api/java/text/DateFormat.html


Hope that answers your question :)

answerguru-ga
nisao-ga rated this answer:1 out of 5 stars and gave an additional tip of: $1.00
I have book and read sun's wedsite.
those is not working on my programming.
I need the real source code get crrent time from local machine.
That is only the helping web. 
I am a beginner of Java program.
I need the detail information instande of some help web.

Comments  
Subject: Re: java programming
From: guymac-ga on 29 Jan 2003 15:28 PST
 
It is simplest to just create a new instance of java.util.Date. It
will have the current time. E.g. (full example)

import java.util.Date;

public class DateTest {
  public static void main(String[] args) {
    Date now = new Date();
    System.out.println( now );
  }
}

For formatting, use java.text.SimpleDateFormat. E.g.

import java.util.*;
import java.text.*;

public class DateTest {
  static SimpleDateFormat format = new SimpleDateFormat("yyyy MM dd");

  public static void main(String[] args) {
    System.out.println( format.format(new Date()) );
  }
}
Subject: Re: java programming
From: nisao-ga on 05 Feb 2003 13:47 PST
 
I like this answer very much.
Thank you

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy