Feb 25, 2018 - This java tutorial shows how to use the setTimeZone(TimeZone value) method of Calendar class of java.util package. This method sets the time. Uses of TimeZone in java.text. Methods in java.text with parameters of type TimeZone. Sets the time zone for the calendar of this DateFormat object.
I would like to start by saying that I've read several threads similar to this one, but none of them really solved my problem.I would also like to state that I've tried to use
SimpleDateFormat
and joda.DateTime
without any success.The problem is the following:
I have a Calendar object that holds the information about a specific date: 2008-04-30T00:00:00ZWhen using the
calendar.getTime()
method I can get different results because I know that that method is looking for the local valueThus:
But I would like to get a Date object that holds just the Date and Time values '2008-04-30T00:00:00' ignoring completely any timezone.
How can I do that?
As I mentioned before I tried to use
SimpleDateFormat sf = new SimpleDateFormat('yyyy-MM-dd'T'HH:mm:ss')
but I always end up with the same results.Any help would be really appreciatedCheers.
MaLLinokMaLLinok
4 Answers
Found out that you can clear the Timezone by using code below:
Calendar cal = Calendar.getInstance();cal.clear(Calendar.ZONE_OFFSET);
GuestOnlyGuestOnly
Calendars
and Dates
mean nothing without a TimeZone
.Calendars and dates cannot exist without a timezone.
You can't
ignore completely any timezone
.You can create a Calendar for Greenwich Mean Time (offset zero) like this:
This represents a Date/Calendar that is only meaningful in the GMT timezone.
It sounds like you want a timestamp, which represents an instant in time.
jahroyjahroy
Do you use a standard constructor for initializing Calendar? What if you used the constructor which allows to specify the time zone and locale?
dgldgl
Old, but still incorrect.
'When using the calendar.getTime() method I can get different results because I know that that method is looking for the local value'
That is a misconception. getTime() will get the Milliseconds only. Countet as GMT.
ONLY during formatting of the Output the time zone becomes relevant. Sind the original poster did not show the code, it can not be decided, where the error occurs.
Gunnar BernsteinGunnar Bernstein
Not the answer you're looking for? Browse other questions tagged javadatetimetimezone or ask your own question.
Syntax
Calendar.setTimeZone(TimeZone value) has the following syntax.
Example
In the following code shows how to use Calendar.setTimeZone(TimeZone value) method.
The code above generates the following result.
Home »
Java Tutorial »
java.util »
Java Tutorial »
java.util »