How to get the system date and time?

Discussion in 'Java' started by wang, Oct 16, 2005.

  1. wang

    wang Guest

    Hi all,
    I want to get the system date and time with java on
    windows platform. I've tried with the class GregorianCalendar,
    but without success. Can you explain me how to do it?
    It would be very helpful if you can explain with a simple
    example. Thanks in advance.
    k.w.wang
     
    wang, Oct 16, 2005
    #1
    1. Advertising

  2. wang

    Roedy Green Guest

    On 16 Oct 2005 09:06:39 -0700, "wang" <> wrote or
    quoted :

    >I want to get the system date and time with java on
    >windows platform. I've tried with the class GregorianCalendar,
    >but without success. Can you explain me how to do it?
    >It would be very helpful if you can explain with a simple
    >example. Thanks in advance.


    see http://mindprod.com/jgloss/time.html
    http://mindprod.com/jgloss/calendar.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 16, 2005
    #2
    1. Advertising

  3. wang

    bbmyth Guest

    you can do it in two ways.
    one:use the Date class.
    //-------------------------------------------------
    Date now = new Date();
    int date = now.getDate();
    int hour = now.getHours();
    int second = now.getSecond();
    //-------------------------------------------------
    two:use the Calendar calss
    //-------------------------------------------------
    Date now = Calendar.getInstance().getTime();
    int date = now.getDate();
    int hour = now.getHours();
    int second = now.getSecond();
    //-------------------------------------------------
    that's all.
     
    bbmyth, Oct 16, 2005
    #3
  4. wang

    Roland Guest

    On 16-10-2005 18:06, wang wrote:
    > Hi all,
    > I want to get the system date and time with java on
    > windows platform. I've tried with the class GregorianCalendar,
    > but without success. Can you explain me how to do it?
    > It would be very helpful if you can explain with a simple
    > example. Thanks in advance.
    > k.w.wang
    >

    import java.util.Date;
    ....
    Date now = new Date();
    System.out.println(now);


    If you need to output in a specific format, you can use SimpleDateFormat
    (or its superclass DateFormat):

    import java.text.SimpleDateFormat;
    import java.util.Date
    ....
    Date now = new Date();
    SimpleDateFormat formatter =
    new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
    System.out.println(formatter.format(now));
    --
    Regards,

    Roland de Ruiter
    ` ___ ___
    `/__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Oct 16, 2005
    #4
  5. wang

    ozgwei Guest

    Instead of:
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy
    hh:mm:ss");

    try:
    DateFormat formatter = DateFormat.getDateInstance();
    which gets the date formatter with the default formatting style for the
    default locale.

    or
    DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM);
    which gets the date formatter with the MEDIUM style of the default
    locale.
    There are totally four styles: DateFormat.SHORT, DateFormat.MEDIUM,
    DateFormat.LONG, DateFormat.FULL.
    DateFormat.DEFAULT defaults to DateFormat.MEDIUM.

    or
    DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM,
    aLocale);
    which gets the date formatter with the MEDIUM style of the given
    locale.

    or
    DateFormat formatter = DateFormat.getTimeInstance();

    or
    DateFormat formatter = DateFormat.getTimeInstance(DateFormat.MEDIUM);

    or
    DateFormat formatter = DateFormat.getTimeInstance(DateFormat.MEDIUM,
    aLocale);

    or
    DateFormat formatter = DateFormat.getDateTimeInstance();

    or
    DateFormat formatter =
    DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    The first parameter indicates the style of the date while the last
    parameter indicates the style of the time.

    or
    DateFormat formatter =
    DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM,
    aLocale);

    Choose the one that suits your needs the best.
     
    ozgwei, Oct 17, 2005
    #5
  6. wang

    wang Guest

    With your help I've solved the problem. Many thanks!

    k.w.wang
     
    wang, Oct 19, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    606
    Matthew Speed
    Nov 8, 2003
  2. Chris Berg
    Replies:
    0
    Views:
    794
    Chris Berg
    Oct 27, 2003
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,276
    Michael Borgwardt
    May 30, 2004
  4. Keith Cochrane
    Replies:
    2
    Views:
    625
    Keith Cochrane
    Aug 6, 2006
  5. Summu82
    Replies:
    5
    Views:
    868
    Richard Heathfield
    Jun 7, 2006
Loading...

Share This Page