Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MessageFormat and Timezone

880 views
Skip to first unread message

Fred Gurkov

unread,
Jun 14, 2000, 3:00:00 AM6/14/00
to
Does anybody know how to set the time zone for a MessageFormat or even
better for a specific date field in it?

regards,
Fred.


Grzegorz Pszona

unread,
Jun 14, 2000, 3:00:00 AM6/14/00
to
You should use DateFormat class.

or
You can prepare string before sets it in MessageFormat.
You can use Calendar class

for example:

( ... )
TimeZone tz = TimeZone.getTimeZone("GMT"); // for Greenwich
Time Zone
Calendar mCal = Calendar.getInstance( tz );

Date mDate = new Date(100, 06, 14, 10, 30, 0);
mCal.setTime(mDate); // deprecated!
// or
mCal.set( 100, 06, 14, 10, 30, 0);

String sDate = mCal.get(Calendar.HOUR) + ":" +
mCal.get(Calendar.MINUTE) + ":" +
mCal.get(Calendar.SECOND);

MessageFormat form = new MessageFormat( sDate );
( ... )

regards,
GP

"Fred Gurkov" <fr...@sunbay.com> wrote in message
news:39473271...@sunbay.com...

Fred Gurkov

unread,
Jun 14, 2000, 3:00:00 AM6/14/00
to
Thanks. That's clear but my intention was to use uniform format and store it
in a properties file.

Paul Hill

unread,
Jun 14, 2000, 3:00:00 AM6/14/00
to

Grzegorz Pszona wrote:
>
> You should use DateFormat class.
>
> or
> You can prepare string before sets it in MessageFormat.
> You can use Calendar class
>
> for example:
>
> ( ... )
> TimeZone tz = TimeZone.getTimeZone("GMT"); // for Greenwich
> Time Zone
> Calendar mCal = Calendar.getInstance( tz );

If you want this calendar to run on GMT time you must (instead of your local default).
mCal.setTimezone( tz );

>
> Date mDate = new Date(100, 06, 14, 10, 30, 0);
> mCal.setTime(mDate); // deprecated!

Calendar.setTime is not deprecated, just new Date(...) with many arguments.

> // or
> mCal.set( 100, 06, 14, 10, 30, 0);
>
> String sDate = mCal.get(Calendar.HOUR) + ":" +
> mCal.get(Calendar.MINUTE) + ":" +
> mCal.get(Calendar.SECOND);
>
> MessageFormat form = new MessageFormat( sDate );
> ( ... )

A SimpleDateFormat might come in handy here, but the original poster was
looking to use a MessageFormat.

To set the format for a date field I believe you need to do the following.
1. Construct a SimpleDateFormat with the TZ set correctly, you might use information
from the default format (DateFormat.getDateInstance())and set the TZ on that.
2. Use the MessageFormat.setFormat( index, format ) method to attach your specially
created format into a particular _field_ in the particular Message.

I don't see a way make all dates in a message change to a given TZ.

Hope that helps,
-Paul

If I had time I'd code this

>
> regards,
> GP
>
> "Fred Gurkov" <fr...@sunbay.com> wrote in message
> news:39473271...@sunbay.com...
> > Does anybody know how to set the time zone for a MessageFormat or even
> > better for a specific date field in it?
> >
> > regards,
> > Fred.
> >

--
Myriad Genetics: http://www.myriad.com/
Java FAQ: http://www.afu.com/javafaq.html (Section 9, Computer Dating)

Fred Gurkov

unread,
Jun 15, 2000, 3:00:00 AM6/15/00
to ph...@myriad.com
Thanks a lot Paul, you have inspired me to write quite acceptable code. I haven't paid
attention to the getFormats method of a MessageFormat. Here's an example:

import java.text.*;
import java.util.*;


public class Format {
public static void main(String argv[]) {
MessageFormat mf = new MessageFormat("The time is: {0, time, HH:mm}");


TimeZone tz = TimeZone.getTimeZone("GMT");

Object [] formats = mf.getFormats();
for (int i = 0; i < formats.length; i++) {
if (formats[i] instanceof SimpleDateFormat) {
((SimpleDateFormat)formats[i]).setTimeZone(tz);
}
}
Date date = new Date();
Object [] args = {date};
System.out.println(mf.format(args));
}
}

Best regards,
Fred.

Paul Hill

unread,
Jun 15, 2000, 3:00:00 AM6/15/00
to Fred Gurkov

Fred Gurkov wrote:
>
> Thanks a lot Paul, you have inspired me to write quite acceptable code. I haven't paid
> attention to the getFormats method of a MessageFormat. Here's an example:

> for (int i = 0; i < formats.length; i++) {
> if (formats[i] instanceof SimpleDateFormat) {
> ((SimpleDateFormat)formats[i]).setTimeZone(tz);
> }
> }

Bravo! Obviously you can just go looking for the right objects in the
list of formats and tweak them. What someone might do is make a
TimeZoneMessageFormat() that contains a setTimeZone( TimeZone tz )

which I just did. I've run it and it seems to work.

import java.util.*;
import java.text.*;

public class TZMessageFormat extends MessageFormat {
public TZMessageFormat( String newPattern ) {
super( newPattern );
}

private TimeZone _tz; // for all dates/times in this message
public void setTimeZone( TimeZone tz ) {
_tz = tz;
applyTimeZone();
}

private void applyTimeZone() {
if ( _tz == null ) return;
// force the specific timezone on any date/times in the message
Object [] formats = getFormats();


for (int i = 0; i < formats.length; i++) {
if (formats[i] instanceof SimpleDateFormat) {

((SimpleDateFormat)formats[i]).setTimeZone(_tz);
}
}
}

public void applyPattern( String newPattern ) {
super.applyPattern( newPattern );
applyTimeZone();
}

public static void main(String[] args) {
TZMessageFormat mf = new TZMessageFormat( "The time is: {0, time, HH:mm}");
mf.setTimeZone( TimeZone.getTimeZone("GMT") );

Date date = new Date();

Object [] args1 = {date};
System.out.println(mf.format(args1));
// or as one line as
System.out.println( mf.format( new Object[] { new Date() } ) );

0 new messages