Added a utility class for common operations needed to create KML documents
Updownquark
13 years ago
0 | /* | |
1 | * KmlUtils.java Created Oct 14, 2010 by Andrew Butler, PSL | |
2 | */ | |
3 | package org.boehn.kmlframework.utils; | |
4 | ||
5 | /** Contains utility methods useful for creating KML documents */ | |
6 | public class KmlUtils | |
7 | { | |
8 | private static final java.text.SimpleDateFormat GOOGLE_TIME_FORMAT; | |
9 | ||
10 | static | |
11 | { | |
12 | GOOGLE_TIME_FORMAT = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | |
13 | GOOGLE_TIME_FORMAT.setTimeZone(java.util.TimeZone.getTimeZone("GMT")); | |
14 | } | |
15 | ||
16 | /** | |
17 | * Writes a time to correct KML format | |
18 | * | |
19 | * @param time The time (in milliseconds from the epoch) to convert | |
20 | * @return The KML-formatted time | |
21 | */ | |
22 | public static String formatTime(long time) | |
23 | { | |
24 | return GOOGLE_TIME_FORMAT.format(new java.util.Date(time)); | |
25 | } | |
26 | ||
27 | private static final String HEX = "0123456789abcdef"; | |
28 | ||
29 | /** | |
30 | * Writes a color to correct KML format | |
31 | * | |
32 | * @param color The color to convert | |
33 | * @return The KML-formatted color | |
34 | */ | |
35 | public static String formatColor(java.awt.Color color) | |
36 | { | |
37 | StringBuilder ret = new StringBuilder(); | |
38 | int a = color.getAlpha(); | |
39 | ret.append(HEX.charAt(a / 16)); | |
40 | ret.append(HEX.charAt(a % 16)); | |
41 | int b = color.getBlue(); | |
42 | ret.append(HEX.charAt(b / 16)); | |
43 | ret.append(HEX.charAt(b % 16)); | |
44 | int g = color.getGreen(); | |
45 | ret.append(HEX.charAt(g / 16)); | |
46 | ret.append(HEX.charAt(g % 16)); | |
47 | int r = color.getRed(); | |
48 | ret.append(HEX.charAt(r / 16)); | |
49 | ret.append(HEX.charAt(r % 16)); | |
50 | return ret.toString(); | |
51 | } | |
52 | } |