Reputation: 197
The JodaTime DateTimeZone
class provides the getAvailableIDs
method to list the timezone identifiers supplied by the Java platform. Is there a plain HTML select tag and options listing the compatible timezone identifiers?
Upvotes: 0
Views: 35
Reputation: 197
The resulting HTML is too large to post here, but the following utility generates the desired HTML select and option tags...
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.joda.time.DateTimeZone;
public class DateTimeUtils {
/**
* Prevents the construction of instances of this utility class which has only static methods.
*/
private DateTimeUtils() {
}
/**
* Displays the timezone identifiers provided by the system.
*/
public static void listAvailableIDs() {
final Set<String> availableIDs = DateTimeZone.getAvailableIDs();
final List<String> sortedAvailableIDs = new ArrayList<>();
sortedAvailableIDs.addAll(availableIDs);
Collections.sort(sortedAvailableIDs);
for (final String sortedAvailableID : sortedAvailableIDs) {
System.out.println(sortedAvailableID);
}
}
/**
* Generates an HTML select element for the timezone identifiers accepted by Joda DateTime on Java 15.
*/
public static void generateHTMLTimezoneSelect() {
final Set<String> availableIDs = DateTimeZone.getAvailableIDs();
final List<String> sortedAvailableIDs = new ArrayList<>();
sortedAvailableIDs.addAll(availableIDs);
Collections.sort(sortedAvailableIDs);
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder
.append("<label for=\"timezone\">Select your timezone:</label>\n")
.append("<select name=\"timezone\">\n");
for (final String sortedAvailableID : sortedAvailableIDs) {
stringBuilder
.append(" <option value=\"")
.append(sortedAvailableID)
.append("\">")
.append(sortedAvailableID)
.append("</option>\n");
}
stringBuilder.append("</select>\n");
System.out.println(stringBuilder.toString());
}
/**
* Generates an initialized String array for the timezone identifiers accepted by Joda DateTime on Java 15.
*/
public static void generateTimezoneIDArray() {
final Set<String> availableIDs = DateTimeZone.getAvailableIDs();
final List<String> sortedAvailableIDs = new ArrayList<>();
sortedAvailableIDs.addAll(availableIDs);
Collections.sort(sortedAvailableIDs);
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder
.append(" private static final String[] TIMEZONE_IDS = {\n");
for (final String sortedAvailableID : sortedAvailableIDs) {
stringBuilder
.append(" \"")
.append(sortedAvailableID)
.append("\",\n");
}
stringBuilder.setLength(stringBuilder.length() - 2);
stringBuilder.append("\n };\n");
System.out.println(stringBuilder.toString());
}
}
Upvotes: 0
Reputation: 9945
There is no such tag in the HTML standard. You must either generate the list in the backend or using one of the available frontend libraries (eg. https://momentjs.com/timezone/). Generating in the backend gives you a guarantee that the identifiers are compatible.
Upvotes: 1