/** * Converts this partial to a full datetime using the specified time zone * setting the time fields from this instance and the date fields from * the current time. * <p> * This method uses the chronology from this instance plus the time zone * specified. * * @param zone the zone to use, null means default * @return this date as a datetime with the time as the current time */ public DateTime toDateTimeToday(DateTimeZone zone) { Chronology chrono = getChronology().withZone(zone); long instantMillis = DateTimeUtils.currentTimeMillis(); long resolved = chrono.set(this, instantMillis); return new DateTime(resolved, chrono); }
private void freezeTime(Long millis) { DateTimeUtils.setCurrentMillisFixed(millis); }
public void cleanup() throws IOException { DateTimeUtils.setCurrentMillisSystem(); if (this.fs.exists(testTempDirPath)) { if (!this.fs.delete(testTempDirPath, true)) { throw new IOException("Failed to clean up path " + this.testTempDirPath); } } }
/** * Prints a ReadableInstant, using the chronology supplied by the instant. * * @param appendable the destination to format to, not null * @param instant instant to format, null means now * @since 2.0 */ public void printTo(Appendable appendable, ReadableInstant instant) throws IOException { long millis = DateTimeUtils.getInstantMillis(instant); Chronology chrono = DateTimeUtils.getInstantChronology(instant); printTo(appendable, millis, chrono); }
/** * Resolves this partial against another complete instant to create a new * full instant. The combination is performed using the chronology of the * specified instant. * <p> * For example, if this partial represents a time, then the result of this * method will be the datetime from the specified base instant plus the * time from this partial. * * @param baseInstant the instant that provides the missing fields, null means now * @return the combined datetime */ public DateTime toDateTime(ReadableInstant baseInstant) { Chronology chrono = DateTimeUtils.getInstantChronology(baseInstant); long instantMillis = DateTimeUtils.getInstantMillis(baseInstant); long resolved = chrono.set(this, instantMillis); return new DateTime(resolved, chrono); }
/** * Converts this LocalDate to a full datetime using the specified time zone * setting the date fields from this instance and the time fields from * the current time. * <p> * This method uses the chronology from this instance plus the time zone * specified. * <p> * This method will throw an exception if the datetime that would be * created does not exist when the time zone is taken into account. * <p> * This instance is immutable and unaffected by this method call. * * @param zone the zone to use, null means default zone * @return this date as a datetime with the time as the current time */ public DateTime toDateTimeAtCurrentTime(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); Chronology chrono = getChronology().withZone(zone); long instantMillis = DateTimeUtils.currentTimeMillis(); long resolved = chrono.set(this, instantMillis); return new DateTime(resolved, chrono); }
/** * Returns a copy of this datetime with a different chronology. * <p> * The returned object will be either be a new instance or <code>this</code>. * Only the chronology will change, the millis are kept. * * @param newChronology the new chronology, null means ISO default * @return a copy of this datetime with a different chronology */ public DateTime withChronology(Chronology newChronology) { newChronology = DateTimeUtils.getChronology(newChronology); return (newChronology == getChronology() ? this : new DateTime(getMillis(), newChronology)); }
@Before public void setUp() throws Exception { // Init zone info Context context = InstrumentationRegistry.getContext(); JodaTimeAndroid.init(context); // Force the system into 24-hour time for tests ContentResolver cr = context.getContentResolver(); mOldTime1224Setting = Settings.System.getString(cr, Settings.System.TIME_12_24); Settings.System.putString(cr, Settings.System.TIME_12_24, "24"); // Force all tests to be in the US locale; that way we can test output in consistent manner Application app = (Application) InstrumentationRegistry.getContext().getApplicationContext(); Resources res = app.getBaseContext().getResources(); Configuration config = res.getConfiguration(); Locale.setDefault(Locale.US); config.locale = Locale.US; res.updateConfiguration(config, res.getDisplayMetrics()); // Force the default timezone mDefaultJodaTz = DateTimeZone.forID("America/New_York"); mOldDefaultJodaTz = DateTimeZone.getDefault(); DateTimeZone.setDefault(mDefaultJodaTz); // ...And for the system as well mDefaultSystemTz = TimeZone.getTimeZone("America/Chicago"); mOldDefaultSystemTz = TimeZone.getDefault(); TimeZone.setDefault(mDefaultSystemTz); // Force current "now" time, so all tests can be consistent mNow = new DateTime(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE_OF_HOUR, SECOND_OF_MINUTE, MILLIS_OF_SECOND, mDefaultJodaTz); DateTimeUtils.setCurrentMillisFixed(mNow.getMillis()); }
DateTimeUtils.setCurrentMillisFixed(new DateTime(2015, 7, 15, 10, 0).withZone(DateTimeZone.UTC).getMillis()); Assert.assertTrue(deletedPaths.get(0).equals(snapshot2)); } finally { DateTimeUtils.setCurrentMillisSystem();
DateTimeUtils.setCurrentMillisFixed(new DateTime(2015, 7, 15, 10, 0).getMillis()); Assert.assertTrue(Trash.TRASH_SNAPSHOT_NAME_FORMATTER.parseDateTime(createdDir.getName()).equals(new DateTime().withZone( DateTimeZone.UTC))); Assert.assertEquals(movedPaths.size(), 1); Assert.assertTrue(movedPaths.get(0).second().getParent().equals(createdDir)); } finally { DateTimeUtils.setCurrentMillisSystem();
@BeforeMethod public void setUp() throws Exception { DateTimeUtils.setCurrentMillisFixed(new DateTime(2016,3,15,10,15).getMillis()); }
@Test public void testISORetentionDuration() throws Exception { DateTimeUtils.setCurrentMillisFixed(new DateTime(2016, 2, 11, 10, 0, 0, 0).getMillis()); try { // 20 Days verify("P20D", ImmutableList.of(WithDate(new DateTime(2016, 1, 5, 10, 0, 0, 0)), WithDate(new DateTime(2016, 1, 6, 10, 0, 0, 0))), ImmutableList.of(WithDate(new DateTime(2016, 2, 10, 10, 0, 0, 0)), WithDate(new DateTime(2016, 2, 11, 10, 0, 0, 0)))); // 2 Months verify("P2M", ImmutableList.of(WithDate(new DateTime(2015, 12, 5, 10, 0, 0, 0)), WithDate(new DateTime(2015, 11, 5, 10, 0, 0, 0))), ImmutableList.of(WithDate(new DateTime(2016, 2, 10, 10, 0, 0, 0)), WithDate(new DateTime(2016, 1, 10, 10, 0, 0, 0)))); // 2 Years verify("P2Y", ImmutableList.of(WithDate(new DateTime(2014, 1, 5, 10, 0, 0, 0)), WithDate(new DateTime(2013, 1, 5, 10, 0, 0, 0))), ImmutableList.of(WithDate(new DateTime(2016, 2, 10, 10, 0, 0, 0)), WithDate(new DateTime(2015, 2, 10, 10, 0, 0, 0)))); // 20 Hours verify("PT20H", ImmutableList.of(WithDate(new DateTime(2016, 2, 10, 11, 0, 0, 0)), WithDate(new DateTime(2016, 2, 9, 11, 0, 0, 0))), ImmutableList.of(WithDate(new DateTime(2016, 2, 11, 8, 0, 0, 0)), WithDate(new DateTime(2016, 2, 11, 9, 0, 0, 0)))); } finally { // Restore time DateTimeUtils.setCurrentMillisSystem(); } }
/** * Converts this LocalDate to a full datetime at midnight using the * specified time zone. * <p> * This method will throw an exception if the time zone switches * to Daylight Savings Time at midnight and this LocalDate represents * that switchover date. The problem is that there is no such time as * midnight on the required date, and as such an exception is thrown. * <p> * This method uses the chronology from this instance plus the time zone * specified. * <p> * This instance is immutable and unaffected by this method call. * * @param zone the zone to use, null means default zone * @return this date as a datetime at midnight * @deprecated Use {@link #toDateTimeAtStartOfDay(DateTimeZone)} which won't throw an exception */ @Deprecated public DateTime toDateTimeAtMidnight(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); Chronology chrono = getChronology().withZone(zone); return new DateTime(getYear(), getMonthOfYear(), getDayOfMonth(), 0, 0, 0, 0, chrono); }
/** * Get this object as a DateTime using the same chronology but a different zone. * * @param zone time zone to apply, or default if null * @return a DateTime using the same millis */ public DateTime toDateTime(DateTimeZone zone) { Chronology chrono = DateTimeUtils.getChronology(getChronology()); chrono = chrono.withZone(zone); return new DateTime(getMillis(), chrono); }
/** * Converts this LocalDate to a full datetime at the earliest valid time * for the date using the specified time zone. * <p> * The time will normally be midnight, as that is the earliest time on * any given day. However, in some time zones when Daylight Savings Time * starts, there is no midnight because time jumps from 11:59 to 01:00. * This method handles that situation by returning 01:00 on that date. * <p> * This method uses the chronology from this instance plus the time zone * specified. * <p> * This instance is immutable and unaffected by this method call. * * @param zone the zone to use, null means default zone * @return this date as a datetime at the start of the day * @since 1.5 */ public DateTime toDateTimeAtStartOfDay(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); Chronology chrono = getChronology().withZone(zone); long localMillis = getLocalMillis() + 6L * DateTimeConstants.MILLIS_PER_HOUR; long instant = zone.convertLocalToUTC(localMillis, false); instant = chrono.dayOfMonth().roundFloor(instant); return new DateTime(instant, chrono); }
/** * Converts this object to a DateTime using the specified zone. * <p> * When the time zone is applied, the local date-time may be affected by daylight saving. * In a daylight saving gap, when the local time does not exist, * this method will throw an exception. * In a daylight saving overlap, when the same local time occurs twice, * this method returns the first occurrence of the local time. * * @param zone time zone to apply, or default if null * @return a DateTime using the same millis * @throws IllegalInstantException if the local time does not exist when the time zone is applied */ public DateTime toDateTime(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); Chronology chrono = iChronology.withZone(zone); return new DateTime( getYear(), getMonthOfYear(), getDayOfMonth(), getHourOfDay(), getMinuteOfHour(), getSecondOfMinute(), getMillisOfSecond(), chrono); }
/** * Constructs an instance set to the local time defined by the specified * instant evaluated using the specified chronology. * <p> * If the chronology is null, ISO chronology in the default zone is used. * Once the constructor is completed, the zone is no longer used. * * @param instant the milliseconds from 1970-01-01T00:00:00Z * @param chronology the chronology, null means ISOChronology in default zone */ public LocalDateTime(long instant, Chronology chronology) { chronology = DateTimeUtils.getChronology(chronology); long localMillis = chronology.getZone().getMillisKeepLocal(DateTimeZone.UTC, instant); iLocalMillis = localMillis; iChronology = chronology.withUTC(); }
/** * Converts this object to an Interval representing the whole day. * <p> * The interval may have more or less than 24 hours if this is a daylight * savings cutover date. * <p> * This instance is immutable and unaffected by this method call. * * @param zone the zone to get the Interval in, null means default * @return a interval over the day */ public Interval toInterval(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); DateTime start = toDateTimeAtStartOfDay(zone); DateTime end = plusDays(1).toDateTimeAtStartOfDay(zone); return new Interval(start, end); }
/** * Gets the millisecond offset to add to UTC to get local time. * * @param instant instant to get the offset for, null means now * @return the millisecond offset to add to UTC to get local time */ public final int getOffset(ReadableInstant instant) { if (instant == null) { return getOffset(DateTimeUtils.currentTimeMillis()); } return getOffset(instant.getMillis()); }
/** * Constructs an instance set to the current system millisecond time * using the specified chronology. * <p> * If the chronology is null, <code>ISOChronology</code> * in the default time zone is used. * * @param chronology the chronology, null means ISOChronology in default zone */ public BaseDateTime(Chronology chronology) { this(DateTimeUtils.currentTimeMillis(), chronology); }