File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Need an offset based on TimeZone given
clakare bright
Greenhorn
Joined: Jul 23, 2008
Posts: 5
posted
Jul 25, 2009 03:25:21
0
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
Joined: Jun 09, 2009
Posts: 327
posted
Jul 25, 2009 05:03:02
0
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
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: Need an offset based on TimeZone given
Similar Threads
java.util.Date for any year
timezone woes
How to subtract the date value
Need Help on Time conversion.
Problem with Calendar().getTime()
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter