Getting the date in another timezone is not quite as evident. But I did whip up a method to return the Date object if you provide your local Date and the timeZone id of where you want to find out what time it is there.
import java.util.*;
public class Timezone {
public static Date getDateInTimeZone(Date currentDate, String timeZoneId)
{
TimeZone tz = TimeZone.getTimeZone(timeZoneId);
Calendar mbCal = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
mbCal.setTimeInMillis(currentDate.getTime());
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, mbCal.get(Calendar.YEAR));
cal.set(Calendar.MONTH, mbCal.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, mbCal.get(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, mbCal.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, mbCal.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, mbCal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, mbCal.get(Calendar.MILLISECOND));
return cal.getTime();
}
public static void main(String[] args)
{
Date now = new Date();
System.out.println("Current Time="+now);
Calendar cal = Calendar.getInstance();
System.out.println("Current Timezone="+cal.getTimeZone().getDisplayName());
//Canada/Central
String timeZoneId = "Canada/Central";
System.out.println("Getting Time in the timezone="+timeZoneId);
System.out.println("Current Time there="+getDateInTimeZone(now,timeZoneId));
}
}
import java.util.*;
public class Timezone {
public static Date getDateInTimeZone(Date currentDate, String timeZoneId)
{
TimeZone tz = TimeZone.getTimeZone(timeZoneId);
Calendar mbCal = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
mbCal.setTimeInMillis(currentDate.getTime());
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, mbCal.get(Calendar.YEAR));
cal.set(Calendar.MONTH, mbCal.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, mbCal.get(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, mbCal.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, mbCal.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, mbCal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, mbCal.get(Calendar.MILLISECOND));
return cal.getTime();
}
public static void main(String[] args)
{
Date now = new Date();
System.out.println("Current Time="+now);
Calendar cal = Calendar.getInstance();
System.out.println("Current Timezone="+cal.getTimeZone().getDisplayName());
//Canada/Central
String timeZoneId = "Canada/Central";
System.out.println("Getting Time in the timezone="+timeZoneId);
System.out.println("Current Time there="+getDateInTimeZone(now,timeZoneId));
}
}

