The code below is how I do this. It works for summer and winter times
automajically.
public static final Locale G_LOCALE_UK = Locale.UK;
public static final String G_S_TIME_ZONE_ID = "Europe/London";
/**
* Converts a date-time in Universal Time Co-ordinated (UTC) time
* into a formatted string of that date-time translated to UK local
time
* (GMT or BST depending on the time of year).
* @param nDateStyle
* The date formatting style to be applied.
* @param nTimeStyle
* The time formatting style to be applied.
* @param dtDateTime
* A date-time (in UTC time).
* If this is <code>null</code> then an empty string is returned.
* @return
* The date-time translated to UK local time formatted as a
string.
*/
public static String dateTimeUTCToStringLocalTime(int nDateStyle,
int nTimeStyle, Date dtDateTime)
{
String sDate = "";
if (dtDateTime != null)
{
GregorianCalendar gcUK = new GregorianCalendar(G_LOCALE_UK);
TimeZone tzUK = TimeZone.getTimeZone(G_S_TIME_ZONE_ID);
gcUK.setTimeZone(tzUK);
gcUK.setTime(dtDateTime);
DateFormat dfUK =
SimpleDateFormat.getDateTimeInstance(nDateStyle,
nTimeStyle, G_LOCALE_UK);
dfUK.setCalendar(gcUK);
sDate = dfUK.format(dtDateTime);
}
return sDate;
}
// Example usage of the method
String sDT = dateTimeUTCToStringLocalTime(SimpleDateFormat.MEDIUM,
SimpleDateFormat.MEDIUM, new Date());
Just alter the two constants to reflect Germany instead of their
current values which reflect the UK.
Enjoy?
On Jan 11, 3:24 pm, vega <[email protected]> wrote:
> hi everyone.
>
> since the days are getting longer, i noticed that in 2 months ill have an
> error with my getTime() Method, which gives me the current time in a "human
> friendly" way.
>
> /**
> **dd.mm.yyyy hh:mm:ss
> **/
> public static String getTimeAsStringTest(){
> Calendar cal = Calendar.getInstance();
> cal.setTimeZone(TimeZone.getTimeZone("Germany/Berlin"));
>
> int mod = 1; //Wintertime +1, Summertime +2
>
> cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
> cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY)+mod,
> cal.get(Calendar.MINUTE));
> DateFormat df = DateFormat.getDateTimeInstance( MEDIUM, MEDIUM,
> Locale.GERMANY );
> return df.format(cal.getTime());
> }
>
> i dont want to change the mod every 6 months (or how long the daylight
> saving time might be, i dont care^^).
> i would prefer it very much if it would give me the correct time without
> adding 1 or 2 hours to the time the calendar gives me...
>
> how do i archive this, without using something ugly like a list or some 42
> page long if else if statements which knows when to use +1 and when to use
> +2?...
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.