turtleboy
turtleboy

Reputation: 7574

Joda DateTime object adding +1 hour

i have the following code:

        Log.e(TAG, "startTime = " + startTime);

        DateTime dateTimeStart = new DateTime(startTime);

        Log.e(TAG, "dateTimeStart = " + dateTimeStart  );

.

which when logged out produces the following:

 startTime = 2014-10-30T12:00:00+00:00

dateTimeStart = 2014-10-30T13:00:00.000+01:00 

.

Why is an extra hour getting added on to the original time?

edit How can i remove the +1:00, i haven't specified that.

Thanks

Upvotes: 2

Views: 315

Answers (3)

Ilya
Ilya

Reputation: 29693

Default DateTime::toString() method returns date in format yyyy-MM-ddTHH:mm:ss.SSSZZ .
+01:00 and +00:00 are the timezone offsets (ZZ in date format).

So if you want to print date without timezone offset, you should use another format. E.g. with method DateTime::toString(String):

String dtFormat = "yyyy-MM-dd'T'HH:mm:ss";
Log.e(TAG, "startTime = " + startTime.toString(dtFormat));   
...  

Log.e(TAG, "dateTimeStart = " + dateTimeStart.toString(dtFormat ));

Upvotes: 0

Ravi Sharma
Ravi Sharma

Reputation: 843

Use split method.

 String splitDateTime[]=dateTimeStart.split("\\+");

 dateTimeStart=splitDateTime[0];

Upvotes: 0

Karol S
Karol S

Reputation: 9411

DateTime is an object consisting of a date, a time, and a timezone. In your case, you took startTime and converted it into an equivalent DateTime using the default system timezone.

+01:00 means "this timestamp is in some UTC+1 timezone", so 12:00:00.000+00:00 means the same as 13:00:00.000+01:00

So your timestamp was created at 12:00 British time = 13:00 Central European time.

If you want the time in UTC, do

DateTime dateTimeStart = new DateTime(startTime, DateTimeZone.UTC);

Upvotes: 4

Related Questions