0

How to convert one time zone value to another time zone value using <s:date>

For example I would like to convert

CTS to GMT+05:30(or IST) using <s:date> tag

In my database I have added date and user time zone value with respect to GMT. my data base server is showing time zone CTS(I am unalbe to chage it). Here I would like to convert date from CTS time zone to user time zone that is GMT+05:30(or IST) or users time zone stored in database

Roman C
  • 49,761
  • 33
  • 66
  • 176
xrcwrn
  • 5,339
  • 17
  • 68
  • 129

1 Answers1

1

For Indian Standard Time, this is the way:

<s:date name = "yourDate" 
      format = "dd/MM/yyyy HH:mm:ss a" 
    timezone = "GMT+05:30" />

In the comments to this related question, you can read about common mistakes you might encounter when dealing with this.

A Date has no TimeZone. A date is just a number of milliseconds since a specific point in time (EPOCH: 01-01-1970, 00:00:00 UTC).

When you save a Date into a database, you are just saving that Long number. If your database has a specific TimeZone, it means that when you will run a query on it, it will format the Dates for human representation with that TimeZone. There are TimeZone settings in your DB, in your AS, in your framework too. But through the whole chain, the Date remains always the same Date object, just represented differently.

Many databases allows you to save the TimeZone informations along with the date. But since you said:

In my database I have added date and user time zone value with respect to GMT.

Then you can absolutely ignore the fact that

my data base server is showing time zone CTS

Just take that Date, and format it with your desired TimeZone, with the code provided.

If this is just confusing you (taking GMT dates, shown as CTS in your DB visualizer, and shown as IST in the browser), then log-print that Date three times in the Action using the three different TimeZones, to have a match between the whole chain, that will help you debugging where the conversions are applied and how.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I have seen that answer. but can we convert from `"CTS+GMT+05:30"`. I want to convert from one time zone to another – xrcwrn Nov 26 '14 at 09:28
  • Dates don't have a Timezone. You deal with Timezones when you are parsing/formatting them with Strings or other objdects. Please update your question with the actual requirement. Are you parsing user entered (String)Dates ? – Andrea Ligios Nov 26 '14 at 09:35