org.joda.time.DateTime.withZone()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(241)

本文整理了Java中org.joda.time.DateTime.withZone()方法的一些代码示例,展示了DateTime.withZone()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTime.withZone()方法的具体详情如下:
包路径:org.joda.time.DateTime
类名称:DateTime
方法名:withZone

DateTime.withZone介绍

[英]Returns a copy of this datetime with a different time zone, preserving the millisecond instant.

This method is useful for finding the local time in another timezone. For example, if this instant holds 12:30 in Europe/London, the result from this method with Europe/Paris would be 13:30.

The returned object will be a new instance of the same implementation type. This method changes the time zone, and does not change the millisecond instant, with the effect that the field values usually change. The returned object will be either be a new instance or this.
[中]返回具有不同时区的此datetime的副本,保留毫秒瞬间。
此方法对于在另一时区中查找本地时间非常有用。例如,如果此瞬间在欧洲/伦敦保持12:30,则此方法对欧洲/巴黎的结果将为13:30。
返回的对象将是相同实现类型的新实例。此方法会更改时区,而不会更改毫秒瞬间,其效果是字段值通常会更改。返回的对象将是新实例或this

代码示例

代码示例来源:origin: apache/incubator-druid

/**
 * Calcite expects "TIMESTAMP" types to be an instant that has the expected local time fields if printed as UTC.
 *
 * @param dateTime joda timestamp
 * @param timeZone session time zone
 *
 * @return Calcite style millis
 */
public static long jodaToCalciteTimestamp(final DateTime dateTime, final DateTimeZone timeZone)
{
 return dateTime.withZone(timeZone).withZoneRetainFields(DateTimeZone.UTC).getMillis();
}

代码示例来源:origin: stackoverflow.com

DateTimeZone timeZoneLondon = DateTimeZone.forID( "Europe/London" );
DateTimeZone timeZoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );
DateTimeZone timeZoneNewYork = DateTimeZone.forID( "America/New_York" );

DateTime nowLondon = DateTime.now( timeZoneLondon ); // Assign a time zone rather than rely on implicit default time zone.
DateTime nowKolkata = nowLondon.withZone( timeZoneKolkata );
DateTime nowNewYork = nowLondon.withZone( timeZoneNewYork );
DateTime nowUtc = nowLondon.withZone( DateTimeZone.UTC );  // Built-in constant for UTC.

代码示例来源:origin: Graylog2/graylog2-server

@Override
  public void serialize(DateTime dateTime,
             JsonGenerator jsonGenerator,
             SerializerProvider serializerProvider) throws IOException {
    final Date date = dateTime.withZone(DateTimeZone.UTC).toDate();
    jsonGenerator.writeObject(date);
  }
}

代码示例来源:origin: apache/incubator-druid

/**
 * Calcite expects "DATE" types to be number of days from the epoch to the UTC date matching the local time fields.
 *
 * @param dateTime joda timestamp
 * @param timeZone session time zone
 *
 * @return Calcite style date
 */
public static int jodaToCalciteDate(final DateTime dateTime, final DateTimeZone timeZone)
{
 final DateTime date = dateTime.withZone(timeZone).dayOfMonth().roundFloorCopy();
 return Days.daysBetween(DateTimes.EPOCH, date.withZoneRetainFields(DateTimeZone.UTC)).getDays();
}

代码示例来源:origin: stackoverflow.com

String input = "2014-01-02T03:04:05";
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( input, timeZone );
DateTime dateTimeUtcGmt = dateTimeIndia.withZone( DateTimeZone.UTC );

代码示例来源:origin: apache/incubator-druid

/**
 * Calcite expects DATE literals to be represented by DateStrings in the local time zone.
 *
 * @param dateTime joda timestamp
 * @param timeZone session time zone
 *
 * @return Calcite style Calendar, appropriate for literals
 */
public static DateString jodaToCalciteDateString(final DateTime dateTime, final DateTimeZone timeZone)
{
 return new DateString(CALCITE_DATE_PRINTER.print(dateTime.withZone(timeZone)));
}

代码示例来源:origin: apache/incubator-druid

/**
 * Calcite expects TIMESTAMP literals to be represented by TimestampStrings in the local time zone.
 *
 * @param dateTime joda timestamp
 * @param timeZone session time zone
 *
 * @return Calcite style Calendar, appropriate for literals
 */
