Timezone problem

A

ahjiang

Hi all,

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss z");
java.util.Date date = new java.util.Date();
System.out.println(dateFormat.format(date));

I having this piece of code and the output is

2006-05-11 02:54:00 GMT

My location is GMT +8. So i would need to add in another 8 hrs to the
time.

How can i do it such that the piece of code would always offset the
time correctly?

Appreciate any help
 
C

Chris Smith

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss z");
java.util.Date date = new java.util.Date();
System.out.println(dateFormat.format(date));

I having this piece of code and the output is

2006-05-11 02:54:00 GMT

My location is GMT +8. So i would need to add in another 8 hrs to the
time.

How can i do it such that the piece of code would always offset the
time correctly?

SimpleDateFormat extends DateFormat, so it inherits a method called
setTimeZone. That does what you want, if I understand you well.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Steve W. Jackson

Chris Smith said:
SimpleDateFormat extends DateFormat, so it inherits a method called
setTimeZone. That does what you want, if I understand you well.

While calling setTimeZone will provide the result, it might be better to
use a Calendar, or perhaps a GregorianCalendar. This should
automatically contain a date/time with time zone info included.
SimpleDateFormat inherits the setCalendar method, too, and calling that
will give it the information included in a new Date object but include
TZ data as well.

= Steve =
 

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

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top