Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
Need an offset based on TimeZone given
clakare bright
Greenhorn
Posts: 5
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public static void getUTCDateFromLocale(Date dt,String tzId){ TimeZone tz = TimeZone.getTimeZone(tzId); //int raw = tz.getOffset(dt.getTime()); int off = tz.getRawOffset(); //tz.setRawOffset(raw); //Calendar calTZ = Calendar.getInstance(TimeZone.getTimeZone(tzId)); Calendar calTZ = Calendar.getInstance(); calTZ.setTimeInMillis(dt.getTime()); calTZ.set(Calendar.ZONE_OFFSET, off); System.out.println("calTZ :"+calTZ.getTime()); String utcDate = DateFormatUtils.formatUTC(calTZ.getTime(), patternYYYY); System.out.println("utcDate : " +utcDate); } public static Date getUTCDate(){ try { String utcDateStr = DateFormatUtils.formatUTC(new Date(), patternYYYY); System.out.println("utcDateStr from server : "+utcDateStr); SimpleDateFormat sdf = new SimpleDateFormat(patternYYYY); Date utcDt = sdf.parse(utcDateStr); return utcDt; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static Date getUTCDateForTimeZone(Date utcDt,String tzId){ try { String utcDateForTz = DateFormatUtils.format(utcDt, patternYYYY,TimeZone.getTimeZone(tzId)); //System.out.println("utcDate from server for TZ : "+utcDateForTz); SimpleDateFormat sdf = new SimpleDateFormat(patternYYYY); Date utcDtTz = sdf.parse(utcDateForTz); return utcDtTz; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static Date getUTCDateForTimeZoneBeforeoneMonth(Date utcDt,String tzId){ Date dt = getUTCDateForTimeZone(utcDt,tzId); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(dt.getTime()); cal.add(Calendar.MONTH, -1); cal.add(Calendar.DATE, 1); return cal.getTime(); } String timeZoneId = "Asia/Tokyo"; //String timeZoneId = "Asia/Calcutta"; //String timeZoneId = "Asia/Amman"; //String timeZoneId = "Asia/Taipei"; //String timeZoneId = "Canada/Central"; //String timeZoneId = "America/Tegucigalpa"; //String timeZoneId = "Pacific/Guam"; Calendar calLocal = Calendar.getInstance(); //Asia/Amman //calLocal.set(2009, 06, 25, 11, 11, 00); //Asia/Calcutta //calLocal.set(2009, 06, 25, 12, 30, 00); //America/Tegucigalpa //calLocal.set(2009, 06, 25, 2, 18, 00); //Pacific/Guam //calLocal.set(2009, 06, 25, 18, 24, 00); //Asia/Tokyo; //calLocal.set(2009, 06, 25, 9, 00, 00); System.out.println(calLocal.getTime()); getUTCDateFromLocale(calLocal.getTime(),timeZoneId);
guyjava30-timezone
guyjava30-timezone
Jason Irwin
Ranch Hand
Posts: 327
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Please use
code tags
with proper indentation, it will make your code
much
easier to read.
And please explain the problem you are having.
SCJP6
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Need Help on Time conversion.
timezone woes
How to subtract the date value
java.util.Date for any year
Problem with Calendar().getTime()
More...