public static TimestampString jodaToCalciteTimestampString(final DateTime dateTime, final DateTimeZone timeZone)
{
 // The replaceAll is because Calcite doesn't like trailing zeroes in its fractional seconds part.
 String timestampString = TRAILING_ZEROS
   .matcher(CALCITE_TIMESTAMP_PRINTER.print(dateTime.withZone(timeZone)))
   .replaceAll("");
 return new TimestampString(timestampString);
}

代码示例来源:origin: apache/incubator-druid

/**
 * Calcite expects TIME literals to be represented by TimeStrings in the local time zone.
 *
 * @param dateTime joda timestamp
 * @param timeZone session time zone
 *
 * @return Calcite style Calendar, appropriate for literals
 */
public static TimeString jodaToCalciteTimeString(final DateTime dateTime, final DateTimeZone timeZone)
{
 // The replaceAll is because Calcite doesn't like trailing zeroes in its fractional seconds part.
 String timeString = TRAILING_ZEROS
   .matcher(CALCITE_TIME_PRINTER.print(dateTime.withZone(timeZone)))
   .replaceAll("");
 return new TimeString(timeString);
}

代码示例来源:origin: Graylog2/graylog2-server

public DateTime getTimestamp() {
  return getFieldAs(DateTime.class, FIELD_TIMESTAMP).withZone(UTC);
}

代码示例来源:origin: azkaban/azkaban

private DateTime getNextRuntime(final long scheduleTime, final DateTimeZone timezone,
  final ReadablePeriod period) {
 final DateTime now = new DateTime();
 DateTime date = new DateTime(scheduleTime).withZone(timezone);
 int count = 0;
 while (!now.isBefore(date)) {
  if (count > 100000) {
   throw new IllegalStateException(
     "100000 increments of period did not get to present time.");
  }
  if (period == null) {
   break;
  } else {
   date = date.plus(period);
  }
  count += 1;
 }
 return date;
}

代码示例来源:origin: azkaban/azkaban

/**
 * @param scheduleTime represents the time when Schedule Servlet receives the Cron Schedule API
 * call.
 * @param timezone is always UTC (after 3.1.0)
 * @return the First Scheduled DateTime to run this flow.
 */
private DateTime getNextCronRuntime(final long scheduleTime, final DateTimeZone timezone,
  final CronExpression ce) {
 Date date = new DateTime(scheduleTime).withZone(timezone).toDate();
 if (ce != null) {
  date = ce.getNextValidTimeAfter(date);
 }
 return new DateTime(date);
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * Convert timestamp in a string format to joda time
 * @param input timestamp
 * @param format timestamp format
 * @param timezone time zone of timestamp
 * @return joda time
 */
public static DateTime toDateTime(String input, String format, String timezone) {
 String tz = StringUtils.defaultString(timezone, ConfigurationKeys.DEFAULT_SOURCE_TIMEZONE);
 DateTimeZone dateTimeZone = getTimeZone(tz);
 DateTimeFormatter inputDtFormat = DateTimeFormat.forPattern(format).withZone(dateTimeZone);
 DateTime outputDateTime = inputDtFormat.parseDateTime(input).withZone(dateTimeZone);
 return outputDateTime;
}

代码示例来源:origin: azkaban/azkaban

private long calculateNextCheckTime() {
 DateTime date = new DateTime(this.nextCheckTime).withZone(this.timezone);
 int count = 0;
 while (!date.isAfterNow()) {
  if (count > 100000) {
   throw new IllegalStateException(
     "100000 increments of period did not get to present time.");
  }
  if (this.period == null && this.cronExpression == null) {
   break;
  } else if (this.cronExecutionTime != null) {
   final Date nextDate = this.cronExecutionTime.getNextValidTimeAfter(date.toDate());
   // Some Cron Expressions possibly do not have follow-up occurrences
   if (nextDate != null) {
    date = new DateTime(nextDate);
   } else {
    break;
   }
  } else {
   date = date.plus(this.period);
  }
  count += 1;
 }
 return date.getMillis();
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
  public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    switch (jsonParser.currentToken()) {
      case VALUE_EMBEDDED_OBJECT:
        final Object embeddedObject = jsonParser.getEmbeddedObject();
        if (embeddedObject instanceof Date) {
          final Date date = (Date) embeddedObject;
          return new DateTime(date, DateTimeZone.UTC);
        } else {
          throw new IllegalStateException("Unsupported token: " + jsonParser.currentToken());
        }
      case VALUE_STRING:
        final String text = jsonParser.getText();
        return DateTime.parse(text, FORMATTER).withZone(DateTimeZone.UTC);
      default:
        throw new IllegalStateException("Unsupported token: " + jsonParser.currentToken());
    }
  }
}

