How to get the system date and time?

W

wang

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
 
B

bbmyth

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.
 
R

Roland

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_/ /__/
/ \ /_/ / \
 
O

ozgwei

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top