org.threeten.bp.ZoneId Java Examples

The following examples show how to use org.threeten.bp.ZoneId. 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: TestDateTimeFormatters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider="sample_isoDateTime")
public void test_parse_isoDateTime(
        Integer year, Integer month, Integer day,
        Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId,
        String input, Class<?> invalid) {
    if (input != null) {
        Expected expected = createDateTime(year, month, day, hour, min, sec, nano);
        if (offsetId != null) {
            expected.fieldValues.put(OFFSET_SECONDS, (long) ZoneOffset.of(offsetId).getTotalSeconds());
            if (zoneId != null) {
                expected.zone = ZoneId.of(zoneId);
            }
        }
        assertParseMatch(DateTimeFormatter.ISO_DATE_TIME.parseUnresolved(input, new ParsePosition(0)), expected);
    }
}
 
Example #2
Source File: Chronology.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Obtains a zoned date-time in this chronology from another temporal object.
 * <p>
 * This creates a date-time in this chronology based on the specified {@code TemporalAccessor}.
 * <p>
 * This should obtain a {@code ZoneId} using {@link ZoneId#from(TemporalAccessor)}.
 * The date-time should be obtained by obtaining an {@code Instant}.
 * If that fails, the local date-time should be used.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zoned date-time in this chronology, not null
 * @throws DateTimeException if unable to create the date-time
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public ChronoZonedDateTime<?> zonedDateTime(TemporalAccessor temporal) {
    try {
        ZoneId zone = ZoneId.from(temporal);
        try {
            Instant instant = Instant.from(temporal);
            return zonedDateTime(instant, zone);

        } catch (DateTimeException ex1) {
            ChronoLocalDateTime cldt = localDateTime(temporal);
            ChronoLocalDateTimeImpl cldtImpl = ensureChronoLocalDateTime(cldt);
            return ChronoZonedDateTimeImpl.ofBest(cldtImpl, zone, null);
        }
    } catch (DateTimeException ex) {
        throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass(), ex);
    }
}
 
Example #3
Source File: CustomInstantDeserializer.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
protected CustomInstantDeserializer(Class<T> supportedType,
                  DateTimeFormatter parser,
                  Function<TemporalAccessor, T> parsedToValue,
                  Function<FromIntegerArguments, T> fromMilliseconds,
                  Function<FromDecimalArguments, T> fromNanoseconds,
                  BiFunction<T, ZoneId, T> adjust) {
  super(supportedType, parser);
  this.parsedToValue = parsedToValue;
  this.fromMilliseconds = fromMilliseconds;
  this.fromNanoseconds = fromNanoseconds;
  this.adjust = adjust == null ? new BiFunction<T, ZoneId, T>() {
    @Override
    public T apply(T t, ZoneId zoneId) {
      return t;
    }
  } : adjust;
}
 
Example #4
Source File: CustomInstantDeserializer.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
protected CustomInstantDeserializer(Class<T> supportedType,
                  DateTimeFormatter parser,
                  Function<TemporalAccessor, T> parsedToValue,
                  Function<FromIntegerArguments, T> fromMilliseconds,
                  Function<FromDecimalArguments, T> fromNanoseconds,
                  BiFunction<T, ZoneId, T> adjust) {
  super(supportedType, parser);
  this.parsedToValue = parsedToValue;
  this.fromMilliseconds = fromMilliseconds;
  this.fromNanoseconds = fromNanoseconds;
  this.adjust = adjust == null ? new BiFunction<T, ZoneId, T>() {
    @Override
    public T apply(T t, ZoneId zoneId) {
      return t;
    }
  } : adjust;
}
 
Example #5
Source File: ChronoZonedDateTimeImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static ChronoZonedDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    ChronoLocalDateTime<?> dateTime = (ChronoLocalDateTime<?>) in.readObject();
    ZoneOffset offset = (ZoneOffset) in.readObject();
    ZoneId zone = (ZoneId) in.readObject();
    return dateTime.atZone(offset).withZoneSameLocal(zone);
    // TODO: ZDT uses ofLenient()
}
 
Example #6
Source File: DateTimeBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void mergeInstantFields0(ZoneId selectedZone) {
    Instant instant = Instant.ofEpochSecond(fieldValues.remove(INSTANT_SECONDS));
    ChronoZonedDateTime<?> zdt = chrono.zonedDateTime(instant, selectedZone);
    if (date == null) {
        addObject(zdt.toLocalDate());
    } else {
        resolveMakeChanges(INSTANT_SECONDS, zdt.toLocalDate());
    }
    addFieldValue(SECOND_OF_DAY, (long) zdt.toLocalTime().toSecondOfDay());
}
 