代码示例来源:origin: apache/incubator-druid

macroTable,
plannerConfig.withOverrides(queryContext),
utcNow.withZone(timeZone),
queryContext,
authenticationResult

代码示例来源:origin: apache/avro

@Test(expected = MissingSchemaException.class)
 public void testSpecificByteArrayIncompatibleWithoutLogicalTypes() throws IOException {
  TestRecordWithLogicalTypes withLogicalTypes = new TestRecordWithLogicalTypes(
    true,
    34,
    35L,
    3.14F,
    3019.34,
    null,
    LocalDate.now(),
    LocalTime.now(),
    DateTime.now().withZone(DateTimeZone.UTC),
    new BigDecimal("123.45")
  );

  ByteBuffer b = withLogicalTypes.toByteBuffer();
  TestRecordWithoutLogicalTypes.fromByteBuffer(b);
 }
}

代码示例来源:origin: apache/avro

@Test
public void testSpecificToFromByteBufferWithLogicalTypes() throws IOException {
 TestRecordWithLogicalTypes record = new TestRecordWithLogicalTypes(
   true,
   34,
   35L,
   3.14F,
   3019.34,
   null,
   LocalDate.now(),
   LocalTime.now(),
   DateTime.now().withZone(DateTimeZone.UTC),
   new BigDecimal("123.45")
 );
 ByteBuffer b = record.toByteBuffer();
 TestRecordWithLogicalTypes copy = TestRecordWithLogicalTypes.fromByteBuffer(b);
 assertEquals(record, copy);
}

代码示例来源:origin: apache/avro

@Test
public void testRecordWithLogicalTypes() throws IOException {
 TestRecordWithLogicalTypes record = new TestRecordWithLogicalTypes(
   true,
   34,
   35L,
   3.14F,
   3019.34,
   null,
   LocalDate.now(),
   LocalTime.now(),
   DateTime.now().withZone(DateTimeZone.UTC),
   new BigDecimal(123.45f).setScale(2, RoundingMode.HALF_DOWN)
 );
 File data = write(TestRecordWithLogicalTypes.getClassSchema(), record);
 List<TestRecordWithLogicalTypes> actual = read(
   TestRecordWithLogicalTypes.getClassSchema(), data);
 Assert.assertEquals("Should match written record", record, actual.get(0));
}
@Test

代码示例来源:origin: apache/avro

@Test(expected = MissingSchemaException.class)
public void testSpecificByteArrayIncompatibleWithLogicalTypes() throws IOException {
 TestRecordWithoutLogicalTypes withoutLogicalTypes = new TestRecordWithoutLogicalTypes(
   true,
   34,
   35L,
   3.14F,
   3019.34,
   null,
   new TimeConversions.DateConversion().toInt(LocalDate.now(), null, null),
   new TimeConversions.TimeConversion().toInt(LocalTime.now(), null, null),
   new TimeConversions.TimestampConversion().toLong(
     DateTime.now().withZone(DateTimeZone.UTC), null, null),
   new Conversions.DecimalConversion().toBytes(
     new BigDecimal("123.45"), null, LogicalTypes.decimal(9, 2))
 );
 ByteBuffer b = withoutLogicalTypes.toByteBuffer();
 TestRecordWithLogicalTypes.fromByteBuffer(b);
}

代码示例来源:origin: apache/avro

@Test
public void testSpecificToFromByteBufferWithoutLogicalTypes() throws IOException {
 TestRecordWithoutLogicalTypes record = new TestRecordWithoutLogicalTypes(
   true,
   34,
   35L,
   3.14F,
   3019.34,
   null,
   new TimeConversions.DateConversion().toInt(LocalDate.now(), null, null),
   new TimeConversions.TimeConversion().toInt(LocalTime.now(), null, null),
   new TimeConversions.TimestampConversion().toLong(
     DateTime.now().withZone(DateTimeZone.UTC), null, null),
   new Conversions.DecimalConversion().toBytes(
     new BigDecimal("123.45"), null, LogicalTypes.decimal(9, 2))
 );
 ByteBuffer b = record.toByteBuffer();
 TestRecordWithoutLogicalTypes copy = TestRecordWithoutLogicalTypes.fromByteBuffer(b);
 assertEquals(record, copy);
}

相关文章

DateTime类方法