Skip to content

Commit 16cf0b1

Browse files
authored
Merge pull request #237 from BobHanson/master
Date fixes
2 parents a4b456b + f6002cd commit 16cf0b1

10 files changed

Lines changed: 165 additions & 130 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20260627090323
1+
20260705081251
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20260627090323
1+
20260705081251
1.07 KB
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/text/DateFormat.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,11 @@ public final static DateFormat getInstance() {
607607
*/
608608
public static Locale[] getAvailableLocales()
609609
{
610-
LocaleServiceProviderPool pool =
611-
LocaleServiceProviderPool.getPool(DateFormatProvider.class);
612-
return pool.getAvailableLocales();
610+
// SwingJS just return US locale
611+
return new Locale[] { Locale.US };
612+
// LocaleServiceProviderPool pool =
613+
// LocaleServiceProviderPool.getPool(DateFormatProvider.class);
614+
// return pool.getAvailableLocales();
613615
}
614616

615617
/**

sources/net.sf.j2s.java.core/src/java/text/Format.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public abstract class Format implements Serializable, Cloneable {
140140

141141
private static final long serialVersionUID = -299282585814624189L;
142142
protected boolean isSimpleMMDDYY;
143+
protected boolean isISO8601;
143144

144145

145146

sources/net.sf.j2s.java.core/src/java/text/SimpleDateFormat.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ public SimpleDateFormat(String pattern, Locale locale, boolean allowNull) {
583583
this.pattern = pattern;
584584
this.locale = locale;
585585
this.isSimpleMMDDYY = isSimple(pattern, locale);
586+
this.isISO8601 = pattern.startsWith("yyyy-MM-dd'T'HH:mm:ss");
586587
// lazy calendar initialization for SwingJS
587588
// avoids loading 49 classes!
588589
// this.formatData = DateFormatSymbols.getInstance(locale);
@@ -594,6 +595,8 @@ public SimpleDateFormat(String pattern, Locale locale, boolean allowNull) {
594595

595596
/* Initialize compiledPattern and numberFormat fields */
596597
private void initializeLocale() {
598+
if (this.isSimpleMMDDYY || this.isISO8601)
599+
return;
597600
if (locale == null)
598601
locale = Locale.getDefault();
599602
// Verify and compile the given pattern.
@@ -622,11 +625,10 @@ private static NumberFormat getCachedNumberFormatData(Locale key) {
622625

623626
@Override
624627
public Calendar getCalendar() {
625-
Calendar cal = super.getCalendar();
626-
if (cal == null) {
628+
if (super.getCalendar() == null) {
627629
initializeCalendar();
628630
}
629-
return cal;
631+
return calendar;
630632
}
631633

632634
private void initializeCalendar() {
@@ -894,11 +896,11 @@ public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos
894896
return format(date, toAppendTo, pos.getFieldDelegate());
895897
}
896898

897-
@Override
898-
public final String format (Object obj) {
899-
return format(obj, new StringBuffer(), isSimpleMMDDYY ? null : new FieldPosition(0)).toString();
900-
}
901-
899+
@Override
900+
public final String format(Object obj) {
901+
return format(obj, new StringBuffer(), isSimpleMMDDYY ? null : new FieldPosition(0)).toString();
902+
}
903+
902904
@Override
903905
public String format(Date date) {
904906
if (isSimpleMMDDYY) {
@@ -923,8 +925,8 @@ private boolean isSimple(String pattern, Locale locale) {
923925
switch (ch) {
924926
case 'M':
925927
case 'd':
926-
case 'H': // H Hour in day (0-23)
927-
case 'k': // k Hour in day (1-24)
928+
case 'H': // H Hour in day (0-23)
929+
case 'k': // k Hour in day (1-24)
928930
case 'y':
929931
case 'h':
930932
case 'm':
@@ -940,27 +942,29 @@ private boolean isSimple(String pattern, Locale locale) {
940942

941943
// toISOString() returns a string in the
942944
// 1970-01-01T00:00:00.000Z
943-
// 0.........1.........
945+
// 0.........1.........
944946
// 01234567890123456789
945947
//
946-
//new Date().toString()
948+
// new Date().toString()
947949
// Sun Mar 09 14:35:53 GMT-0500 2025"
948-
// 0.........1.........
950+
// 0.........1.........
949951
// 01234567890123456789
950-
private final static String[] fields = { "yyyy", "yy", "MM", "M", "dd", "d", "hh", "h", "HH", "H", "kk", "k", "mm", "m", "ss", "s" };
951-
private final static int[] fieldlens = { 4, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1 };
952-
private final static int[] fieldzero = { 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };
953-
private final static int[] adj = { 0, 0, 3, 3, 0, 0, 12, 12, 23, 23, 24, 24, 0, 0, 0, 0 };
954-
private final static int[] isopts = { 0, 2, 5, 5, 8, 8, 11, 11, 11, 11, 11, 11, 14, 14, 17, 17 };
955-
private final static int[] isolens = { 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
956-
private final static int[] locpts = { -4, -2, 4, 4, 8, 8, 11, 11, 11, 11, 11, 11, 14, 14, 17, 17 };
957-
private final static int[] loclens = { 4, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
952+
private final static String[] fields = { "yyyy", "yy", "MM", "M", "dd", "d", "hh", "h", "HH", "H", "kk", "k", "mm",
953+
"m", "ss", "s" };
954+
private final static int[] fieldlens = { 4, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1 };
955+
private final static int[] fieldzero = { 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };
956+
private final static int[] adj = { 0, 0, 3, 3, 0, 0, 12, 12, 23, 23, 24, 24, 0, 0, 0, 0 };
957+
private final static int[] isopts = { 0, 2, 5, 5, 8, 8, 11, 11, 11, 11, 11, 11, 14, 14, 17, 17 };
958+
private final static int[] isolens = { 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
959+
private final static int[] locpts = { -4, -2, 4, 4, 8, 8, 11, 11, 11, 11, 11, 11, 14, 14, 17, 17 };
960+
private final static int[] loclens = { 4, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
958961
private final static int fieldCount = fields.length;
959962

960963
private Object formatSimpleJS(Date date, StringBuffer sb) {
961964
// considered doing this -- still could if we want GMT
962-
//String iso = (/** @j2sNative date.toISOString() || */"");
963-
String loc = (/** @j2sNative date.toString() || */"");
965+
// String iso = (/** @j2sNative date.toISOString() || */"");
966+
String loc = (/** @j2sNative date.toString() || */
967+
"");
964968
String ret = pattern;
965969
for (int i = 0, l = 2; i < fieldCount; i++, l = 3 - l) {
966970
int pt = ret.indexOf(fields[i]);
@@ -980,7 +984,7 @@ private String rep(String p, int pt, int flen, int fzero, String iso, int i0, in
980984
String v = (i0 < 0 ? iso.substring(iso.length() + i0) : iso.substring(i0, i0 + isolen));
981985
int iv = -1;
982986
if (isoadj == 3) {
983-
iv = (months.indexOf(v)/3 + 1);
987+
iv = (months.indexOf(v) / 3 + 1);
984988
} else if (isoadj != 0) {
985989
iv = Integer.parseInt(v);
986990
switch (isoadj) {
@@ -1085,17 +1089,19 @@ public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
10851089

10861090
// Maps from DecimalFormatSymbols index to Field constant
10871091
private static Field[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID;
1088-
1092+
10891093
private static Field[] getPatternIndexFieldIDs() {
10901094
if (PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID == null) {
10911095
PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID = new Field[] { Field.ERA, Field.YEAR, Field.MONTH,
1092-
Field.DAY_OF_MONTH, Field.HOUR_OF_DAY1, Field.HOUR_OF_DAY0, Field.MINUTE, Field.SECOND, Field.MILLISECOND,
1093-
Field.DAY_OF_WEEK, Field.DAY_OF_YEAR, Field.DAY_OF_WEEK_IN_MONTH, Field.WEEK_OF_YEAR, Field.WEEK_OF_MONTH,
1094-
Field.AM_PM, Field.HOUR1, Field.HOUR0, Field.TIME_ZONE, Field.TIME_ZONE, };
1096+
Field.DAY_OF_MONTH, Field.HOUR_OF_DAY1, Field.HOUR_OF_DAY0, Field.MINUTE, Field.SECOND,
1097+
Field.MILLISECOND, Field.DAY_OF_WEEK, Field.DAY_OF_YEAR, Field.DAY_OF_WEEK_IN_MONTH,
1098+
Field.WEEK_OF_YEAR, Field.WEEK_OF_MONTH, Field.AM_PM, Field.HOUR1, Field.HOUR0, Field.TIME_ZONE,
1099+
Field.TIME_ZONE, };
10951100
}
1096-
return PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID;
1097-
1101+
return PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID;
1102+
10981103
}
1104+
10991105
/**
11001106
* Private member function that does the real date/time formatting.
11011107
*/

sources/net.sf.j2s.java.core/src/java/time/Instant.java

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
import java.io.InvalidObjectException;
7979
import java.io.ObjectInputStream;
8080
import java.io.Serializable;
81-
import java.time.format.DateTimeFormatter;
81+
//import java.time.format.DateTimeFormatter;
8282
import java.time.format.DateTimeParseException;
8383
import java.time.temporal.ChronoField;
8484
import java.time.temporal.ChronoUnit;
@@ -92,6 +92,7 @@
9292
import java.time.temporal.TemporalUnit;
9393
import java.time.temporal.UnsupportedTemporalTypeException;
9494
import java.time.temporal.ValueRange;
95+
import java.util.Date;
9596
import java.util.Objects;
9697

9798
/**
@@ -392,7 +393,9 @@ public static Instant from(TemporalAccessor temporal) {
392393
* @throws DateTimeParseException if the text cannot be parsed
393394
*/
394395
public static Instant parse(final CharSequence text) {
395-
return DateTimeFormatter.ISO_INSTANT.parse(text, Instant::from);
396+
long l = /** j2sNative new Date(text.toString()).getTime() || */0;
397+
return ofEpochMilli(l);
398+
//DateTimeFormatter.ISO_INSTANT.parse(text, Instant::from);
396399
}
397400

398401
//-----------------------------------------------------------------------
@@ -1321,45 +1324,57 @@ public int hashCode() {
13211324
*/
13221325
@Override
13231326
public String toString() {
1324-
return DateTimeFormatter.ISO_INSTANT.format(this);
1325-
}
1326-
1327-
// -----------------------------------------------------------------------
1328-
/**
1329-
* Writes the object using a
1330-
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1331-
* @serialData
1332-
* <pre>
1333-
* out.writeByte(2); // identifies an Instant
1334-
* out.writeLong(seconds);
1335-
* out.writeInt(nanos);
1336-
* </pre>
1337-
*
1338-
* @return the instance of {@code Ser}, not null
1339-
*/
1340-
private Object writeReplace() {
1341-
return new Ser(Ser.INSTANT_TYPE, this);
1342-
}
1343-
1344-
/**
1345-
* Defend against malicious streams.
1346-
*
1347-
* @param s the stream to read
1348-
* @throws InvalidObjectException always
1349-
*/
1350-
private void readObject(ObjectInputStream s) throws InvalidObjectException {
1351-
throw new InvalidObjectException("Deserialization via serialization delegate");
1352-
}
1353-
1354-
void writeExternal(DataOutput out) throws IOException {
1355-
out.writeLong(seconds);
1356-
out.writeInt(nanos);
1357-
}
1358-
1359-
static Instant readExternal(DataInput in) throws IOException {
1360-
long seconds = in.readLong();
1361-
int nanos = in.readInt();
1362-
return Instant.ofEpochSecond(seconds, nanos);
1327+
@SuppressWarnings("unused")
1328+
Date d = new Date(this.toEpochMilli());
1329+
1330+
/**
1331+
*
1332+
* @j2sNative
1333+
*
1334+
* return d.toISOString();
1335+
*/
1336+
{
1337+
return null;
1338+
// return DateTimeFormatter.ISO_INSTANT.format(this);
1339+
}
13631340
}
13641341

1342+
// // -----------------------------------------------------------------------
1343+
// /**
1344+
// * Writes the object using a
1345+
// * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1346+
// * @serialData
1347+
// * <pre>
1348+
// * out.writeByte(2); // identifies an Instant
1349+
// * out.writeLong(seconds);
1350+
// * out.writeInt(nanos);
1351+
// * </pre>
1352+
// *
1353+
// * @return the instance of {@code Ser}, not null
1354+
// */
1355+
// private Object writeReplace() {
1356+
// return new Ser(Ser.INSTANT_TYPE, this);
1357+
// }
1358+
//
1359+
// /**
1360+
// * Defend against malicious streams.
1361+
// *
1362+
// * @param s the stream to read
1363+
// * @throws InvalidObjectException always
1364+
// */
1365+
// private void readObject(ObjectInputStream s) throws InvalidObjectException {
1366+
// throw new InvalidObjectException("Deserialization via serialization delegate");
1367+
// }
1368+
//
1369+
// void writeExternal(DataOutput out) throws IOException {
1370+
// out.writeLong(seconds);
1371+
// out.writeInt(nanos);
1372+
// }
1373+
//
1374+
// static Instant readExternal(DataInput in) throws IOException {
1375+
// long seconds = in.readLong();
1376+
// int nanos = in.readInt();
1377+
// return Instant.ofEpochSecond(seconds, nanos);
1378+
// }
1379+
//
13651380
}

sources/net.sf.j2s.java.core/src/swingjs/JSToolkit.java

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
import javax.swing.UIManager;
6262
import javax.swing.plaf.ComponentUI;
6363

64-
import javajs.api.js.HTML5Applet.JSFunction;
6564
import javajs.api.js.HTML5Applet;
65+
import javajs.api.js.HTML5Applet.JSFunction;
6666
import javajs.util.JSThread;
6767
import javajs.util.PT;
6868
import sun.awt.AppContext;
@@ -930,58 +930,14 @@ else if (family.equals("monospaced"))
930930
* @param isoType null, 8824, or 8601, or a standard SimpleDataFormat format
931931
* @return formatted date
932932
*/
933-
public static String getDateFormat(String isoType) {
934-
String prefix = "";
935-
String suffix = "";
936-
/**
937-
*
938-
* Mon Jan 07 2013 19:54:39 GMT-0600
939-
* or YYYYMMDDHHmmssOHH'mm'
940-
*
941-
* @j2sNative
942-
*
943-
* if (isoType == null) {
944-
* return ("" + (new Date())).split(" (")[0];
945-
* }
946-
* if (isoType.indexOf("8824") >= 0) {
947-
* var d = new Date();
948-
* var x = d.toString().split(" ");
949-
* var MM = "0" + (1 + d.getMonth()); MM = MM.substring(MM.length - 2);
950-
* var dd = "0" + d.getDate(); dd = dd.substring(dd.length - 2);
951-
* return x[3] + MM + dd + x[4].replace(/\:/g,"") + x[5].substring(3,6) + "'" + x[5].substring(6,8) + "'"
952-
* }
953-
* if (isoType.indexOf("8601") >= 0){
954-
* var d = new Date();
955-
* var x = d.toString().split(" ");
956-
* // Firefox now doing this?
957-
* if (x.length == 1)
958-
* return x;
959-
* var MM = "0" + (1 + d.getMonth()); MM = MM.substring(MM.length - 2);
960-
* var dd = "0" + d.getDate(); dd = dd.substring(dd.length - 2);
961-
* return x[3] + '-' + MM + '-' + dd + 'T' + x[4]
962-
* }
963-
*
964-
*/
965-
{
966-
// if (isoType == null) {
967-
// isoType = "EEE dd MMM yyyy HH:mm:ss 'GMT'Z";
968-
// } else if (isoType.contains("8824")) {
969-
// prefix = "D:";
970-
// suffix = "'00'";
971-
// isoType = "YYYYMMddHHmmssX";
972-
// } else if (isoType.contains("8601")) {
973-
// isoType = "yyyy-MM-dd'T'HH:mm:ss";
974-
// }
975-
Date date = (Date) Interface.getInstance("java.util.Date", true);
976-
SimpleDateFormat format = (SimpleDateFormat) Interface.getInstanceWithParams("java.text.SimpleDateFormat", new Class<?>[] { String.class }, new Object[] { isoType});
977-
return prefix + format.format(date) + suffix;
978-
}
979-
}
980-
981-
@Override
982-
public void beep() {
983-
System.out.println("JSToolkit.beep");
984-
}
933+
public static String getDateFormat(String isoType) {
934+
return JSUtil.getDateFormat(isoType);
935+
}
936+
937+
@Override
938+
public void beep() {
939+
System.out.println("JSToolkit.beep");
940+
}
985941

986942
/**
987943
* Get a gnu.jpdf.PDFJob.

0 commit comments

Comments
 (0)