I declared Calendar and SimpleDateFormat like this:
calendar = Calendar.getInstance(TimeZone.getTimeZone("Malaysia"));
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MMMMM.dd hh:mm aaa");
or:
calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
Then I call this:
sdf.format(calendar.getTime());
but result is not in correct time zone (+8 hours). What could be the problem?
Unless you are going to perform Date/Time related calculations, there is no point in instantiating Calendar with given TimeZone. After calling Calendar’s
getTime()method, you will receiveDateobject, which is timezone-less either way (GMT based, actually).What you need to do, is to set
TimeZonefor formatter instead. And also do not bother with passing your own format, there is a built-in already:This prints something like
10 Mei 2011 2:30:05 AM, which I believe is your desired result.