26 comments:
thanks alot man. this code saved me a lot of time
Thanks a lot...really helped me a lot :)
Immediately useable, excellent code
Excellent ... Thank You ...
thanks for this code... it was really helpful..
also saved a lot of time and effort...
thanks again!
>>Getting the date in another timezone is not quite as evident.
I think Date is a wrapper of time difference to 1970 "epoch" UTC, it must be in UTC,never think about "get the date in another timezone", it's a wrong understanding about how Date works in Java. If you want "date in another timezone", I guess what you really want is this:
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
System.out.println(sdf.format(d));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+1"));
System.out.println(sdf.format(d));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+2"));
System.out.println(sdf.format(d));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+3"));
System.out.println(sdf.format(d));
Daniel is right. Date is Date is epoch time, Timezones don't actually alter the date, merely the date presentation.
Great! Great! Great!
[url=http://www.blingforfun.com]hip hop jewelry[/url],[url=http://www.blingforfun.com/pendants/cat_9.html]hip hop pendants[/url],hip hop watches,[url=http://blingforfun.com/belts/cat_18.html]bling bling[/url] ,hip hop,[url=http://blingforfun.com/chains/cat_7.html]hip hop chains[/url],hip hop bling,[url=http://blingforfun.com/chains/cat_7.html]iced out chains[/url],[url=http://www.blingforfun.com/chains/cat_7.html]wholesale chains[/url]
hip hop jewelry
wholesale hip hop watches
hip hop rings
Thanks a lot man. i took lot of time to find this one. Thanks again
this kind of topic is good for people to learn more about it, and that people should be every day less ignorant, and medicine for this is the reading of issues like this
THANKS!!!!!
This line asignment:
TimeZone tz = TimeZone.getTimeZone(timeZoneId);
is unused.
http://www.smartbargainsus.com/main/index.php
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=1
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=22
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=4
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=7
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=8
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=9
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=15
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=16
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=17
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=19
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=20
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=saleItems
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=2
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=21
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=18
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=20
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=16
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=24
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=78
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=63
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=65
http://www.smartbargainsus.com/main/index.php
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=1
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=22
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=4
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=7
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=8
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=9
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=15
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=16
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=17
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=19
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=20
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=saleItems
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=2
http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=21
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=18
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=20
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=16
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=24
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=78
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=63
http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=65
the output for this program:
Current Time=Wed Jun 09 23:19:03 IST 2010
Current Timezone=India Standard Time
Getting Time in the timezone=Canada/Central
Current Time there=Wed Jun 09 12:49:03 IST 2010
Here, the time is change to Canada/central equivalent. but, still it is displaying as IST only. how can i change this too as well
There are some sexy rihanna naked pictures floating around the internet today says TMZ from her new song Love the Way You Lie with her new partner Eminem. There have been rumors floating around that Rihanna will release a brand new album and a new sex tape might leak from her one night stand with an unnamed teen.
rihanna naked pics
He asked Rihanna to perform whatever song that came to mind for her. Rogers was obviously suprised on what great talent Rihanna had. Immediately, both Rogers and Rihanna made demos containing close to twelve tracks including the 2005 smash hit Pon De Replay and mailed the demos out to various recording comanies including Def Jam Recordings where Rihanna would eventually be signed onto.
rihanna naked
auto insurance missouri I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work Look forward to reading more from you in the future. Martin , texas auto insurance
[url=http://infobooksonline.com/shop/Textbooks/]Textbooks[/url]. Algorithm Design, Communications, Database Systems, Financial Accounting, Journalism Books, Nursing Books, The Law of Torts.
Hi Everybody! I'm originally from London but am living in Berlin at the moment.
Gotta like this forum!
____________________________________
[url=http://stephetteh.wordpress.com/]Barrater is my life[/url]
Hi Everybody! I'm from London but am living in Berlin at the moment.
Gotta love this site!
____________________________________
[url=http://stephetteh.wordpress.com/]Barrater is my life[/url]
Cheap Watches, [url=http://cheap-bargain-watches.com/deals/Casio+Mens+Watch/]Casio Mens Watch[/url], [url=http://cheap-bargain-watches.com/deals/TAG+Heuer+Mens+Watch/]TAG Heuer Mens Watch[/url], [url=http://cheap-bargain-watches.com/deals/Cartier+Womens+Watch/]Cartier Womens Watch[/url], [url=http://cheap-bargain-watches.com/deals/Rolex+Womens+Watch/]Rolex Womens Watch[/url], [url=http://cheap-bargain-watches.com/deals/Ed Hardy+Kids+Watch/]Ed Hardy Kids Watch[/url]. Fossil Watch, Casio Watch, Guess Watch, Rolex Watch, Omega Watch, Seiko Watch, Bulova Watch, Tag Heuer Watch, Movado Watch, Gucci Watch and more.
I have a problem with date in OAF coding..
try
{
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateString = am.getOADBTransaction().getCurrentDBDate().toString();
sqlDate = new java.sql.Date(f.parse(newdate).getTime());
OAMessageStyledTextBean dd = (OAMessageStyledTextBean)webBean.findIndexedChildRecursive("DispositionDate");
dd.setValue(pageContext, dateString);
}
This is saving the date to my db column
issue is -
if any changes are done between 12:00 noon to 1:00 PM (say at 12:15:12 PM)
the date is getting inserted as
5/12/2011 12:15:12 AM
instead of
5/12/2011 12:15:12 PM
Why?
Any help please
The beauty of this Tech Initiatives website online is amazed. I like your blog.
--------------------------
Link to my site: No Deposit Poker Bonuses for Poker rooms.
Great job with "Getting Java Date in another Timezone" article! Love your writing style on Tech Initiatives! Funny, but serious and to the point, at the same time! Nice job dear admin.
--------------------------
My site: Bonus Without Deposit 100% free poker !
I appear forward to coming back to read your "Getting Java Date in another Timezone" post also.
--------------------------
Visit my web: Poker bonus sans depot & Poker Bonus 50 & Poker ohne einzahlung & Bonus senza deposito
This is really a fascinating website and all your articles, lots of useful stuff. One thing I just want to say is that your Tech Initiatives Blog is so perfect to me.
--------------------------
Our www: Free Holdem Poker
Post a Comment