public static long unixTimestamp(int year, int month, int day, int hour, int minute, int second) { final int date = ymdToUnixDate(year, month, day); return (long) date * MILLIS_PER_DAY + (long) hour * MILLIS_PER_HOUR + (long) minute * MILLIS_PER_MINUTE + (long) second * MILLIS_PER_SECOND; }
++year; return ymdToUnixDate(year, 1, 1); case MONTH: if (!floor && day > 1) { ++month; return ymdToUnixDate(year, month, 1); default: throw new AssertionError(range);
public static int dateStringToUnixDate(String s) { int hyphen1 = s.indexOf('-'); int y; int m; int d; if (hyphen1 < 0) { y = Integer.parseInt(s.trim()); m = 1; d = 1; } else { y = Integer.parseInt(s.substring(0, hyphen1).trim()); final int hyphen2 = s.indexOf('-', hyphen1 + 1); if (hyphen2 < 0) { m = Integer.parseInt(s.substring(hyphen1 + 1).trim()); d = 1; } else { m = Integer.parseInt(s.substring(hyphen1 + 1, hyphen2).trim()); d = Integer.parseInt(s.substring(hyphen2 + 1).trim()); } } return ymdToUnixDate(y, m, d); }
/** Adds a given number of months to a date, represented as the number of * days since the epoch. */ public static int addMonths(int date, int m) { int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date); int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date); int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date); int y = m / 12; y0 += y; m0 += m - y * 12; int last = lastDay(y0, m0); if (d0 > last) { d0 = 1; if (++m0 > 12) { m0 = 1; ++y0; } } return DateTimeUtils.ymdToUnixDate(y0, m0, d0); }
public static long unixTimestamp(int year, int month, int day, int hour, int minute, int second) { final int date = ymdToUnixDate(year, month, day); return (long) date * MILLIS_PER_DAY + (long) hour * MILLIS_PER_HOUR + (long) minute * MILLIS_PER_MINUTE + (long) second * MILLIS_PER_SECOND; }
public static long unixTimestamp(int year, int month, int day, int hour, int minute, int second) { final int date = ymdToUnixDate(year, month, day); return (long) date * MILLIS_PER_DAY + (long) hour * MILLIS_PER_HOUR + (long) minute * MILLIS_PER_MINUTE + (long) second * MILLIS_PER_SECOND; }
public static long unixTimestamp(int year, int month, int day, int hour, int minute, int second) { final int date = ymdToUnixDate(year, month, day); return (long) date * MILLIS_PER_DAY + (long) hour * MILLIS_PER_HOUR + (long) minute * MILLIS_PER_MINUTE + (long) second * MILLIS_PER_SECOND; }
public static long unixTimestamp(int year, int month, int day, int hour, int minute, int second) { final int date = ymdToUnixDate(year, month, day); return (long) date * MILLIS_PER_DAY + (long) hour * MILLIS_PER_HOUR + (long) minute * MILLIS_PER_MINUTE + (long) second * MILLIS_PER_SECOND; }
public static long unixTimestamp(int year, int month, int day, int hour, int minute, int second) { final int date = ymdToUnixDate(year, month, day); return (long) date * MILLIS_PER_DAY + (long) hour * MILLIS_PER_HOUR + (long) minute * MILLIS_PER_MINUTE + (long) second * MILLIS_PER_SECOND; }
/** Returns the number of milliseconds since the epoch. */ public long getMillisSinceEpoch() { final int year = Integer.valueOf(v.substring(0, 4)); final int month = Integer.valueOf(v.substring(5, 7)); final int day = Integer.valueOf(v.substring(8, 10)); final int h = Integer.valueOf(v.substring(11, 13)); final int m = Integer.valueOf(v.substring(14, 16)); final int s = Integer.valueOf(v.substring(17, 19)); final int ms = getMillisInSecond(); final int d = DateTimeUtils.ymdToUnixDate(year, month, day); return d * DateTimeUtils.MILLIS_PER_DAY + h * DateTimeUtils.MILLIS_PER_HOUR + m * DateTimeUtils.MILLIS_PER_MINUTE + s * DateTimeUtils.MILLIS_PER_SECOND + ms; }
/** Returns the number of milliseconds since the epoch. */ public long getMillisSinceEpoch() { final int year = Integer.valueOf(v.substring(0, 4)); final int month = Integer.valueOf(v.substring(5, 7)); final int day = Integer.valueOf(v.substring(8, 10)); final int h = Integer.valueOf(v.substring(11, 13)); final int m = Integer.valueOf(v.substring(14, 16)); final int s = Integer.valueOf(v.substring(17, 19)); final int ms = getMillisInSecond(); final int d = DateTimeUtils.ymdToUnixDate(year, month, day); return d * DateTimeUtils.MILLIS_PER_DAY + h * DateTimeUtils.MILLIS_PER_HOUR + m * DateTimeUtils.MILLIS_PER_MINUTE + s * DateTimeUtils.MILLIS_PER_SECOND + ms; }
/** Returns the number of days since the epoch. */ public int getDaysSinceEpoch() { int year = getYear(); int month = getMonth(); int day = getDay(); return DateTimeUtils.ymdToUnixDate(year, month, day); }
/** Returns the number of days since the epoch. */ public int getDaysSinceEpoch() { int year = getYear(); int month = getMonth(); int day = getDay(); return DateTimeUtils.ymdToUnixDate(year, month, day); }
@Test public void testYmdToUnixDate() { assertEquals(0, ymdToUnixDate(1970, 1, 1)); assertEquals(365, ymdToUnixDate(1971, 1, 1)); assertEquals(-365, ymdToUnixDate(1969, 1, 1)); assertEquals(11015, ymdToUnixDate(2000, 2, 28)); assertEquals(11016, ymdToUnixDate(2000, 2, 29)); assertEquals(11017, ymdToUnixDate(2000, 3, 1)); assertEquals(-9077, ymdToUnixDate(1945, 2, 24)); assertEquals(-25509, ymdToUnixDate(1900, 2, 28)); assertEquals(-25508, ymdToUnixDate(1900, 3, 1)); }
@Test public void testYmdToUnixDate() { assertEquals(0, ymdToUnixDate(1970, 1, 1)); assertEquals(365, ymdToUnixDate(1971, 1, 1)); assertEquals(-365, ymdToUnixDate(1969, 1, 1)); assertEquals(11015, ymdToUnixDate(2000, 2, 28)); assertEquals(11016, ymdToUnixDate(2000, 2, 29)); assertEquals(11017, ymdToUnixDate(2000, 3, 1)); assertEquals(-9077, ymdToUnixDate(1945, 2, 24)); assertEquals(-25509, ymdToUnixDate(1900, 2, 28)); assertEquals(-25508, ymdToUnixDate(1900, 3, 1)); }
/** Adds a given number of months to a date, represented as the number of * days since the epoch. */ public static int addMonths(int date, int m) { int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date); int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date); int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date); int y = m / 12; y0 += y; m0 += m - y * 12; int last = lastDay(y0, m0); if (d0 > last) { d0 = last; } return DateTimeUtils.ymdToUnixDate(y0, m0, d0); }
/** Adds a given number of months to a date, represented as the number of * days since the epoch. */ public static int addMonths(int date, int m) { int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date); int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date); int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date); int y = m / 12; y0 += y; m0 += m - y * 12; int last = lastDay(y0, m0); if (d0 > last) { d0 = last; } return DateTimeUtils.ymdToUnixDate(y0, m0, d0); }
@Test public void testDateConversion() { for (int i = 0; i < 4000; ++i) { for (int j = 1; j <= 12; ++j) { String date = String.format(Locale.ENGLISH, "%04d-%02d-28", i, j); assertThat(unixDateToString(ymdToUnixDate(i, j, 28)), is(date)); } } }
@Test public void testDateConversion() { for (int i = 0; i < 4000; ++i) { for (int j = 1; j <= 12; ++j) { String date = String.format(Locale.ENGLISH, "%04d-%02d-28", i, j); assertThat(unixDateToString(ymdToUnixDate(i, j, 28)), is(date)); } } }
private void checkAddMonths(int y0, int m0, int d0, int y1, int m1, int d1, int months) { final int date0 = ymdToUnixDate(y0, m0, d0); final long date = addMonths(date0, months); final int date1 = ymdToUnixDate(y1, m1, d1); assertThat((int) date, is(date1)); assertThat(subtractMonths(date1, date0), anyOf(is(months), is(months + 1))); assertThat(subtractMonths(date1 + 1, date0), anyOf(is(months), is(months + 1))); assertThat(subtractMonths(date1, date0 + 1), anyOf(is(months), is(months - 1))); assertThat(subtractMonths(d2ts(date1, 1), d2ts(date0, 0)), anyOf(is(months), is(months + 1))); assertThat(subtractMonths(d2ts(date1, 0), d2ts(date0, 1)), anyOf(is(months - 1), is(months), is(months + 1))); }