DateTime sample shows usage of DateFormat

This commit is contained in:
konsoletyper 2014-06-27 16:52:03 +04:00
parent 54e0c43fb2
commit 08c912e80f
3 changed files with 84 additions and 107 deletions

View File

@ -148,14 +148,17 @@ class TSimpleDatePatternParser {
elements.add(new TDateFormatElement.Numeric(TCalendar.MILLISECOND, rep)); elements.add(new TDateFormatElement.Numeric(TCalendar.MILLISECOND, rep));
break; break;
} }
default: { default:
if (isControl(c)) {
parseRepetitions();
} else {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
while (index < pattern.length() && !isControl(pattern.charAt(index))) { while (index < pattern.length() && !isControl(pattern.charAt(index))) {
sb.append(pattern.charAt(index++)); sb.append(pattern.charAt(index++));
} }
elements.add(new TDateFormatElement.ConstantText(sb.toString())); elements.add(new TDateFormatElement.ConstantText(sb.toString()));
break;
} }
break;
} }
} }
} }

View File

@ -15,8 +15,8 @@
*/ */
package org.teavm.samples; package org.teavm.samples;
import java.text.DateFormatSymbols; import java.text.DateFormat;
import java.util.Calendar; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Locale; import java.util.Locale;
@ -38,23 +38,34 @@ public class DateTime {
private static Window window = (Window)JS.getGlobal(); private static Window window = (Window)JS.getGlobal();
private static HTMLDocument document = window.getDocument(); private static HTMLDocument document = window.getDocument();
private static HTMLSelectElement localeElem = (HTMLSelectElement)document.getElementById("locale"); private static HTMLSelectElement localeElem = (HTMLSelectElement)document.getElementById("locale");
private static HTMLSelectElement fieldElem = (HTMLSelectElement)document.getElementById("field"); private static HTMLSelectElement formatElem = (HTMLSelectElement)document.getElementById("format");
private static HTMLSelectElement customFormatElem = (HTMLSelectElement)document.getElementById("custom-format");
private static Date currentDate = new Date(); private static Date currentDate = new Date();
private static Locale[] locales; private static Locale[] locales;
private static Locale currentLocale; private static Locale currentLocale;
private static int currentField; private static DateFormat dateFormat;
public static void main(String[] args) { public static void main(String[] args) {
fillLocales(); fillLocales();
bindFieldEvent();
window.setInterval(new TimerHandler() { window.setInterval(new TimerHandler() {
@Override @Override public void onTimer() {
public void onTimer() {
updateCurrentTime(); updateCurrentTime();
} }
}, 250); }, 250);
updateCurrentLocale(); updateCurrentLocale();
updateCurrentField(); updateFormat();
formatElem.addEventListener("change", new EventListener() {
@Override public void handleEvent(Event evt) {
updateFormat();
updateCurrentTimeText();
}
});
customFormatElem.addEventListener("change", new EventListener() {
@Override public void handleEvent(Event evt) {
updateFormat();
updateCurrentTimeText();
}
});
} }
private static void fillLocales() { private static void fillLocales() {
@ -68,19 +79,12 @@ public class DateTime {
localeElem.addEventListener("change", new EventListener() { localeElem.addEventListener("change", new EventListener() {
@Override public void handleEvent(Event evt) { @Override public void handleEvent(Event evt) {
updateCurrentLocale(); updateCurrentLocale();
updateFormat();
updateCurrentTimeText(); updateCurrentTimeText();
} }
}); });
} }
private static void bindFieldEvent() {
fieldElem.addEventListener("change", new EventListener() {
@Override public void handleEvent(Event evt) {
updateCurrentField();
}
});
}
private static void updateCurrentLocale() { private static void updateCurrentLocale() {
currentLocale = locales[localeElem.getSelectedIndex()]; currentLocale = locales[localeElem.getSelectedIndex()];
GregorianCalendar calendar = new GregorianCalendar(currentLocale); GregorianCalendar calendar = new GregorianCalendar(currentLocale);
@ -97,89 +101,64 @@ public class DateTime {
private static void setCurrentTime(Date date) { private static void setCurrentTime(Date date) {
currentDate = date; currentDate = date;
updateCurrentTimeText(); updateCurrentTimeText();
updateFieldText();
} }
private static void updateCurrentTimeText() { private static void updateCurrentTimeText() {
HTMLInputElement timeElem = (HTMLInputElement)document.getElementById("current-time"); HTMLInputElement timeElem = (HTMLInputElement)document.getElementById("current-time");
timeElem.setValue(currentDate.toString()); try {
timeElem.setValue(dateFormat.format(currentDate));
} catch (RuntimeException e) {
timeElem.setValue("Error formatting date");
}
} }
private static void updateCurrentField() { private static void updateFormat() {
switch (fieldElem.getValue()) { customFormatElem.setDisabled(true);
case "era": switch (formatElem.getValue()) {
currentField = Calendar.ERA; case "short-date":
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, currentLocale);
break; break;
case "year": case "medium-date":
currentField = Calendar.YEAR; dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, currentLocale);
break; break;
case "month": case "long-date":
currentField = Calendar.MONTH; dateFormat = DateFormat.getDateInstance(DateFormat.LONG, currentLocale);
break; break;
case "week-of-year": case "full-date":
currentField = Calendar.WEEK_OF_YEAR; dateFormat = DateFormat.getDateInstance(DateFormat.FULL, currentLocale);
break; break;
case "week-of-month": case "short-time":
currentField = Calendar.WEEK_OF_MONTH; dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
break; break;
case "date": case "medium-time":
currentField = Calendar.DATE; dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, currentLocale);
break; break;
case "day-of-year": case "long-time":
currentField = Calendar.DAY_OF_YEAR; dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, currentLocale);
break; break;
case "day-of-week": case "full-time":
currentField = Calendar.DAY_OF_WEEK; dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, currentLocale);
break; break;
case "am-pm": case "short-datetime":
currentField = Calendar.AM_PM; dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, currentLocale);
break; break;
case "hour": case "medium-datetime":
currentField = Calendar.HOUR; dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, currentLocale);
break; break;
case "hour-of-day": case "long-datetime":
currentField = Calendar.HOUR_OF_DAY; dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, currentLocale);
break; break;
case "minute": case "full-datetime":
currentField = Calendar.MINUTE; dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, currentLocale);
break; break;
case "second": case "custom":
currentField = Calendar.SECOND; customFormatElem.setDisabled(false);
break; try {
case "zone-offset": dateFormat = new SimpleDateFormat(customFormatElem.getValue(), currentLocale);
currentField = Calendar.ZONE_OFFSET; } catch (IllegalArgumentException e) {
dateFormat = new SimpleDateFormat("'Invalid pattern'");
}
break; break;
} }
updateFieldText();
}
private static void updateFieldText() {
HTMLInputElement fieldValueElem = (HTMLInputElement)document.getElementById("field-value");
Calendar calendar = new GregorianCalendar(currentLocale);
calendar.setTime(currentDate);
int value = calendar.get(currentField);
fieldValueElem.setValue(String.valueOf(value));
DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);
String text;
switch (currentField) {
case Calendar.ERA:
text = symbols.getEras()[value];
break;
case Calendar.AM_PM:
text = symbols.getAmPmStrings()[value];
break;
case Calendar.MONTH:
text = symbols.getMonths()[value] + "/" + symbols.getShortMonths()[value];
break;
case Calendar.DAY_OF_WEEK:
text = symbols.getWeekdays()[value - 1] + "/" + symbols.getShortWeekdays()[value - 1];
break;
default:
text = "";
break;
}
HTMLInputElement fieldTextElem = (HTMLInputElement)document.getElementById("field-value-text");
fieldTextElem.setValue(text);
} }
} }