Example #7
Source File: Invoice.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setPlannedPaymentDate(LocalDate plannedPaymentDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  plannedPaymentDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.plannedPaymentDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #8
Source File: Employee.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setTerminationDate(LocalDate terminationDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  terminationDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.terminationDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #9
Source File: ChronoZonedDateTimeImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Jdk8Methods.requireNonNull(localDateTime, "localDateTime");
    Jdk8Methods.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<R>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Jdk8Methods.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<R>(localDateTime, offset, zone);
}
 
Example #10
Source File: Schedule.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setStartDate(LocalDate startDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  startDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.startDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #11
Source File: Employee.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setDateOfBirth(LocalDate dateOfBirth) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  dateOfBirth.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.dateOfBirth = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #12
Source File: HistoryStat.java    From ankihelper with GNU General Public License v3.0 5 votes vote down vote up
public int[][] getHourStatistics(){
    int[][] result = new int[3][24];
    for(HistoryPOJO history : dataOfLastDays){
        long mills = history.getTimeStamp();
        int type = history.getType();
        int hour = LocalDateTime.ofInstant(Instant.ofEpochMilli(mills), ZoneId.systemDefault()).getHour();
        result[type][hour] += 1;
    }
    return result;
}
 
Example #13
Source File: TestZoneIdParser.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_parse_lowerCase() throws Exception {
    ZoneIdPrinterParser pp = new ZoneIdPrinterParser(TemporalQueries.zoneId(), null);
    parseContext.setCaseSensitive(false);
    int result = pp.parse(parseContext, "europe/london", 0);
    assertEquals(result, 13);
    assertParsed(ZoneId.of("Europe/London"));
}
 
Example #14
Source File: OpeningBalances.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setOpeningBalanceDate(LocalDate openingBalanceDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  openingBalanceDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.openingBalanceDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #15
Source File: DateTimeFormatterBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean print(DateTimePrintContext context, StringBuilder buf) {
    ZoneId zone = context.getValue(query);
    if (zone == null) {
        return false;
    }
    buf.append(zone.getId());
    return true;
}
 
Example #16
Source File: PayRun.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setPayRunPeriodEndDate(LocalDate payRunPeriodEndDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  payRunPeriodEndDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.payRunPeriodEndDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #17
Source File: PurchaseOrder.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setExpectedArrivalDate(LocalDate expectedArrivalDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  expectedArrivalDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.expectedArrivalDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #18
Source File: LeaveApplication.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setStartDate(LocalDate startDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  startDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.startDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #19
Source File: TestYearMonth.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void now_ZoneId() {
    ZoneId zone = ZoneId.of("UTC+01:02:03");
    YearMonth expected = YearMonth.now(Clock.system(zone));
    YearMonth test = YearMonth.now(zone);
    for (int i = 0; i < 100; i++) {
        if (expected.equals(test)) {
            return;
        }
        expected = YearMonth.now(Clock.system(zone));
        test = YearMonth.now(zone);
    }
    assertEquals(test, expected);
}
 
Example #20
Source File: Schedule.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setNextScheduledDate(LocalDate nextScheduledDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  nextScheduledDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.nextScheduledDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #21
Source File: TestYear.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void now_ZoneId() {
    ZoneId zone = ZoneId.of("UTC+01:02:03");
    Year expected = Year.now(Clock.system(zone));
    Year test = Year.now(zone);
    for (int i = 0; i < 100; i++) {
        if (expected.equals(test)) {
            return;
        }
        expected = Year.now(Clock.system(zone));
        test = Year.now(zone);
    }
    assertEquals(test, expected);
}
 
Example #22
Source File: LazyThreeTen.java    From lazythreetenbp with Apache License 2.0 5 votes vote down vote up
/**
 * Call on background thread to eagerly load all zones. Starts with loading
 * {@link ZoneId#systemDefault()} which is the one most likely to be used.
 */
@WorkerThread
public static void cacheZones() {
    ZoneId.systemDefault().getRules();
    for (String zoneId : ZoneRulesProvider.getAvailableZoneIds()) {
        ZoneRulesProvider.getRules(zoneId, true);
    }
}
 
Example #23
Source File: BatchPayment.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setDate(LocalDate date) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  date.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.date = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #24
Source File: PayrollCalendar.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setStartDate(LocalDate startDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  startDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.startDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #25
Source File: Prepayment.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setDate(LocalDate date) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  date.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.date = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #26
Source File: PayrollCalendar.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setPaymentDate(LocalDate paymentDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  paymentDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.paymentDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #27
Source File: BankTransaction.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setDate(LocalDate date) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  date.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.date = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #28
Source File: Invoice.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setExpectedPaymentDate(LocalDate expectedPaymentDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  expectedPaymentDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.expectedPaymentDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #29
Source File: CreditNote.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setFullyPaidOnDate(LocalDate fullyPaidOnDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  fullyPaidOnDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.fullyPaidOnDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}
 
Example #30
Source File: Journal.java    From Xero-Java with MIT License 5 votes vote down vote up
public void setJournalDate(LocalDate journalDate) {
  //CONVERT LocalDate args into MS DateFromat String
  Instant instant =  journalDate.atStartOfDay(ZoneId.of("UTC").normalized()).toInstant();  
  long timeInMillis = instant.toEpochMilli();

  this.journalDate = "/Date(" + Long.toString(timeInMillis) + "+0000)/";
}