Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Sorting TimeZone

Reply
Thread Tools

Sorting TimeZone

 
 
Wojtek
Guest
Posts: n/a
 
      11-03-2009
How would you sort timezones?

I am trying to sort them according to their offset from UTC. I cannot
use a TreeMap because there are many timezones with the same offset,
which of course over-writes the previously put timezone.

The preferred sort would be offset, then display name.

--
Wojtek


 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      11-03-2009
Wojtek wrote:
> How would you sort timezones?
>
> I am trying to sort them according to their offset from UTC. I cannot
> use a TreeMap because there are many timezones with the same offset,
> which of course over-writes the previously put timezone.
>
> The preferred sort would be offset, then display name.
>



I guess put them all in an array, the use Array.sort( Object[],
Comparator<T>) to sort them as you desire.

 
Reply With Quote
 
 
 
 
Wojtek
Guest
Posts: n/a
 
      11-03-2009
markspace wrote :
> Wojtek wrote:
>> How would you sort timezones?
>>
>> I am trying to sort them according to their offset from UTC. I cannot use a
>> TreeMap because there are many timezones with the same offset, which of
>> course over-writes the previously put timezone.
>>
>> The preferred sort would be offset, then display name.
>>

>
>
> I guess put them all in an array, the use Array.sort( Object[],
> Comparator<T>) to sort them as you desire.


Yes, I made my own custom Comparator.

But I just found out what the problem is. You can retrieve a TimeZone
using a variety of names, but that TimeZone only has one display name.
For instance, in Canada:

Canada/Newfoundland Newfoundland Standard Time
Canada/Atlantic Atlantic Standard Time
Canada Eastern Eastern Standard Time
Canada/Central Central Standard Time *
Canada/East-Saskatchewan Central Standard Time *
Canada/Saskatchewan Central Standard Time *
Canada/Mountain Mountain Standard Time
Canada/Pacific Pacific Standard Time *
Canada/Yukon Pacific Standard Time *

I retrieve them using the left names, but the .getDisplayName() returns
the right side. Which throws the sorting out.

Ok, I can make this work. Funny how actually _asking_ a question can
lead to an answer...

--
Wojtek


 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-03-2009
Wojtek wrote:
> How would you sort timezones?
>
> I am trying to sort them according to their offset from UTC. I cannot
> use a TreeMap because there are many timezones with the same offset,
> which of course over-writes the previously put timezone.
>
> The preferred sort would be offset, then display name.
>



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package sorttimezone;

import java.util.Arrays;
import java.util.Comparator;
import java.util.TimeZone;

