0

I am creating web application where I need to store timestamp/ date.

The problem is that I have hosted my site on one of the hosting site and hosting site is based on US. When the data is store, US timing are store and not my own country timing.

I believe I need to set the hosting server time to my country timing OR I need to convert the time before I save. I tried few code, however I am not getting.

Could someone help me how to convert the time in java?

When I print

 System.out.println(new Date());

I get US time and not my current time. I need to convert this time to show as per My country time i.e. Kuwait timing.

Edit 1

When I print date as System.out.println(new Date());, I get output as Thu Sep 06 11:07:49 EDT 2012 which means I am getting server time.

Now I want to convert this time to some country time, lets say Kuwait OR Any other country i.e. same time should be displayed as current country.

i.e. the time for country Kuwait should be Thu Sep 06 19:07:49 AST 2012

For India it should be Thu Sep 06 21:37:49 IST 2012

Edit 2

Questions with better wording Hope So

I have created a web application using JSF where my server is in US. I am storing timestamp in database. Now while printing the time, I will obviously get the time that is there in DB i.e. US data.

I am preparing report where I want to display data along-with timings.

+++++++++++++++++++++++++++++++++++
+   Name    +      Time           +
+++++++++++++++++++++++++++++++++++
+ Name 1    +  10/09/2012 10:11   +
+ Name 2    +  10/09/2012 21:11   +
+ Name 3    +  10/09/2012 17:11   +
+++++++++++++++++++++++++++++++++++

Now the problem is that those timings are as per US timing hence I need to convert those US timing to my country time where I am. I need to display this time where I am accessing internet i.e. if I am in kuwait, I need those timings as per Kuwait timings. If I am accessing this data in Dubai, I need to display report as per Dubai timings.

Could someone help how could I get rid of these problem?

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

3 Answers3

0

java.util.Date should always be giving you UTC. When you put it inside of a call to println it's converted to local time (where the code is being executed) because there's an implicit call to .toString() inside of the call to .println().

Make sure you're storing the date as the original value and that .toString() isn't being called anywhere along the way (such as when the DB converts it to a timestamp for staged), etc.

Jon Skeet (in the answer I referenced) also suggests possibly using an alternate datetime package, which isn't exactly what you're asking for, but may provide at least some alternate ways of looking at the problem.

Community
  • 1
  • 1
jefflunt
  • 33,527
  • 7
  • 88
  • 126
0

You might to look at Internationalization items related to Date. Here is a good link that might be useful to get some start. Internationalization

sathish_at_madison
  • 823
  • 11
  • 34
0

If you are using time manipulations you should use a Calendar Object getting an instance with the Calendar#getInstance(Timezone) static method, providing your a TimeZone object that corresponds to your timezone.

If you have to print a Date object with a defined timezone get a DateFormat object and set its timezone to one that corresponds to your liking.

Here is a sample:

SimpleDateFormat dateFormat = new SimpleDateFormat(
                                "yyyy.MM.dd G 'at' HH:mm:ss z", Locale.UK);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+1:00")); //London?

String fullDateUK = dateFormat.format(new Date());

System.out.println(fullDateUK);
ElderMael
  • 7,000
  • 5
  • 34
  • 53
  • I believe the same thing applies there, you just need to use a specialized DateFormat like SimpleDateFormat and set the pattern you need to print. – ElderMael Sep 07 '12 at 16:46
  • Well, as for edit 2 you will need to store your users TimeZone and use date converters for those components and set the converters timeZone attribute to your user timezone... – ElderMael Sep 07 '12 at 17:11
  • if you can surf through code you may want to check this app, I had to use some formatting and timezone stuff: https://bitbucket.org/ElderMael/jgtjo/ – ElderMael Sep 07 '12 at 17:12