View File

@ -22,28 +22,23 @@
<input type="text" id="week-length" readonly size="5"> <input type="text" id="week-length" readonly size="5">
</div> </div>
<div> <div>
<label for="field">Pick a field:</label> <label for="format">Format:</label>
<select id="field" size="1"> <select id="format" size="1">
<option value="era">Era</option> <option value="short-date">Date (short)</option>
<option value="year">Year</option> <option value="medium-date">Date (medium)</option>
<option value="month">Month</option> <option value="long-date">Date (long)</option>
<option value="week-of-year">Week of year</option> <option value="full-date">Date (full)</option>
<option value="week-of-month">Week of month</option> <option value="short-time">Time (short)</option>
<option value="date">Date</option> <option value="medium-time">Time (medium)</option>
<option value="day-of-year">Day of year</option> <option value="long-time">Time (long)</option>
<option value="day-of-week">Day of week</option> <option value="full-time">Time (full)</option>
<option value="am-pm">AM/PM</option> <option value="short-datetime">Date and time (short)</option>
<option value="hour">Hour</option> <option value="medium-datetime">Date and (medium)</option>
<option value="hour-of-day">Hour of day</option> <option value="long-datetime">Date and time (long)</option>
<option value="minute">Minute</option> <option value="full-datetime">Date and time (full)</option>
<option value="second">Second</option> <option value="custom">Custom</option>
<option value="zone-offset">Zone offset</option>
</select> </select>
</div> <input type="text" id="custom-format" disabled value="yyyy-MM-dd HH:mm:ss">
<div>
<label for="field-value">Field value is:</label>
<input type="text" id="field-value" readonly>
<input type="text" id="field-value-text" readonly>
</div> </div>
</body> </body>
</html> </html>