2

This is the date coming from db 2015-06-0403:34:38

I have parsed it like this

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm:ss");
DateTime dateTime = formatter.withOffsetParsed().parseDateTime("2015-06-0403:34:38");

result : 2015-06-04T03:34:38.000+05:00

Now I want this date to add the GMT automatically with +5 in this case so the date would be 2015-06-04 08:34:38

I have tried to add the timezone like this

java.util.Calendar now = java.util.Calendar.getInstance();
java.util.TimeZone timeZone = now.getTimeZone();
DateTimeZone dtZone = DateTimeZone.forID(timeZone.getID());
DateTime dateTimez = dateTime.toDateTime(dtZone);

result : 2015-06-04T03:34:38.000+05:00 (which is wrong)

I am using JodaTime.

TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
skcrpk
  • 558
  • 5
  • 18

2 Answers2

1

When you parse a DateTime object, it returns the DateTime object in the Systems TimeZone. Hence your dateTime object is already in GMT+5.
In dateTimez you are just getting the same DateTime with same timezone. Hence it doesn't change.

Fix:

dateTime = dateTime.withZoneRetainFields(org.joda.time.DateTimeZone) // pass the Timezone of the DB
DateTime dateTimez = dateTime.toDateTime(dtZone);
Codebender
  • 14,221
  • 7
  • 48
  • 85
0

Take a look at this question about how to get timeDiff. Using this answers you can build a method to add or remove hour based on timezone wanted.

Community
  • 1
  • 1
Paolo Fernandes
  • 113
  • 1
  • 7