Doug McComber
Doug McComber

Reputation: 33

SimpleDateFormat in Android giving unwanted time zone in Android 1.6

Given the following:

String dt = "Wed Jan 1 12:34:03 2010";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy");
Date output = sdf.parse(dt);

Produces:

Wed Jan 1 12:34:03 ADT 2010

Where is the timezone coming from? I don't have z in my format pattern.

Thanks, Doug

Upvotes: 2

Views: 245

Answers (2)

leifg
leifg

Reputation: 8988

When a Date object is created, the timezone is set from the system settings.

try output.getTimeZoneoffset() and it returns the offset in minutes (for ADT it's -180)

Upvotes: 0

BalusC
BalusC

Reputation: 1108557

You're apparently displaying the toString() outcome of the Date object like as

System.out.println(output);

The format is specified in the javadoc and it indeed includes the timezone.

toString

public String toString()

Converts this Date object to a String of the form:

dow mon dd hh:mm:ss zzz yyyy

You need SimpleDateFormat#format() to convert the obtained Date object to a String in the desired format before representing it. For example,

String s = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(output);
System.out.println(s); // 01-01-2010 12:34:03

Upvotes: 3

Related Questions