Java Code Examples for java.util.GregorianCalendar#setGregorianChange()
The following examples show how to use
java.util.GregorianCalendar#setGregorianChange() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Performance.java From threetenbp with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static List<GregorianCalendar> setupGCal() { java.util.TimeZone tz = java.util.TimeZone.getTimeZone("Europe/London"); Random random = new Random(47658758756875687L); List<GregorianCalendar> list = new ArrayList<GregorianCalendar>(SIZE); long start = System.nanoTime(); for (int i = 0; i < SIZE; i++) { GregorianCalendar t = new GregorianCalendar(tz); t.setGregorianChange(new Date(Long.MIN_VALUE)); t.set(random.nextInt(10000), random.nextInt(12), random.nextInt(28) + 1, random.nextInt(24), random.nextInt(60), random.nextInt(60)); list.add(t); } long end = System.nanoTime(); System.out.println("GCalendar: Setup: " + NF.format(end - start) + " ns"); result("GregCal-I", end - start); return list; }
Example 2
Source File: DateDialogNormalizer.java From 365browser with Apache License 2.0 | 5 votes |
static DateAndMillis create(long millisUtc) { // millisUtc uses the Gregorian calendar only, so disable the Julian changeover date. GregorianCalendar utcCal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); utcCal.setGregorianChange(new Date(Long.MIN_VALUE)); utcCal.setTimeInMillis(millisUtc); int year = utcCal.get(Calendar.YEAR); int month = utcCal.get(Calendar.MONTH); int day = utcCal.get(Calendar.DAY_OF_MONTH); return create(year, month, day); }
Example 3
Source File: TimestampTest.java From ion-java with Apache License 2.0 | 5 votes |
@Ignore // TODO ion-java#165 @Test public void testCustomCalendarLeapYearAfterCalendarValue() { // Create a purely Julian calendar, where leap years were every four years. GregorianCalendar julianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); julianCalendar.setGregorianChange(new Date(Long.MAX_VALUE)); julianCalendar.clear(); // 1800 is not a leap year under the Gregorian calendar, but it is under the Julian calendar. julianCalendar.set(1800, Calendar.MARCH, 1, 0, 0); Timestamp timestamp1 = Timestamp.forCalendar(julianCalendar); Timestamp timestamp2 = Timestamp.forCalendar(timestamp1.calendarValue()).addMinute(-1); assertEquals("1800-02-29T23:59Z", timestamp2.toString()); }
Example 4
Source File: TimestampTest.java From ion-java with Apache License 2.0 | 5 votes |
@Ignore // TODO ion-java#165 @Test public void testCustomCalendarLeapYearAfterWithLocalOffset() { // Create a purely Julian calendar, where leap years were every four years. GregorianCalendar julianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); julianCalendar.setGregorianChange(new Date(Long.MAX_VALUE)); julianCalendar.clear(); // 1800 is not a leap year under the Gregorian calendar, but it is under the Julian calendar. julianCalendar.set(1800, Calendar.FEBRUARY, 28, 23, 59, 59); Timestamp timestamp1 = Timestamp.forCalendar(julianCalendar); Timestamp timestamp2 = timestamp1.withLocalOffset(0).addSecond(1); assertEquals("1800-02-29T00:00:00Z", timestamp2.toString()); }
Example 5
Source File: BigdataValueFactoryImpl.java From database with GNU General Public License v2.0 | 5 votes |
@Override public BigdataLiteralImpl createXSDDateTime(final long timestamp) { final TimeZone tz = TimeZone.getDefault()/*getTimeZone("GMT")*/; final GregorianCalendar c = new GregorianCalendar(tz); c.setGregorianChange(new Date(Long.MIN_VALUE)); c.setTimeInMillis(timestamp); final XMLGregorianCalendar xmlGC = DateTimeExtension.datatypeFactorySingleton.newXMLGregorianCalendar(c); return createLiteral(xmlGC); }
Example 6
Source File: CalendarRegression.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
static Date getGregorianDate(int year, int month, int dayOfMonth) { GregorianCalendar g = new GregorianCalendar(); // Make g a pure Gregorian calendar g.setGregorianChange(new Date(Long.MIN_VALUE)); g.clear(); g.set(year, month, dayOfMonth); return g.getTime(); }
Example 7
Source File: TimestampTest.java From ion-java with Apache License 2.0 | 5 votes |
@Ignore // TODO ion-java#165 @Test public void testConstructCustomCalendarWithValidLeapYear() { // Create a purely Julian calendar, where leap years were every four years. GregorianCalendar julianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); julianCalendar.setGregorianChange(new Date(Long.MAX_VALUE)); julianCalendar.clear(); // 1800 is not a leap year under the Gregorian calendar, but it is under the Julian calendar. julianCalendar.set(1800, Calendar.FEBRUARY, 29, 0, 0); Timestamp timestamp = Timestamp.forCalendar(julianCalendar); assertEquals("1800-02-29T00:00Z", timestamp.toString()); }
Example 8
Source File: CalendarTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * Test the mapping between millis and fields. For the purposes * of this test, we don't care about timezones and week data * (first day of week, minimal days in first week). */ @SuppressWarnings("deprecation") public void TestMapping() { TimeZone saveZone = TimeZone.getDefault(); int[] DATA = { // Julian# Year Month DOM JULIAN:Year Month, DOM 2440588, 1970, JANUARY, 1, 1969, DECEMBER, 19, 2415080, 1900, MARCH, 1, 1900, FEBRUARY, 17, 2451604, 2000, FEBRUARY, 29, 2000, FEBRUARY, 16, 2452269, 2001, DECEMBER, 25, 2001, DECEMBER, 12, 2416526, 1904, FEBRUARY, 15, 1904, FEBRUARY, 2, 2416656, 1904, JUNE, 24, 1904, JUNE, 11, 1721426, 1, JANUARY, 1, 1, JANUARY, 3, 2000000, 763, SEPTEMBER, 18, 763, SEPTEMBER, 14, 4000000, 6239, JULY, 12, 6239, MAY, 28, 8000000, 17191, FEBRUARY, 26, 17190, OCTOBER, 22, 10000000, 22666, DECEMBER, 20, 22666, JULY, 5}; try { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); Date PURE_GREGORIAN = new Date(Long.MIN_VALUE); Date PURE_JULIAN = new Date(Long.MAX_VALUE); GregorianCalendar cal = new GregorianCalendar(); for (int i = 0; i < DATA.length; i += 7) { int julian = DATA[i]; int year = DATA[i + 1]; int month = DATA[i + 2]; int dom = DATA[i + 3]; int year2, month2, dom2; long millis = ((long) julian - EPOCH_JULIAN) * ONE_DAY; String s; // Test Gregorian computation cal.setGregorianChange(PURE_GREGORIAN); cal.clear(); cal.set(year, month, dom); long calMillis = cal.getTime().getTime(); long delta = calMillis - millis; cal.setTime(new Date(millis)); year2 = cal.get(YEAR); month2 = cal.get(MONTH); dom2 = cal.get(DAY_OF_MONTH); s = "G " + year + "-" + (month + 1 - JANUARY) + "-" + dom + " => " + calMillis + " (" + ((float) delta / ONE_DAY) + " day delta) => " + year2 + "-" + (month2 + 1 - JANUARY) + "-" + dom2; if (delta != 0 || year != year2 || month != month2 || dom != dom2) { errln(s + " FAIL"); } else { logln(s); } // Test Julian computation year = DATA[i + 4]; month = DATA[i + 5]; dom = DATA[i + 6]; cal.setGregorianChange(PURE_JULIAN); cal.clear(); cal.set(year, month, dom); calMillis = cal.getTime().getTime(); delta = calMillis - millis; cal.setTime(new Date(millis)); year2 = cal.get(YEAR); month2 = cal.get(MONTH); dom2 = cal.get(DAY_OF_MONTH); s = "J " + year + "-" + (month + 1 - JANUARY) + "-" + dom + " => " + calMillis + " (" + ((float) delta / ONE_DAY) + " day delta) => " + year2 + "-" + (month2 + 1 - JANUARY) + "-" + dom2; if (delta != 0 || year != year2 || month != month2 || dom != dom2) { errln(s + " FAIL"); } else { logln(s); } } cal.setGregorianChange(new Date(1582 - 1900, OCTOBER, 15)); auxMapping(cal, 1582, OCTOBER, 4); auxMapping(cal, 1582, OCTOBER, 15); auxMapping(cal, 1582, OCTOBER, 16); for (int y = 800; y < 3000; y += 1 + 100 * Math.random()) { for (int m = JANUARY; m <= DECEMBER; ++m) { auxMapping(cal, y, m, 15); } } } finally { TimeZone.setDefault(saveZone); } }
Example 9
Source File: JavatimeTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 10
Source File: JavatimeTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; @SuppressWarnings("deprecation") long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 11
Source File: DateTimeExtension.java From database with GNU General Public License v2.0 | 4 votes |
/** * Use the long value of the {@link XSDLongIV} delegate (which represents * milliseconds since the epoch) to create a an XMLGregorianCalendar * object (GMT timezone). Use the XMLGregorianCalendar to create a datatype * literal value with the appropriate datatype. */ public V asValue(final LiteralExtensionIV iv, final BigdataValueFactory vf) { if (!datatypes.containsKey(iv.getExtensionIV())) { throw new IllegalArgumentException("unrecognized datatype"); } /* * Milliseconds since the epoch. */ final long l = iv.getDelegate().longValue(); final TimeZone tz = BSBMHACK ? TimeZone.getDefault()/*getTimeZone("GMT")*/ : defaultTZ; final GregorianCalendar c = new GregorianCalendar(tz); c.setGregorianChange(new Date(Long.MIN_VALUE)); c.setTimeInMillis(l); try { final BigdataURI dt = datatypes.get(iv.getExtensionIV()); final DatatypeFactory f = datatypeFactorySingleton; final XMLGregorianCalendar xmlGC = f.newXMLGregorianCalendar(c); String s = xmlGC.toString(); final int offset = s.startsWith("-") ? 1 : 0; if (dt.equals(XSD.DATETIME)) { if (BSBMHACK) { // Chopping off the milliseconds part and the trailing 'Z'. final int i = s.lastIndexOf('.'); if (i >= 0) { s = s.substring(0, i); } } } else if (dt.equals(XSD.DATE)) { // YYYY-MM-DD (10 chars) s = s.substring(0, 10+offset); } else if (dt.equals(XSD.TIME)) { // everything after the date (from 11 chars in) s = s.substring(11+offset); } else if (dt.equals(XSD.GDAY)) { // gDay Defines a part of a date - the day (---DD) s = "---" + s.substring(8+offset, 10+offset); } else if (dt.equals(XSD.GMONTH)) { // gMonth Defines a part of a date - the month (--MM) s = "--" + s.substring(5+offset, 7+offset); } else if (dt.equals(XSD.GMONTHDAY)) { // gMonthDay Defines a part of a date - the month and day (--MM-DD) s = "--" + s.substring(5+offset, 10+offset); } else if (dt.equals(XSD.GYEAR)) { // gYear Defines a part of a date - the year (YYYY) s = s.substring(0, 4+offset); } else if (dt.equals(XSD.GYEARMONTH)) { // gYearMonth Defines a part of a date - the year and month (YYYY-MM) s = s.substring(0, 7+offset); } return (V) vf.createLiteral(s, dt); } catch (RuntimeException ex) { if (InnerCause.isInnerCause(ex, InterruptedException.class)) { throw ex; } throw new IllegalArgumentException("bad iv: " + iv, ex); } }
Example 12
Source File: JavatimeTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 13
Source File: CalendarRegression.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * Calendar and GregorianCalendar hashCode() methods need improvement. * Calendar needs a good implementation that subclasses can override, * and GregorianCalendar should use that implementation. */ public void Test4136399() { /* Note: This test is actually more strict than it has to be. * Technically, there is no requirement that unequal objects have * unequal hashes. We only require equal objects to have equal hashes. * It is desirable for unequal objects to have distributed hashes, but * there is no hard requirement here. * * In this test we make assumptions about certain attributes of calendar * objects getting represented in the hash, which need not always be the * case (although it does work currently with the given test). */ Calendar a = Calendar.getInstance(); Calendar b = (Calendar) a.clone(); if (a.hashCode() != b.hashCode()) { errln("Calendar hash code unequal for cloned objects"); } b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek()); if (a.hashCode() == b.hashCode()) { errln("Calendar hash code ignores minimal days in first week"); } b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek()); b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1); // Next day if (a.hashCode() == b.hashCode()) { errln("Calendar hash code ignores first day of week"); } b.setFirstDayOfWeek(a.getFirstDayOfWeek()); b.setLenient(!a.isLenient()); if (a.hashCode() == b.hashCode()) { errln("Calendar hash code ignores lenient setting"); } b.setLenient(a.isLenient()); // Assume getTimeZone() returns a reference, not a clone // of a reference -- this is true as of this writing b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset() + 60 * 60 * 1000); if (a.hashCode() == b.hashCode()) { errln("Calendar hash code ignores zone"); } b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset()); GregorianCalendar c = new GregorianCalendar(); GregorianCalendar d = (GregorianCalendar) c.clone(); if (c.hashCode() != d.hashCode()) { errln("GregorianCalendar hash code unequal for clones objects"); } Date cutover = c.getGregorianChange(); d.setGregorianChange(new Date(cutover.getTime() + 24 * 60 * 60 * 1000)); if (c.hashCode() == d.hashCode()) { errln("GregorianCalendar hash code ignores cutover"); } }
Example 14
Source File: JavatimeTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 15
Source File: FileTime.java From jdk-1.7-annotated with Apache License 2.0 | 4 votes |
/** * Returns the string representation of this {@code FileTime}. The string * is returned in the <a * href="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> format: * <pre> * YYYY-MM-DDThh:mm:ss[.s+]Z * </pre> * where "{@code [.s+]}" represents a dot followed by one of more digits * for the decimal fraction of a second. It is only present when the decimal * fraction of a second is not zero. For example, {@code * FileTime.fromMillis(1234567890000L).toString()} yields {@code * "2009-02-13T23:31:30Z"}, and {@code FileTime.fromMillis(1234567890123L).toString()} * yields {@code "2009-02-13T23:31:30.123Z"}. * * <p> A {@code FileTime} is primarily intended to represent the value of a * file's time stamp. Where used to represent <i>extreme values</i>, where * the year is less than "{@code 0001}" or greater than "{@code 9999}" then * this method deviates from ISO 8601 in the same manner as the * <a href="http://www.w3.org/TR/xmlschema-2/#deviantformats">XML Schema * language</a>. That is, the year may be expanded to more than four digits * and may be negative-signed. If more than four digits then leading zeros * are not present. The year before "{@code 0001}" is "{@code -0001}". * * @return the string representation of this file time */ @Override public String toString() { String v = valueAsString; if (v == null) { // overflow saturates to Long.MIN_VALUE or Long.MAX_VALUE so this // limits the range: // [-292275056-05-16T16:47:04.192Z,292278994-08-17T07:12:55.807Z] long ms = toMillis(); // nothing to do when seconds/minutes/hours/days String fractionAsString = ""; if (unit.compareTo(TimeUnit.SECONDS) < 0) { long fraction = asDaysAndNanos().fractionOfSecondInNanos(); if (fraction != 0L) { // fraction must be positive if (fraction < 0L) { final long MAX_FRACTION_PLUS_1 = 1000L * 1000L * 1000L; fraction += MAX_FRACTION_PLUS_1; if (ms != Long.MIN_VALUE) ms--; } // convert to String, adding leading zeros as required and // stripping any trailing zeros String s = Long.toString(fraction); int len = s.length(); int width = 9 - len; StringBuilder sb = new StringBuilder("."); while (width-- > 0) { sb.append('0'); } if (s.charAt(len-1) == '0') { // drop trailing zeros len--; while (s.charAt(len-1) == '0') len--; sb.append(s.substring(0, len)); } else { sb.append(s); } fractionAsString = sb.toString(); } } // create calendar to use with formatter. GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.ROOT); if (value < 0L) cal.setGregorianChange(new Date(Long.MIN_VALUE)); cal.setTimeInMillis(ms); // years are negative before common era String sign = (cal.get(Calendar.ERA) == GregorianCalendar.BC) ? "-" : ""; // [-]YYYY-MM-DDThh:mm:ss[.s]Z v = new Formatter(Locale.ROOT) .format("%s%tFT%tR:%tS%sZ", sign, cal, cal, cal, fractionAsString) .toString(); valueAsString = v; } return v; }
Example 16
Source File: InputDialogContainer.java From 365browser with Apache License 2.0 | 4 votes |
protected void showPickerDialog(final int dialogType, double dialogValue, double min, double max, double step) { Calendar cal; // |dialogValue|, |min|, |max| mean different things depending on the |dialogType|. // For input type=month is the number of months since 1970. // For input type=time it is milliseconds since midnight. // For other types they are just milliseconds since 1970. // If |dialogValue| is NaN it means an empty value. We will show the current time. if (Double.isNaN(dialogValue)) { cal = Calendar.getInstance(); cal.set(Calendar.MILLISECOND, 0); } else { if (dialogType == TextInputType.MONTH) { cal = MonthPicker.createDateFromValue(dialogValue); } else if (dialogType == TextInputType.WEEK) { cal = WeekPicker.createDateFromValue(dialogValue); } else { GregorianCalendar gregorianCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); // According to the HTML spec we only use the Gregorian calendar // so we ignore the Julian/Gregorian transition. gregorianCalendar.setGregorianChange(new Date(Long.MIN_VALUE)); gregorianCalendar.setTimeInMillis((long) dialogValue); cal = gregorianCalendar; } } if (dialogType == TextInputType.DATE) { showPickerDialog(dialogType, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0, 0, min, max, step); } else if (dialogType == TextInputType.TIME) { showPickerDialog(dialogType, 0, 0, 0, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), 0, 0, 0, min, max, step); } else if (dialogType == TextInputType.DATE_TIME || dialogType == TextInputType.DATE_TIME_LOCAL) { showPickerDialog(dialogType, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), cal.get(Calendar.MILLISECOND), 0, min, max, step); } else if (dialogType == TextInputType.MONTH) { showPickerDialog(dialogType, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 0, 0, 0, 0, 0, 0, min, max, step); } else if (dialogType == TextInputType.WEEK) { int year = WeekPicker.getISOWeekYearForDate(cal); int week = WeekPicker.getWeekForDate(cal); showPickerDialog(dialogType, year, 0, 0, 0, 0, 0, 0, week, min, max, step); } }
Example 17
Source File: JavatimeTest.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 18
Source File: JavatimeTest.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { int N = 10000; long t1970 = new java.util.Date(70, 0, 01).getTime(); Random r = new Random(); for (int i = 0; i < N; i++) { int days = r.nextInt(50) * 365 + r.nextInt(365); long secs = t1970 + days * 86400 + r.nextInt(86400); int nanos = r.nextInt(NANOS_PER_SECOND); int nanos_ms = nanos / 1000000 * 1000000; // millis precision long millis = secs * 1000 + r.nextInt(1000); LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); Instant inst = Instant.ofEpochSecond(secs, nanos); Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); ///////////// java.util.Date ///////////////////////// Date jud = new java.util.Date(millis); Instant inst0 = jud.toInstant(); if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d"); } // roundtrip only with millis precision Date jud0 = Date.from(inst_ms); if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: instant -> j.u.d -> instant"); } //////////// java.util.GregorianCalendar ///////////// GregorianCalendar cal = new GregorianCalendar(); // non-roundtrip of tz name between j.u.tz and j.t.zid cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE)); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setMinimalDaysInFirstWeek(4); cal.setTimeInMillis(millis); ZonedDateTime zdt0 = cal.toZonedDateTime(); if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli() || !cal.equals(GregorianCalendar.from(zdt0))) { System.out.println("cal:" + cal); System.out.println("zdt:" + zdt0); System.out.println("calNew:" + GregorianCalendar.from(zdt0)); System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt -> gcal"); } inst0 = cal.toInstant(); if (cal.getTimeInMillis() != inst0.toEpochMilli()) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: gcal -> zdt"); } ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault()); GregorianCalendar cal0 = GregorianCalendar.from(zdt); if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis() || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) { System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); throw new RuntimeException("FAILED: zdt -> gcal -> zdt"); } } ///////////// java.util.TimeZone ///////////////////////// for (String zidStr : TimeZone.getAvailableIDs()) { // TBD: tzdt intergration if (zidStr.startsWith("SystemV") || zidStr.contains("Riyadh8") || zidStr.equals("US/Pacific-New") || zidStr.equals("EST") || zidStr.equals("HST") || zidStr.equals("MST")) { continue; } ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS); if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) { throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr); } TimeZone tz = TimeZone.getTimeZone(zidStr); // no round-trip for alias and "GMT" if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId())) && !ZoneId.SHORT_IDS.containsKey(zidStr) && !zidStr.startsWith("GMT")) { throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr); } } System.out.println("Passed!"); }
Example 19
Source File: DateUtil.java From Viewer with Apache License 2.0 | 3 votes |
/** * 对日期(时间)中由field参数指定的日期成员进行加减计算. <br> * 例子: <br> * 如果Date类型的d为 2005年8月20日,那么 <br> * calculate(d,GregorianCalendar.YEAR,-10)的值为1995年8月20日 <br> * 而calculate(d,GregorianCalendar.YEAR,+10)的值为2015年8月20日 <br> * * @param d * 日期(时间). * @param field * 日期成员. <br> * 日期成员主要有: <br> * 年:GregorianCalendar.YEAR <br> * 月:GregorianCalendar.MONTH <br> * 日:GregorianCalendar.DATE <br> * 时:GregorianCalendar.HOUR <br> * 分:GregorianCalendar.MINUTE <br> * 秒:GregorianCalendar.SECOND <br> * 毫秒:GregorianCalendar.MILLISECOND <br> * @param amount * 加减计算的幅度.+n=加n个由参数field指定的日期成员值;-n=减n个由参数field代表的日期成员值. * @return 计算后的日期(时间). */ private static Date calculate(Date d, int field, int amount) { if (d == null) return null; GregorianCalendar g = new GregorianCalendar(); g.setGregorianChange(d); g.add(field, amount); return g.getTime(); }
Example 20
Source File: ZYDateUtils.java From Viewer with Apache License 2.0 | 3 votes |
/** * 对日期(时间)中由field参数指定的日期成员进行加减计算. <br> * 例子: <br> * 如果Date类型的d为 2005年8月20日,那么 <br> * calculate(d,GregorianCalendar.YEAR,-10)的值为1995年8月20日 <br> * 而calculate(d,GregorianCalendar.YEAR,+10)的值为2015年8月20日 <br> * * @param d * 日期(时间). * @param field * 日期成员. <br> * 日期成员主要有: <br> * 年:GregorianCalendar.YEAR <br> * 月:GregorianCalendar.MONTH <br> * 日:GregorianCalendar.DATE <br> * 时:GregorianCalendar.HOUR <br> * 分:GregorianCalendar.MINUTE <br> * 秒:GregorianCalendar.SECOND <br> * 毫秒:GregorianCalendar.MILLISECOND <br> * @param amount * 加减计算的幅度.+n=加n个由参数field指定的日期成员值;-n=减n个由参数field代表的日期成员值. * @return 计算后的日期(时间). */ private static Date calculate(Date d, int field, int amount) { if (d == null) return null; GregorianCalendar g = new GregorianCalendar(); g.setGregorianChange(d); g.add(field, amount); return g.getTime(); }