mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Adds test form SimpleDateFormat. Adds tests for DateFormat
This commit is contained in:
parent
9c8d137e90
commit
52ed32e9aa
|
@ -112,24 +112,28 @@ public class CLDRHelper {
|
||||||
return resolveFormatSymbols(getDateFormatMap(), language, country);
|
return resolveFormatSymbols(getDateFormatMap(), language, country);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MetadataProvider(DateFormatMetadataGenerator.class)
|
||||||
private static native ResourceMap<StringResource> getDateFormatMap();
|
private static native ResourceMap<StringResource> getDateFormatMap();
|
||||||
|
|
||||||
public static String resolveFullDateFormat(String language, String country) {
|
public static String resolveFullDateFormat(String language, String country) {
|
||||||
return resolveFormatSymbols(getFullDateFormatMap(), language, country);
|
return resolveFormatSymbols(getFullDateFormatMap(), language, country);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MetadataProvider(DateFormatMetadataGenerator.class)
|
||||||
private static native ResourceMap<StringResource> getFullDateFormatMap();
|
private static native ResourceMap<StringResource> getFullDateFormatMap();
|
||||||
|
|
||||||
public static String resolveLongDateFormat(String language, String country) {
|
public static String resolveLongDateFormat(String language, String country) {
|
||||||
return resolveFormatSymbols(getLongDateFormatMap(), language, country);
|
return resolveFormatSymbols(getLongDateFormatMap(), language, country);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MetadataProvider(DateFormatMetadataGenerator.class)
|
||||||
private static native ResourceMap<StringResource> getLongDateFormatMap();
|
private static native ResourceMap<StringResource> getLongDateFormatMap();
|
||||||
|
|
||||||
public static String resolveShortDateFormat(String language, String country) {
|
public static String resolveShortDateFormat(String language, String country) {
|
||||||
return resolveFormatSymbols(getShortDateFormatMap(), language, country);
|
return resolveFormatSymbols(getShortDateFormatMap(), language, country);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MetadataProvider(DateFormatMetadataGenerator.class)
|
||||||
private static native ResourceMap<StringResource> getShortDateFormatMap();
|
private static native ResourceMap<StringResource> getShortDateFormatMap();
|
||||||
|
|
||||||
public static String resolveNumberFormat(String language, String country) {
|
public static String resolveNumberFormat(String language, String country) {
|
||||||
|
|
|
@ -33,6 +33,10 @@ public class CLDRLocale {
|
||||||
String[] shortMonths;
|
String[] shortMonths;
|
||||||
String[] weekdays;
|
String[] weekdays;
|
||||||
String[] shortWeekdays;
|
String[] shortWeekdays;
|
||||||
|
String shortDateFormat;
|
||||||
|
String mediumDateFormat;
|
||||||
|
String longDateFormat;
|
||||||
|
String fullDateFormat;
|
||||||
|
|
||||||
public Map<String, String> getLanguages() {
|
public Map<String, String> getLanguages() {
|
||||||
return Collections.unmodifiableMap(languages);
|
return Collections.unmodifiableMap(languages);
|
||||||
|
@ -65,4 +69,20 @@ public class CLDRLocale {
|
||||||
public String[] getShortWeekdays() {
|
public String[] getShortWeekdays() {
|
||||||
return Arrays.copyOf(shortWeekdays, shortWeekdays.length);
|
return Arrays.copyOf(shortWeekdays, shortWeekdays.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getShortDateFormat() {
|
||||||
|
return shortDateFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMediumDateFormat() {
|
||||||
|
return mediumDateFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLongDateFormat() {
|
||||||
|
return longDateFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFullDateFormat() {
|
||||||
|
return fullDateFormat;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,7 @@ public class CLDRReader {
|
||||||
readShortMonths(localeName, localeInfo, root);
|
readShortMonths(localeName, localeInfo, root);
|
||||||
readWeekdays(localeName, localeInfo, root);
|
readWeekdays(localeName, localeInfo, root);
|
||||||
readShortWeekdays(localeName, localeInfo, root);
|
readShortWeekdays(localeName, localeInfo, root);
|
||||||
|
readDateFormats(localeName, localeInfo, root);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,27 +184,37 @@ public class CLDRReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readWeekdays(String localeCode, CLDRLocale locale, JsonObject root) {
|
private void readWeekdays(String localeCode, CLDRLocale locale, JsonObject root) {
|
||||||
JsonObject monthsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject()
|
JsonObject weekdaysJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject()
|
||||||
.get("dates").getAsJsonObject().get("calendars").getAsJsonObject()
|
.get("dates").getAsJsonObject().get("calendars").getAsJsonObject()
|
||||||
.get("gregorian").getAsJsonObject().get("days").getAsJsonObject()
|
.get("gregorian").getAsJsonObject().get("days").getAsJsonObject()
|
||||||
.get("format").getAsJsonObject().get("wide").getAsJsonObject();
|
.get("format").getAsJsonObject().get("wide").getAsJsonObject();
|
||||||
locale.weekdays = new String[7];
|
locale.weekdays = new String[7];
|
||||||
for (int i = 0; i < 7; ++i) {
|
for (int i = 0; i < 7; ++i) {
|
||||||
locale.weekdays[i] = monthsJson.get(weekdayKeys[i]).getAsString();
|
locale.weekdays[i] = weekdaysJson.get(weekdayKeys[i]).getAsString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readShortWeekdays(String localeCode, CLDRLocale locale, JsonObject root) {
|
private void readShortWeekdays(String localeCode, CLDRLocale locale, JsonObject root) {
|
||||||
JsonObject monthsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject()
|
JsonObject weekdaysJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject()
|
||||||
.get("dates").getAsJsonObject().get("calendars").getAsJsonObject()
|
.get("dates").getAsJsonObject().get("calendars").getAsJsonObject()
|
||||||
.get("gregorian").getAsJsonObject().get("days").getAsJsonObject()
|
.get("gregorian").getAsJsonObject().get("days").getAsJsonObject()
|
||||||
.get("format").getAsJsonObject().get("short").getAsJsonObject();
|
.get("format").getAsJsonObject().get("abbreviated").getAsJsonObject();
|
||||||
locale.shortWeekdays = new String[7];
|
locale.shortWeekdays = new String[7];
|
||||||
for (int i = 0; i < 7; ++i) {
|
for (int i = 0; i < 7; ++i) {
|
||||||
locale.shortWeekdays[i] = monthsJson.get(weekdayKeys[i]).getAsString();
|
locale.shortWeekdays[i] = weekdaysJson.get(weekdayKeys[i]).getAsString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void readDateFormats(String localeCode, CLDRLocale locale, JsonObject root) {
|
||||||
|
JsonObject formatsJson = root.get("main").getAsJsonObject().get(localeCode).getAsJsonObject()
|
||||||
|
.get("dates").getAsJsonObject().get("calendars").getAsJsonObject()
|
||||||
|
.get("gregorian").getAsJsonObject().get("dateFormats").getAsJsonObject();
|
||||||
|
locale.shortDateFormat = formatsJson.get("short").getAsString();
|
||||||
|
locale.mediumDateFormat = formatsJson.get("medium").getAsString();
|
||||||
|
locale.longDateFormat = formatsJson.get("long").getAsString();
|
||||||
|
locale.fullDateFormat = formatsJson.get("full").getAsString();
|
||||||
|
}
|
||||||
|
|
||||||
private void readWeekData(InputStream input) {
|
private void readWeekData(InputStream input) {
|
||||||
JsonObject root = (JsonObject)new JsonParser().parse(new InputStreamReader(input));
|
JsonObject root = (JsonObject)new JsonParser().parse(new InputStreamReader(input));
|
||||||
JsonObject weekJson = root.get("supplemental").getAsJsonObject().get("weekData").getAsJsonObject();
|
JsonObject weekJson = root.get("supplemental").getAsJsonObject().get("weekData").getAsJsonObject();
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2014 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.classlib.impl.unicode;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import org.teavm.model.MethodReference;
|
||||||
|
import org.teavm.platform.metadata.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev
|
||||||
|
*/
|
||||||
|
public class DateFormatMetadataGenerator implements MetadataGenerator {
|
||||||
|
@Override
|
||||||
|
public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) {
|
||||||
|
switch (method.getName()) {
|
||||||
|
case "getDateFormatMap":
|
||||||
|
return getDateFormatMap(context, new FormatExtractor() {
|
||||||
|
@Override public String extract(CLDRLocale locale) {
|
||||||
|
return locale.getMediumDateFormat();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
case "getShortDateFormatMap":
|
||||||
|
return getDateFormatMap(context, new FormatExtractor() {
|
||||||
|
@Override public String extract(CLDRLocale locale) {
|
||||||
|
return locale.getShortDateFormat();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
case "getLongDateFormatMap":
|
||||||
|
return getDateFormatMap(context, new FormatExtractor() {
|
||||||
|
@Override public String extract(CLDRLocale locale) {
|
||||||
|
return locale.getLongDateFormat();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
case "getFullDateFormatMap":
|
||||||
|
return getDateFormatMap(context, new FormatExtractor() {
|
||||||
|
@Override public String extract(CLDRLocale locale) {
|
||||||
|
return locale.getFullDateFormat();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Method is not supported: " + method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Resource getDateFormatMap(MetadataGeneratorContext context, FormatExtractor extractor) {
|
||||||
|
CLDRReader reader = context.getService(CLDRReader.class);
|
||||||
|
ResourceMap<StringResource> result = context.createResourceMap();
|
||||||
|
for (Map.Entry<String, CLDRLocale> entry : reader.getKnownLocales().entrySet()) {
|
||||||
|
StringResource formatRes = context.createResource(StringResource.class);
|
||||||
|
formatRes.setValue(extractor.extract(entry.getValue()));
|
||||||
|
result.put(entry.getKey(), formatRes);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FormatExtractor {
|
||||||
|
String extract(CLDRLocale locale);
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,7 +22,6 @@ import org.teavm.classlib.java.util.*;
|
||||||
|
|
||||||
public abstract class TDateFormat extends TFormat {
|
public abstract class TDateFormat extends TFormat {
|
||||||
protected TCalendar calendar;
|
protected TCalendar calendar;
|
||||||
//protected NumberFormat numberFormat;
|
|
||||||
public final static int DEFAULT = 2;
|
public final static int DEFAULT = 2;
|
||||||
public final static int FULL = 0;
|
public final static int FULL = 0;
|
||||||
public final static int LONG = 1;
|
public final static int LONG = 1;
|
||||||
|
@ -54,30 +53,22 @@ public abstract class TDateFormat extends TFormat {
|
||||||
public Object clone() {
|
public Object clone() {
|
||||||
TDateFormat clone = (TDateFormat) super.clone();
|
TDateFormat clone = (TDateFormat) super.clone();
|
||||||
clone.calendar = (TCalendar) calendar.clone();
|
clone.calendar = (TCalendar) calendar.clone();
|
||||||
// TODO: implement
|
|
||||||
//clone.numberFormat = (NumberFormat) numberFormat.clone();
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement
|
|
||||||
/*
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object object) {
|
public boolean equals(Object object) {
|
||||||
if (this == object) {
|
if (this == object) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!(object instanceof DateFormat)) {
|
if (!(object instanceof TDateFormat)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
DateFormat dateFormat = (DateFormat) object;
|
TDateFormat dateFormat = (TDateFormat) object;
|
||||||
return numberFormat.equals(dateFormat.numberFormat) &&
|
return calendar.getFirstDayOfWeek() == dateFormat.calendar.getFirstDayOfWeek() &&
|
||||||
calendar.getFirstDayOfWeek() == dateFormat.calendar.getFirstDayOfWeek() &&
|
|
||||||
calendar.getMinimalDaysInFirstWeek() == dateFormat.calendar.getMinimalDaysInFirstWeek() &&
|
calendar.getMinimalDaysInFirstWeek() == dateFormat.calendar.getMinimalDaysInFirstWeek() &&
|
||||||
calendar.isLenient() == dateFormat.calendar.isLenient();
|
calendar.isLenient() == dateFormat.calendar.isLenient();
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final StringBuffer format(Object object, StringBuffer buffer, TFieldPosition field) {
|
public final StringBuffer format(Object object, StringBuffer buffer, TFieldPosition field) {
|
||||||
|
@ -156,10 +147,6 @@ public abstract class TDateFormat extends TFormat {
|
||||||
return getDateTimeInstance(SHORT, SHORT);
|
return getDateTimeInstance(SHORT, SHORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public NumberFormat getNumberFormat() {
|
|
||||||
return numberFormat;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
static String getStyleName(int style) {
|
static String getStyleName(int style) {
|
||||||
String styleName;
|
String styleName;
|
||||||
switch (style) {
|
switch (style) {
|
||||||
|
@ -199,14 +186,11 @@ public abstract class TDateFormat extends TFormat {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: implement
|
@Override
|
||||||
/*@Override
|
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return calendar.getFirstDayOfWeek() + calendar.getMinimalDaysInFirstWeek() +
|
return calendar.getFirstDayOfWeek() + calendar.getMinimalDaysInFirstWeek() +
|
||||||
(calendar.isLenient() ? 1231 : 1237) + numberFormat.hashCode();
|
(calendar.isLenient() ? 1231 : 1237);
|
||||||
|
}
|
||||||
return 0;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
public boolean isLenient() {
|
public boolean isLenient() {
|
||||||
return calendar.isLenient();
|
return calendar.isLenient();
|
||||||
|
@ -236,10 +220,6 @@ public abstract class TDateFormat extends TFormat {
|
||||||
calendar.setLenient(value);
|
calendar.setLenient(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public void setNumberFormat(NumberFormat format) {
|
|
||||||
numberFormat = format;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
public static class Field extends TFormat.Field {
|
public static class Field extends TFormat.Field {
|
||||||
private static THashMap<Integer, Field> table = new THashMap<>();
|
private static THashMap<Integer, Field> table = new THashMap<>();
|
||||||
public final static Field ERA = new Field("era", TCalendar.ERA);
|
public final static Field ERA = new Field("era", TCalendar.ERA);
|
||||||
|
|
|
@ -93,7 +93,7 @@ abstract class TDateFormatElement {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void format(TCalendar date, StringBuffer buffer) {
|
public void format(TCalendar date, StringBuffer buffer) {
|
||||||
int weekday = date.get(TCalendar.DAY_OF_WEEK);
|
int weekday = date.get(TCalendar.DAY_OF_WEEK) - 1;
|
||||||
buffer.append(abbreviated ? shortWeeks[weekday] : weeks[weekday]);
|
buffer.append(abbreviated ? shortWeeks[weekday] : weeks[weekday]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ abstract class TDateFormatElement {
|
||||||
int num = 0;
|
int num = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int pos = position.getIndex();
|
int pos = position.getIndex();
|
||||||
while (position.getIndex() < text.length() && i < length) {
|
while (pos < text.length()) {
|
||||||
char c = text.charAt(pos);
|
char c = text.charAt(pos);
|
||||||
if (c >= '0' && c <= '9') {
|
if (c >= '0' && c <= '9') {
|
||||||
num = num * 10 + (c - '0');
|
num = num * 10 + (c - '0');
|
||||||
|
@ -193,7 +193,7 @@ abstract class TDateFormatElement {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i == 0) {
|
if (i < length) {
|
||||||
position.setErrorIndex(position.getIndex());
|
position.setErrorIndex(position.getIndex());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ public class TDecimalFormat extends TNumberFormat {
|
||||||
super.setMinimumIntegerDigits(decimalData.getMinimumIntegerDigits());
|
super.setMinimumIntegerDigits(decimalData.getMinimumIntegerDigits());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void applyPattern(String pattern) {
|
public void applyPattern(@SuppressWarnings("unused") String pattern) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -155,7 +155,6 @@ public abstract class TNumberFormat extends TFormat {
|
||||||
TParsePosition pos = new TParsePosition(0);
|
TParsePosition pos = new TParsePosition(0);
|
||||||
Number number = parse(string, pos);
|
Number number = parse(string, pos);
|
||||||
if (pos.getIndex() == 0) {
|
if (pos.getIndex() == 0) {
|
||||||
// text.1D=: {0}
|
|
||||||
throw new TParseException("Unparseable number: " + string, pos.getErrorIndex());
|
throw new TParseException("Unparseable number: " + string, pos.getErrorIndex());
|
||||||
}
|
}
|
||||||
return number;
|
return number;
|
||||||
|
|
|
@ -79,8 +79,7 @@ public class TSimpleDateFormat extends TDateFormat {
|
||||||
@Override
|
@Override
|
||||||
public TDate parse(String string, TParsePosition position) {
|
public TDate parse(String string, TParsePosition position) {
|
||||||
TCalendar calendar = new TGregorianCalendar(locale);
|
TCalendar calendar = new TGregorianCalendar(locale);
|
||||||
calendar.set(0, 0, 0, 0, 0, 0);
|
calendar.clear();
|
||||||
calendar.set(TCalendar.MILLISECOND, 0);
|
|
||||||
for (TDateFormatElement element : elements) {
|
for (TDateFormatElement element : elements) {
|
||||||
if (position.getIndex() > string.length()) {
|
if (position.getIndex() > string.length()) {
|
||||||
position.setErrorIndex(position.getErrorIndex());
|
position.setErrorIndex(position.getErrorIndex());
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2014 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.classlib.java.text;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
import java.util.Locale;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev
|
||||||
|
*/
|
||||||
|
public class DateFormatTest {
|
||||||
|
@Test
|
||||||
|
public void shortDateFormatHandled() throws ParseException {
|
||||||
|
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
|
||||||
|
assertEquals("6/24/14", format.format(getDateWithZoneOffset(1403539200000L)));
|
||||||
|
assertEquals(1403539200000L, getTimeWithoutZoneOffset(format.parse("6/24/14")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mediumDateFormatHandled() throws ParseException {
|
||||||
|
DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH);
|
||||||
|
assertEquals("Jun 24, 2014", format.format(getDateWithZoneOffset(1403539200000L)));
|
||||||
|
assertEquals(1403539200000L, getTimeWithoutZoneOffset(format.parse("Jun 24, 2014")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void longDateFormatHandled() throws ParseException {
|
||||||
|
DateFormat format = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH);
|
||||||
|
assertEquals("June 24, 2014", format.format(getDateWithZoneOffset(1403539200000L)));
|
||||||
|
assertEquals(1403539200000L, getTimeWithoutZoneOffset(format.parse("June 24, 2014")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fullDateFormatHandled() throws ParseException {
|
||||||
|
DateFormat format = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH);
|
||||||
|
assertEquals("Tuesday, June 24, 2014", format.format(getDateWithZoneOffset(1403539200000L)));
|
||||||
|
assertEquals(1403539200000L, getTimeWithoutZoneOffset(format.parse("Tuesday, June 24, 2014")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Date getDateWithZoneOffset(long milliseconds) {
|
||||||
|
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||||
|
calendar.setTimeInMillis(milliseconds);
|
||||||
|
milliseconds += calendar.get(Calendar.ZONE_OFFSET);
|
||||||
|
return new Date(milliseconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getTimeWithoutZoneOffset(Date date) {
|
||||||
|
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||||
|
calendar.setTime(date);
|
||||||
|
return calendar.getTimeInMillis() - calendar.get(Calendar.ZONE_OFFSET);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,151 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2014 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.classlib.java.text;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
import java.util.Locale;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev
|
||||||
|
*/
|
||||||
|
public class SimpleDateFormatTest {
|
||||||
|
@Test
|
||||||
|
public void firstDayOfWeekMatches() {
|
||||||
|
assertEquals(Calendar.SUNDAY, new GregorianCalendar(Locale.ENGLISH).getFirstDayOfWeek());
|
||||||
|
assertEquals(1, new GregorianCalendar(Locale.ENGLISH).getMinimalDaysInFirstWeek());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fieldsFormatted() {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals("2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fieldsParsed() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void eraHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("G yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals("AD 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("AD 2014-06-24 17:33:49")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shortYearHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals("14-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("14-06-24 17:33:49")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void weekInYearHandled() throws ParseException {
|
||||||
|
long day = 24 * 3600 * 1000;
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss www", Locale.ENGLISH);
|
||||||
|
assertEquals("2014-06-24 17:33:49 026", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals("2014-06-28 17:33:49 026", format.format(getDateWithZoneOffset(1403602429504L + day * 4)));
|
||||||
|
assertEquals("2014-06-29 17:33:49 027", format.format(getDateWithZoneOffset(1403602429504L + day * 5)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49 026")));
|
||||||
|
assertEquals(1403602429000L + day * 4, getTimeWithoutZoneOffset(format.parse("2014-06-28 17:33:49 026")));
|
||||||
|
assertEquals(1403602429000L + day * 5, getTimeWithoutZoneOffset(format.parse("2014-06-29 17:33:49 027")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void weekInMonthHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss WW", Locale.ENGLISH);
|
||||||
|
assertEquals("2014-06-24 17:33:49 04", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49 04")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dayInYearHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss DD", Locale.ENGLISH);
|
||||||
|
assertEquals("2014-06-24 17:33:49 175", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49 175")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void weekdayInMonthHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss F", Locale.ENGLISH);
|
||||||
|
assertEquals("2014-06-24 17:33:49 4", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49 4")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shortWeekdayHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("E yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals("Tue 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Tue 2014-06-24 17:33:49")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void longWeekdayHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("EEEE, yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals("Tuesday, 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Tuesday, 2014-06-24 17:33:49")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void numericWeekdayHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("u yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals("2 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2 2014-06-24 17:33:49")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void amPmHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 'at' hh:mm:ss a", Locale.ENGLISH);
|
||||||
|
assertEquals("2014-06-24 at 05:33:49 PM", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 at 05:33:49 PM")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shortMonthHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("MMM, dd yyyy HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals("Jun, 24 2014 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Jun, 24 2014 17:33:49")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void longMonthHandled() throws ParseException {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("MMMM, dd yyyy HH:mm:ss", Locale.ENGLISH);
|
||||||
|
assertEquals("June, 24 2014 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
||||||
|
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("June, 24 2014 17:33:49")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Date getDateWithZoneOffset(long milliseconds) {
|
||||||
|
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||||
|
calendar.setTimeInMillis(milliseconds);
|
||||||
|
milliseconds += calendar.get(Calendar.ZONE_OFFSET);
|
||||||
|
return new Date(milliseconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getTimeWithoutZoneOffset(Date date) {
|
||||||
|
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||||
|
calendar.setTime(date);
|
||||||
|
return calendar.getTimeInMillis() - calendar.get(Calendar.ZONE_OFFSET);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,95 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2014 Alexey Andreev.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.teavm.classlib.java.text;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.GregorianCalendar;
|
|
||||||
import java.util.Locale;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author Alexey Andreev
|
|
||||||
*/
|
|
||||||
public class SimpleDateTimeFormatTest {
|
|
||||||
@Test
|
|
||||||
public void firstDayOfWeekMatches() {
|
|
||||||
assertEquals(Calendar.SUNDAY, new GregorianCalendar(Locale.ENGLISH).getFirstDayOfWeek());
|
|
||||||
assertEquals(1, new GregorianCalendar(Locale.ENGLISH).getMinimalDaysInFirstWeek());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void fieldsFormatted() {
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
|
||||||
assertEquals("2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void fieldsParsed() throws ParseException {
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
|
||||||
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void eraHandled() throws ParseException {
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat("G yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
|
||||||
assertEquals("AD 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
|
||||||
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("AD 2014-06-24 17:33:49")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shortYearHandled() throws ParseException {
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
|
||||||
assertEquals("14-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
|
|
||||||
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("14-06-24 17:33:49")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void weekInYearHandled() throws ParseException {
|
|
||||||
long day = 24 * 3600 * 1000;
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss www", Locale.ENGLISH);
|
|
||||||
assertEquals("14-06-24 17:33:49 026", format.format(getDateWithZoneOffset(1403602429504L)));
|
|
||||||
assertEquals("14-06-28 17:33:49 026", format.format(getDateWithZoneOffset(1403602429504L + day * 4)));
|
|
||||||
assertEquals("14-06-29 17:33:49 027", format.format(getDateWithZoneOffset(1403602429504L + day * 5)));
|
|
||||||
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("14-06-24 17:33:49 026")));
|
|
||||||
assertEquals(1403602429000L + day * 4, getTimeWithoutZoneOffset(format.parse("14-06-28 17:33:49 026")));
|
|
||||||
assertEquals(1403602429000L + day * 5, getTimeWithoutZoneOffset(format.parse("14-06-29 17:33:49 027")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void weekInMonthHandled() throws ParseException {
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss WW", Locale.ENGLISH);
|
|
||||||
assertEquals("14-06-24 17:33:49 04", format.format(getDateWithZoneOffset(1403602429504L)));
|
|
||||||
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("14-06-24 17:33:49 04")));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Date getDateWithZoneOffset(long milliseconds) {
|
|
||||||
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
|
||||||
calendar.setTimeInMillis(milliseconds);
|
|
||||||
milliseconds += calendar.get(Calendar.ZONE_OFFSET);
|
|
||||||
return new Date(milliseconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
private long getTimeWithoutZoneOffset(Date date) {
|
|
||||||
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
|
||||||
calendar.setTime(date);
|
|
||||||
return calendar.getTimeInMillis() - calendar.get(Calendar.ZONE_OFFSET);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user