List of usage examples for com.google.gwt.i18n.shared TimeZone getOffset
int getOffset(Date date);
From source file:com.rnb.plategka.shared.DateTimeFormat.java
License:Apache License
/** * Format a date object using specified time zone. * /* www .ja v a2 s. c om*/ * @param date * the date object being formatted * @param timeZone * a TimeZone object that holds time zone information, or * {@code null} to use the default * * @return string representation for this date in the format defined by this * object */ @SuppressWarnings("deprecation") public String format(Date date, TimeZone timeZone) { // We use the Date class to calculate each date/time field in order // to maximize performance and minimize code size. // JavaScript only provides an API for rendering local time (in the os time // zone). Here we want to render time in any timezone. So suppose we try to // render the date (20:00 GMT0000, or 16:00 GMT-0400, or 12:00 GMT-0800) for // time zone GMT-0400, and OS has time zone GMT-0800. By adding the // difference between OS time zone (GMT-0800) and target time zone // (GMT-0400) to "date", we end up with 16:00 GMT-0800. This date object // has the same date/time fields (year, month, date, hour, minutes, etc) // in GMT-0800 as original date in our target time zone (GMT-0400). We // just need to take care of time zone display, but that's needed anyway. // Things get a little bit more tricky when a daylight time transition // happens. For example, if the OS timezone is America/Los_Angeles, // it is just impossible to have a Date represent 2006/4/2 02:30, because // 2:00 to 3:00 on that day does not exist in US Pacific time zone because // of the daylight time switch. // But we can use 2 separate date objects, one to represent 2006/4/2, one // to represent 02:30. Of course, for the 2nd date object its date can be // any other day in that year, except 2006/4/2. So we end up have 3 Date // objects: one for resolving "Year, month, day", one for time within that // day, and the original date object, which is needed for figuring out // actual time zone offset. if (timeZone == null) { timeZone = createTimeZone(date.getTimezoneOffset()); } int diff = (date.getTimezoneOffset() - timeZone.getOffset(date)) * 60000; Date keepDate = new Date(date.getTime() + diff); Date keepTime = keepDate; if (keepDate.getTimezoneOffset() != date.getTimezoneOffset()) { if (diff > 0) { diff -= NUM_MILLISECONDS_IN_DAY; } else { diff += NUM_MILLISECONDS_IN_DAY; } keepTime = new Date(date.getTime() + diff); } StringBuffer toAppendTo = new StringBuffer(64); int j, n = pattern.length(); for (int i = 0; i < n;) { char ch = pattern.charAt(i); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { // ch is a date-time pattern character to be interpreted by subFormat(). // Count the number of times it is repeated. for (j = i + 1; j < n && pattern.charAt(j) == ch; ++j) { } subFormat(toAppendTo, ch, j - i, date, keepDate, keepTime, timeZone); i = j; } else if (ch == '\'') { // Handle an entire quoted string, included embedded // doubled apostrophes (as in 'o''clock'). // i points after '. ++i; // If start with '', just add ' and continue. if (i < n && pattern.charAt(i) == '\'') { toAppendTo.append('\''); ++i; continue; } // Otherwise add the quoted string. boolean trailQuote = false; while (!trailQuote) { // j points to next ' or EOS. j = i; while (j < n && pattern.charAt(j) != '\'') { ++j; } if (j >= n) { // Trailing ' (pathological). throw new IllegalArgumentException("Missing trailing \'"); } // Look ahead to detect '' within quotes. if (j + 1 < n && pattern.charAt(j + 1) == '\'') { ++j; } else { trailQuote = true; } toAppendTo.append(pattern.substring(i, j)); i = j + 1; } } else { // Append unquoted literal characters. toAppendTo.append(ch); ++i; } } return toAppendTo.toString(); }
From source file:org.ssgwt.share.i18n.DateTimeFormat.java
License:Apache License
/** * Format a date object using specified time zone. * * @param date the date object being formatted * @param timeZone a TimeZone object that holds time zone information, or * {@code null} to use the default// w w w . j av a2 s . com * * @return string representation for this date in the format defined by this * object */ @SuppressWarnings("deprecation") public String format(SSDate date, TimeZone timeZone) { // We use the Date class to calculate each date/time field in order // to maximize performance and minimize code size. // JavaScript only provides an API for rendering local time (in the os time // zone). Here we want to render time in any timezone. So suppose we try to // render the date (20:00 GMT0000, or 16:00 GMT-0400, or 12:00 GMT-0800) for // time zone GMT-0400, and OS has time zone GMT-0800. By adding the // difference between OS time zone (GMT-0800) and target time zone // (GMT-0400) to "date", we end up with 16:00 GMT-0800. This date object // has the same date/time fields (year, month, date, hour, minutes, etc) // in GMT-0800 as original date in our target time zone (GMT-0400). We // just need to take care of time zone display, but that's needed anyway. // Things get a little bit more tricky when a daylight time transition // happens. For example, if the OS timezone is America/Los_Angeles, // it is just impossible to have a Date represent 2006/4/2 02:30, because // 2:00 to 3:00 on that day does not exist in US Pacific time zone because // of the daylight time switch. // But we can use 2 separate date objects, one to represent 2006/4/2, one // to represent 02:30. Of course, for the 2nd date object its date can be // any other day in that year, except 2006/4/2. So we end up have 3 Date // objects: one for resolving "Year, month, day", one for time within that // day, and the original date object, which is needed for figuring out // actual time zone offset. if (timeZone == null) { timeZone = createTimeZone(date.getTimezoneOffset()); } int diff = (date.getTimezoneOffset() - timeZone.getOffset(date)) * 60000; SSDate keepDate = new SSDate(date.getTime() + diff); SSDate keepTime = keepDate; if (keepDate.getTimezoneOffset() != date.getTimezoneOffset()) { if (diff > 0) { diff -= NUM_MILLISECONDS_IN_DAY; } else { diff += NUM_MILLISECONDS_IN_DAY; } keepTime = new SSDate(date.getTime() + diff); } StringBuffer toAppendTo = new StringBuffer(64); int j, n = pattern.length(); for (int i = 0; i < n;) { char ch = pattern.charAt(i); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { // ch is a date-time pattern character to be interpreted by subFormat(). // Count the number of times it is repeated. for (j = i + 1; j < n && pattern.charAt(j) == ch; ++j) { } subFormat(toAppendTo, ch, j - i, date, keepDate, keepTime, timeZone); i = j; } else if (ch == '\'') { // Handle an entire quoted string, included embedded // doubled apostrophes (as in 'o''clock'). // i points after '. ++i; // If start with '', just add ' and continue. if (i < n && pattern.charAt(i) == '\'') { toAppendTo.append('\''); ++i; continue; } // Otherwise add the quoted string. boolean trailQuote = false; while (!trailQuote) { // j points to next ' or EOS. j = i; while (j < n && pattern.charAt(j) != '\'') { ++j; } if (j >= n) { // Trailing ' (pathological). throw new IllegalArgumentException("Missing trailing \'"); } // Look ahead to detect '' within quotes. if (j + 1 < n && pattern.charAt(j + 1) == '\'') { ++j; } else { trailQuote = true; } toAppendTo.append(pattern.substring(i, j)); i = j + 1; } } else { // Append unquoted literal characters. toAppendTo.append(ch); ++i; } } return toAppendTo.toString(); }