本文整理汇总了Java中java.time.ZoneOffset类的典型用法代码示例。如果您正苦于以下问题:Java ZoneOffset类的具体用法?Java ZoneOffset怎么用?Java ZoneOffset使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZoneOffset类属于java.time包,在下文中一共展示了ZoneOffset类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: now_Clock_allSecsInDay_utc
import java.time.ZoneOffset; //导入依赖的package包/类
@Test
public void now_Clock_allSecsInDay_utc() {
for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
OffsetDateTime test = OffsetDateTime.now(clock);
assertEquals(test.getYear(), 1970);
assertEquals(test.getMonth(), Month.JANUARY);
assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
assertEquals(test.getHour(), (i / (60 * 60)) % 24);
assertEquals(test.getMinute(), (i / 60) % 60);
assertEquals(test.getSecond(), i % 60);
assertEquals(test.getNano(), 123456789);
assertEquals(test.getOffset(), ZoneOffset.UTC);
}
}
示例2: test_JapaneseChrono_vsCalendar
import java.time.ZoneOffset; //导入依赖的package包/类
@Test(dataProvider="RangeVersusCalendar")
public void test_JapaneseChrono_vsCalendar(LocalDate isoStartDate, LocalDate isoEndDate) {
Locale locale = Locale.forLanguageTag("ja-JP-u-ca-japanese");
assertEquals(locale.toString(), "ja_JP_#u-ca-japanese", "Unexpected locale");
Calendar cal = java.util.Calendar.getInstance(locale);
assertEquals(cal.getCalendarType(), "japanese", "Unexpected calendar type");
JapaneseDate jDate = JapaneseChronology.INSTANCE.date(isoStartDate);
// Convert to millis and set Japanese Calendar to that start date (at GMT)
OffsetDateTime jodt = OffsetDateTime.of(isoStartDate, LocalTime.MIN, ZoneOffset.UTC);
long millis = jodt.toInstant().toEpochMilli();
cal.setTimeZone(TimeZone.getTimeZone("GMT+00"));
cal.setTimeInMillis(millis);
while (jDate.isBefore(isoEndDate)) {
assertEquals(jDate.get(ChronoField.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH), "Day mismatch in " + jDate + "; cal: " + cal);
assertEquals(jDate.get(ChronoField.MONTH_OF_YEAR), cal.get(Calendar.MONTH) + 1, "Month mismatch in " + jDate);
assertEquals(jDate.get(ChronoField.YEAR_OF_ERA), cal.get(Calendar.YEAR), "Year mismatch in " + jDate);
jDate = jDate.plus(1, ChronoUnit.DAYS);
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
示例3: create
import java.time.ZoneOffset; //导入依赖的package包/类
/**
* Create a graph
* @param goalInKg the goal set
* @param measurements the measurement data
* @return a chart
*/
public Chart create(BigDecimal goalInKg, Iterable<WeightMeasurement> measurements) {
BigDecimal minValue = goalInKg;
BigDecimal maxValue = goalInKg;
List<List<Object>> dataPoints = new ArrayList<>();
for (WeightMeasurement wm : measurements) {
if (minValue == null) {
minValue = wm.getWeightInKg();
} else {
minValue = minValue.min(wm.getWeightInKg());
}
if (maxValue == null) {
maxValue = wm.getWeightInKg();
} else {
maxValue = maxValue.max(wm.getWeightInKg());
}
dataPoints.add(Lists.newArrayList(wm.getDateTime().toInstant(ZoneOffset.UTC).toEpochMilli(),wm.getWeightInKg()));
}
if (minValue != null) {
minValue = minValue.subtract(BigDecimal.valueOf(1));
}
if (maxValue != null) {
maxValue = maxValue.add(BigDecimal.valueOf(1));
}
return Chart.of(minValue, maxValue, goalInKg, dataPoints, calculareTrend(dataPoints));
}
示例4: test_factory_string_hours_minutes_noColon
import java.time.ZoneOffset; //导入依赖的package包/类
@Test
public void test_factory_string_hours_minutes_noColon() {
for (int i = -17; i <= 17; i++) {
for (int j = -59; j <= 59; j++) {
if ((i < 0 && j <= 0) || (i > 0 && j >= 0) || i == 0) {
String str = (i < 0 || j < 0 ? "-" : "+") +
Integer.toString(Math.abs(i) + 100).substring(1) +
Integer.toString(Math.abs(j) + 100).substring(1);
ZoneOffset test = ZoneOffset.of(str);
doTestOffset(test, i, j, 0);
}
}
}
ZoneOffset test1 = ZoneOffset.of("-1800");
doTestOffset(test1, -18, 0, 0);
ZoneOffset test2 = ZoneOffset.of("+1800");
doTestOffset(test2, 18, 0, 0);
}
示例5: getPost
import java.time.ZoneOffset; //导入依赖的package包/类
public static Node getPost(Node author, Long time) {
LocalDateTime postedDateTime = LocalDateTime.ofEpochSecond(time, 0, ZoneOffset.UTC);
RelationshipType original = RelationshipType.withName("POSTED_ON_" +
postedDateTime.format(dateFormatter));
Node post = null;
for(Relationship r1 : author.getRelationships(Direction.OUTGOING, original)) {
Node potential = r1.getEndNode();
if (time.equals(potential.getProperty(TIME))) {
post = potential;
break;
}
}
if(post == null) { throw PostExceptions.postNotFound; };
return post;
}
示例6: testLoad
import java.time.ZoneOffset; //导入依赖的package包/类
@Test
public void testLoad() throws Exception {
new ExecuteAsTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC))
.run(() -> assertThat(
load("2017-08-28T07:09:36.000000042Z")
.isEqual(OffsetDateTime.of(2017, 8, 28, 7, 9, 36, 42, ZoneOffset.UTC)),
is(true)
));
}
示例7: test_badPlusTemporalUnitChrono
import java.time.ZoneOffset; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_badPlusTemporalUnitChrono(Chronology chrono) {
LocalDate refDate = LocalDate.of(2013, 1, 1);
ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
for (Chronology[] clist : data_of_calendars()) {
Chronology chrono2 = clist[0];
ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
TemporalUnit adjuster = new FixedTemporalUnit(czdt2);
if (chrono != chrono2) {
try {
czdt.plus(1, adjuster);
Assert.fail("TemporalUnit.doPlus plus should have thrown a ClassCastException, " + czdt
+ " can not be cast to " + czdt2);
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoZonedDateTime<?> result = czdt.plus(1, adjuster);
assertEquals(result, czdt2, "WithAdjuster failed to replace date");
}
}
}
示例8: convert_willHandleCollectionOfOffsetDateTimeValues_whenValuesHaveOffset
import java.time.ZoneOffset; //导入依赖的package包/类
@Test
public void convert_willHandleCollectionOfOffsetDateTimeValues_whenValuesHaveOffset() {
OffsetDateTime time1 = OffsetDateTime.of(2017, 10, 11, 17, 24, 36, 123456789, ZoneOffset.ofHours(10));
OffsetDateTime time2 = OffsetDateTime.of(2017, 10, 15, 16, 28, 40, 123456789, ZoneOffset.ofHours(10));
assertThat(convertCollection(
Arrays.asList(time1, time2)
)).isEqualTo(
Arrays.asList("2017-10-11", "2017-10-15")
);
assertThat(convertCollection(
new OffsetDateTime[]{time1, time2}
)).isEqualTo(
Arrays.asList("2017-10-11", "2017-10-15")
);
}
示例9: test_London_getOffsetInfo_gap
import java.time.ZoneOffset; //导入依赖的package包/类
public void test_London_getOffsetInfo_gap() {
ZoneRules test = europeLondon();
final LocalDateTime dateTime = LocalDateTime.of(2008, 3, 30, 1, 0, 0, 0);
ZoneOffsetTransition trans = checkOffset(test, dateTime, OFFSET_ZERO, GAP);
assertEquals(trans.isGap(), true);
assertEquals(trans.isOverlap(), false);
assertEquals(trans.getOffsetBefore(), OFFSET_ZERO);
assertEquals(trans.getOffsetAfter(), OFFSET_PONE);
assertEquals(trans.getInstant(), createInstant(2008, 3, 30, 1, 0, ZoneOffset.UTC));
assertEquals(trans.getDateTimeBefore(), LocalDateTime.of(2008, 3, 30, 1, 0));
assertEquals(trans.getDateTimeAfter(), LocalDateTime.of(2008, 3, 30, 2, 0));
assertEquals(trans.isValidOffset(OFFSET_ZERO), false);
assertEquals(trans.isValidOffset(OFFSET_PONE), false);
assertEquals(trans.isValidOffset(OFFSET_PTWO), false);
assertEquals(trans.toString(), "Transition[Gap at 2008-03-30T01:00Z to +01:00]");
assertFalse(trans.equals(null));
assertFalse(trans.equals(OFFSET_ZERO));
assertTrue(trans.equals(trans));
final ZoneOffsetTransition otherTrans = test.getTransition(dateTime);
assertTrue(trans.equals(otherTrans));
assertEquals(trans.hashCode(), otherTrans.hashCode());
}
示例10: testNavDate
import java.time.ZoneOffset; //导入依赖的package包/类
@Test
public void testNavDate() throws IOException {
OffsetDateTime navDate = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
Manifest manifest = new Manifest("http://some.uri", "A label for the Manifest");
manifest.setNavDate(navDate);
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(manifest);
DocumentContext ctx = JsonPath.parse(json);
JsonPathAssert.assertThat(ctx).jsonPathAsString("navDate").isEqualTo("1970-01-01T00:00:00Z");
assertThat(mapper.readValue(json, Manifest.class).getNavDate()).isEqualTo(manifest.getNavDate());
Collection coll = new Collection("http://some.uri", "A label for the Collection");
coll.setNavDate(navDate);
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(coll);
ctx = JsonPath.parse(json);
JsonPathAssert.assertThat(ctx).jsonPathAsString("navDate").isEqualTo("1970-01-01T00:00:00Z");
assertThat(mapper.readValue(json, Collection.class).getNavDate()).isEqualTo(navDate);
}
示例11: doTest_factory_ofInstant_all
import java.time.ZoneOffset; //导入依赖的package包/类
private void doTest_factory_ofInstant_all(long minYear, long maxYear) {
long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
int minOffset = (minYear <= 0 ? 0 : 3);
int maxOffset = (maxYear <= 0 ? 0 : 3);
long minDays = (minYear * 365L + ((minYear + minOffset) / 4L - (minYear + minOffset) / 100L + (minYear + minOffset) / 400L)) - days_0000_to_1970;
long maxDays = (maxYear * 365L + ((maxYear + maxOffset) / 4L - (maxYear + maxOffset) / 100L + (maxYear + maxOffset) / 400L)) + 365L - days_0000_to_1970;
final LocalDate maxDate = LocalDate.of(Year.MAX_VALUE, 12, 31);
OffsetDateTime expected = OffsetDateTime.of(LocalDate.of((int) minYear, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
for (long i = minDays; i < maxDays; i++) {
Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
try {
OffsetDateTime test = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
assertEquals(test, expected);
if (expected.toLocalDate().equals(maxDate) == false) {
expected = expected.plusDays(1);
}
} catch (RuntimeException|Error ex) {
System.out.println("Error: " + i + " " + expected);
throw ex;
}
}
}
示例12: setUp
import java.time.ZoneOffset; //导入依赖的package包/类
@BeforeMethod
public void setUp() {
TEST_2007_07_15 = LocalDate.of(2007, 7, 15);
LocalDate max = LocalDate.MAX;
LocalDate min = LocalDate.MIN;
MAX_VALID_EPOCHDAYS = max.toEpochDay();
MIN_VALID_EPOCHDAYS = min.toEpochDay();
MAX_DATE = max;
MIN_DATE = min;
MAX_INSTANT = max.atStartOfDay(ZoneOffset.UTC).toInstant();
MIN_INSTANT = min.atStartOfDay(ZoneOffset.UTC).toInstant();
}
示例13: test_badMinusTemporalUnitChrono
import java.time.ZoneOffset; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_badMinusTemporalUnitChrono(Chronology chrono) {
LocalDate refDate = LocalDate.of(2013, 1, 1);
ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
for (Chronology[] clist : data_of_calendars()) {
Chronology chrono2 = clist[0];
ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
TemporalUnit adjuster = new FixedTemporalUnit(czdt2);
if (chrono != chrono2) {
try {
czdt.minus(1, adjuster);
Assert.fail("TemporalUnit.doPlus minus should have thrown a ClassCastException, " + czdt.getClass()
+ " can not be cast to " + czdt2.getClass());
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoZonedDateTime<?> result = czdt.minus(1, adjuster);
assertEquals(result, czdt2, "WithAdjuster failed to replace date");
}
}
}
示例14: onMouseDrag
import java.time.ZoneOffset; //导入依赖的package包/类
@Override
public boolean onMouseDrag(MouseEvent e, double x, double y) {
view.preparePopup(this.toString());
dragged = true;
if (isMoving) {
long mins = (endTime.toEpochSecond(ZoneOffset.UTC) - startTime.toEpochSecond(ZoneOffset.UTC)) / 60;
startTime = view.roundLocalDateTime(view.getMouseLocalDateTime().plusMinutes(minsOffset));
endTime = startTime.plusMinutes(mins);
} else {
LocalDateTime newEndTime = view.roundLocalDateTime(view.getMouseLocalDateTime());
if (newEndTime.isAfter(startTime)) {
endTime = newEndTime;
}
}
return true;
}
示例15: testParsingCustomFormattedStringToDate
import java.time.ZoneOffset; //导入依赖的package包/类
@Test
public final void testParsingCustomFormattedStringToDate() {
MatcherAssert.assertThat(
"Can't parse a Date with custom format.",
new DateOf(
"2017-12-13 14:15:16.000000017",
"yyyy-MM-dd HH:mm:ss.n"
).value(),
Matchers.is(
Date.from(
LocalDateTime.of(
2017, 12, 13, 14, 15, 16, 17
).toInstant(ZoneOffset.UTC)
)
)
);
}