/**
*
* @author Brenden
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String [] zonesIDs = TimeZone.getAvailableIDs();
TimeZone [] allTZ = new TimeZone[zonesIDs.length];
for( int i=0; i< allTZ.length; i++ ) {
allTZ[i]=TimeZone.getTimeZone( zonesIDs[i] );
}
Arrays.sort( allTZ, new SortByOffsetAndName() );

for( TimeZone zone : allTZ ) {
System.out.println( zone.getDisplayName() + ": "+
zone.getRawOffset()/1000/60/60 );
}
}
}

class SortByOffsetAndName implements Comparator<TimeZone> {

public int compare( TimeZone o1, TimeZone o2 )
{
if( o1.getRawOffset() != o2.getRawOffset() ) {
return o1.getRawOffset() - o2.getRawOffset();
}
return o1.getDisplayName().compareTo( o2.getDisplayName() );
}

}
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-03-2009
Wojtek wrote:

> I retrieve them using the left names, but the .getDisplayName() returns
> the right side. Which throws the sorting out.
>


Instead of using getDisplayName() (which was the same thing I did, in my
example I posted), use getID(), that returns the string on the left in
your table.

public int compare( TimeZone o1, TimeZone o2 )
{
if( o1.getRawOffset() != o2.getRawOffset() ) {
return o1.getRawOffset() - o2.getRawOffset();
}
return o1.getID().compareTo( o2.getID() );
}


> Ok, I can make this work. Funny how actually _asking_ a question can
> lead to an answer...



It works that way for pretty much everyone.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-03-2009
Wojtek wrote:
> How would you sort timezones?


Do you mean 'java.util.TimeZone'?

> I am trying to sort them according to their offset from UTC. I cannot
> use a TreeMap because there are many timezones with the same offset,
> which of course over-writes the previously put timezone.


There's no "of course" about that if you are using your "preferred sort".

Why would you use any kind of 'Map' rather than a 'Collection'?

> The preferred sort would be offset, then display name.


'TimeZone' can easily be a map key, yes, even for a 'TreeMap'. I fail to see
the problem. You simply compare based on your "preferred sort".

I'd be more concerned about relocation of a key when the offset changes for
DST. But perhaps you're only building this structure for a given moment in
time and that isn't a concern for you.

Regardless, here's uncompiled, off-the-cuff code, using a 'Set' instead of a
'Map':

public class SortedZone
{
private long ref = System.currentTimeMillis();

private final TreeSet <TimeZone> zones =
new TreeSet <TimeZone>
( new Comparator <TimeZone> ()
{
@Override public int compare( TimeZone t0, TimeZone t1 )
{
return
(t0 == null? (t1 == null? 0: -1)
: t1 == null? 1
: t0.getOffset( ref ) > t1.getOffset( ref )? 1
: t0.getOffset( ref ) < t1.getOffset( ref )? -1
: t0.getDisplayName().compareTo( t1.getDisplayName() )
);
}
}
);

// cover methods for the Set ...
}

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-03-2009
On Mon, 02 Nov 2009 16:09:42 -0800, Wojtek <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>
>I am trying to sort them according to their offset from UTC. I cannot
>use a TreeMap because there are many timezones with the same offset,
>which of course over-writes the previously put timezone.
>
>The preferred sort would be offset, then display name.


I would create my own TimeZone Object with the fields I need then cook
up a Comparable/Comparator for it.

see http://mindprod.com/applet/comparatorcutter.html
to generate the code.
--
Roedy Green Canadian Mind Products
http://mindprod.com

An example (complete and annotated) is worth 1000 lines of BNF.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-03-2009
Wojtek quoted or indirectly quoted someone who said :
>> I am trying to sort them according to their offset from UTC. I cannot
>> use a TreeMap because there are many timezones with the same offset,
>> which of course over-writes the previously put timezone.
>>
>> The preferred sort would be offset, then display name.


Roedy Green wrote:
> I would create my own TimeZone Object with the fields I need then cook
> up a Comparable/Comparator for it.


Wojtek was speaking of java.util.TimeZone, judging by the fields he mentioned.

--
Lew
 
Reply With Quote
 
Wojtek
Guest
Posts: n/a
 
      11-03-2009
Lew wrote :
> 'TimeZone' can easily be a map key, yes, even for a 'TreeMap'.


No it cannot be used as a key. It does not have a 'compareTo' method.

--
Wojtek


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-03-2009
Lew wrote :
>> 'TimeZone' can easily be a map key, yes, even for a 'TreeMap'.

>


Wojtek wrote:
> No it cannot be used as a key. It does not have a 'compareTo' method.
>


Yes, it can be used as a key!
<http://java.sun.com/javase/6/docs/ap...p.html#TreeMap
(java.util.Comparator)>

The Javadocs are your friend.

--
Lew


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
TimeZone change - how to use TimeZone class axrock Java 8 03-25-2009 11:54 PM
TimeZone calculation on Windows Vista with DateTime::TimeZone Ami Perl Misc 5 09-24-2007 07:27 AM
fired event Sorting which wasn't handled - sorting and SelectedIndexChanged Jason ASP .Net Web Controls 0 10-04-2006 02:19 PM
Convert windows TimeZone to Java TimeZone asaf Java 3 09-11-2006 05:40 PM



Advertisments