0

i am so confuse how to do that i have a database date in UTC i want to convert in any given format by user kile IST ,WAST,CST . Iwant to know how can i do so.I am passing the zone value which is given by client here is my code :

for getting value from client i am using this:

var localTimeZone = new Date().getTimezone(); 

after that i want to a method which have three parameter

targetTimeFromZone(Date date, String fromTZ, String toTZ);

where fromTZ="UTC" and toTZ = "IST" or CST or any of time Zone.

Naresh
  • 632
  • 4
  • 19
  • `java.util.Date` doesn't have timezone with its state – jmj Jun 10 '14 at 18:33
  • so please let me know how it is possible – Naresh Jun 10 '14 at 18:34
  • 2
    You've got tags of both javascript and java. They're different languages - what are you actually using? (It looks like Javascript rather than Java, but who knows.) Also note that using abbreviations for time zones is a really bad idea - they're not unique, so they don't *really* identify the zone. – Jon Skeet Jun 10 '14 at 18:36
  • thanks Jon i am using javascript in client side which give me the zone name like IST after that i am using java as a backend for get the converted date according to client – Naresh Jun 10 '14 at 18:39

1 Answers1

2

Avoid java.util.Date and .Calendar as they are notoriously troublesome. Use either Joda-Time or the java.time package in Java 8.

Per Jon Skeet's comment, avoid 3 or 4 letter time zone codes as they are neither standardized nor unique. Use proper time zone names.

A DateTime in Joda-Time knows its own assigned time zone, unlike j.u.Date.

DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" ); // or get the default time zone.

Convert a Date to DateTime.

java.util.Date someDate = new java.util.Date(); // As if we were passed a Date.
DateTime dateTime = new DateTime( someDate, timeZone );

Adjust time zone.

DateTime dateTimeMontréal = dateTime.withZone( DateTimeZone.forID( "America/Montreal" ) );
DateTime dateTimeUtc = dateTime.withZone( DateTimeZone.UTC );

If required by other classes, generate a java.util.Date object.

java.util.Date date = dateTime.toDate();

Search StackOverflow for many more examples and discussion.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • thanks a lot but is there any method to pass "IST" insted of "Asia/Kolkata" means only code – Naresh Jun 10 '14 at 19:16
  • @naresh Do you mean Irish Standard Time, India Standard Time, or Israel Standard Time? Reread my second paragraph and Jon Skeet's comment. – Basil Bourque Jun 10 '14 at 19:21
  • so how can i get "Asia/Kolkata" value from javascript new Date() function – Naresh Jun 10 '14 at 19:26
  • @naresh I don't know anything about JavaScript. You could try searching StackOverflow, to find questions like [this](http://stackoverflow.com/q/23546755/642706) and [this](http://stackoverflow.com/q/22618056/642706). – Basil Bourque Jun 10 '14 at 22:48