Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I make a date with Calender???

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
0
0
GB
Hi.
I'm trying to create a date and have decided to use Calendar.
I've tried:

Calender myDate = Calendar.getInstance();

then:

String printableDate = myDate.DAY_OF_MONTH + "\\" + myDate.MONTH + "\\" + myDate.YEAR;

But for some reason I get the wrong date appear? My clock's set correctly and when I print out myDate it displays the correct values in amongst all the other data.

If anyone knows how I can easily create a date with Calendar I'd be VERY grateful!
Cheers
Oxy

we are all of us living in the gutter.
But some of us are looking at the stars.
 
Hi,

To get the current date using calendar, u need to specify the time zone properly.
u can refer to the documentation under GregorianCalendar in JAVA API. Either java.util.TimeZone or java.util.SimpleTimeZone classes are used to specify the tim zone. for the example the documentation under GregorianCalendar is the best.

Actually, my advice is that u should use java.util.Date only to get the current date.

TO get the current time then, u should use calendar. For this, i m attaching the example below.


Date currentDate = new Date();

Calendar c = new GregorianCalendar();
c.setTime(currentDate);
int cHour = c.get(Calendar.HOUR_OF_DAY);
int cMinute = c.get(Calendar.MINUTE);
int cSecond = c.get(Calendar.SECOND);

long currentTime = cHour*60*60*1000 + cMinute*60*1000 + cSecond*1000;


Rgds,
Payal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top