下面列出了java.time.LocalTime#getHour ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void test_minusHours_fromZero() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
LocalDate d = base.toLocalDate().plusDays(2);
LocalTime t = LocalTime.of(3, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.minusHours(i);
t = t.minusHours(1);
if (t.getHour() == 23) {
d = d.minusDays(1);
}
assertEquals(dt.toLocalDate(), d, String.valueOf(i));
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_plusHours_fromZero() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
LocalDate d = base.toLocalDate().minusDays(3);
LocalTime t = LocalTime.of(21, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.plusHours(i);
t = t.plusHours(1);
if (t.getHour() == 0) {
d = d.plusDays(1);
}
assertEquals(dt.toLocalDate(), d);
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_minusHours_fromOne() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
LocalDate d = base.toLocalDate().plusDays(2);
LocalTime t = LocalTime.of(4, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.minusHours(i);
t = t.minusHours(1);
if (t.getHour() == 23) {
d = d.minusDays(1);
}
assertEquals(dt.toLocalDate(), d, String.valueOf(i));
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_plusHours_fromOne() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
LocalDate d = base.toLocalDate().minusDays(3);
LocalTime t = LocalTime.of(22, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.plusHours(i);
t = t.plusHours(1);
if (t.getHour() == 0) {
d = d.plusDays(1);
}
assertEquals(dt.toLocalDate(), d);
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_plusHours_fromZero() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
LocalDate d = base.toLocalDate().minusDays(3);
LocalTime t = LocalTime.of(21, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.plusHours(i);
t = t.plusHours(1);
if (t.getHour() == 0) {
d = d.plusDays(1);
}
assertEquals(dt.toLocalDate(), d);
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_minusHours_fromZero() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
LocalDate d = base.toLocalDate().plusDays(2);
LocalTime t = LocalTime.of(3, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.minusHours(i);
t = t.minusHours(1);
if (t.getHour() == 23) {
d = d.minusDays(1);
}
assertEquals(String.valueOf(i), dt.toLocalDate(), d);
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_plusHours_fromOne() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
LocalDate d = base.toLocalDate().minusDays(3);
LocalTime t = LocalTime.of(22, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.plusHours(i);
t = t.plusHours(1);
if (t.getHour() == 0) {
d = d.plusDays(1);
}
assertEquals(dt.toLocalDate(), d);
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_minusHours_fromZero() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
LocalDate d = base.toLocalDate().plusDays(2);
LocalTime t = LocalTime.of(3, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.minusHours(i);
t = t.minusHours(1);
if (t.getHour() == 23) {
d = d.minusDays(1);
}
assertEquals(dt.toLocalDate(), d, String.valueOf(i));
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_minusHours_fromOne() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
LocalDate d = base.toLocalDate().plusDays(2);
LocalTime t = LocalTime.of(4, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.minusHours(i);
t = t.minusHours(1);
if (t.getHour() == 23) {
d = d.minusDays(1);
}
assertEquals(dt.toLocalDate(), d, String.valueOf(i));
assertEquals(dt.toLocalTime(), t);
}
}
@Test
public void test_plusHours_fromOne() {
LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
LocalDate d = base.toLocalDate().minusDays(3);
LocalTime t = LocalTime.of(22, 0);
for (int i = -50; i < 50; i++) {
LocalDateTime dt = base.plusHours(i);
t = t.plusHours(1);
if (t.getHour() == 0) {
d = d.plusDays(1);
}
assertEquals(dt.toLocalDate(), d);
assertEquals(dt.toLocalTime(), t);
}
}
@Override
public boolean isMatchDateTime(ZonedDateTime dateTime) {
IslamicDate islamic = new IslamicDate(dateTime.toLocalDate());
int year = islamic.getYear();
int month = islamic.getMonth();
int day = islamic.getDay();
int size = IslamicDate.getDaySize(year, month);
BitSet days = getDays(size);
LocalTime time = dateTime.toLocalTime();
int hour = time.getHour();
int minute = time.getMinute();
int second = time.getSecond();
if (seconds.get(second) && minutes.get(minute) && hours.get(hour)) {
if (days.get(day) && months.get(month) && years.get(year - IslamicDate.MINIMUM_YEAR)) {
return true;
}
}
return false;
}
protected static AmPmLocalTime convertTo12hFormat(LocalTime time) {
checkNotNull(time, "Unable to convert null value to 12h format");
int hour = time.getHour() == 0 || time.getHour() == 12
? 12 : time.getHour() % 12;
AmPm amPm = time.getHour() < 12
? AmPm.AM : AmPm.PM;
return new AmPmLocalTime(time.withHour(hour), amPm);
}
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_print(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
String formatted = f.format(time);
assertEquals(formatted, text);
}
private static String formatTime(LocalTime time)
{
if (time.getHour() > 0)
{
return time.format(DateTimeFormatter.ofPattern("HH:mm"));
}
else if (time.getMinute() > 9)
{
return time.format(DateTimeFormatter.ofPattern("mm:ss"));
}
else
{
return time.format(DateTimeFormatter.ofPattern("m:ss"));
}
}
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_print(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
String formatted = f.format(time);
assertEquals(formatted, text);
}
private static boolean isEqual(LocalTime lt, java.sql.Time t) {
return lt.getHour() == t.getHours() &&
lt.getMinute() == t.getMinutes() &&
lt.getSecond() == t.getSeconds();
}
int getHourFromLocalTime(LocalTime localTime) {
return localTime.getHour();
}
/**
* Writes the state of the transition rule to the stream.
*
* @param rule the transition rule, not null
* @param out the output stream, not null
* @throws IOException if an error occurs
*/
static void writeRule(ZoneOffsetTransitionRule rule, DataOutput out) throws IOException {
int month = rule.getMonth().getValue();
byte dom = (byte)rule.getDayOfMonthIndicator();
int dow = (rule.getDayOfWeek() == null ? -1 : rule.getDayOfWeek().getValue());
LocalTime time = rule.getLocalTime();
boolean timeEndOfDay = rule.isMidnightEndOfDay();
TimeDefinition timeDefinition = rule.getTimeDefinition();
ZoneOffset standardOffset = rule.getStandardOffset();
ZoneOffset offsetBefore = rule.getOffsetBefore();
ZoneOffset offsetAfter = rule.getOffsetAfter();
int timeSecs = (timeEndOfDay ? 86400 : time.toSecondOfDay());
int stdOffset = standardOffset.getTotalSeconds();
int beforeDiff = offsetBefore.getTotalSeconds() - stdOffset;
int afterDiff = offsetAfter.getTotalSeconds() - stdOffset;
int timeByte = (timeSecs % 3600 == 0 ? (timeEndOfDay ? 24 : time.getHour()) : 31);
int stdOffsetByte = (stdOffset % 900 == 0 ? stdOffset / 900 + 128 : 255);
int beforeByte = (beforeDiff == 0 || beforeDiff == 1800 || beforeDiff == 3600 ? beforeDiff / 1800 : 3);
int afterByte = (afterDiff == 0 || afterDiff == 1800 || afterDiff == 3600 ? afterDiff / 1800 : 3);
int dowByte = (dow == -1 ? 0 : dow);
int b = (month << 28) + // 4 bytes
((dom + 32) << 22) + // 6 bytes
(dowByte << 19) + // 3 bytes
(timeByte << 14) + // 5 bytes
(timeDefinition.ordinal() << 12) + // 2 bytes
(stdOffsetByte << 4) + // 8 bytes
(beforeByte << 2) + // 2 bytes
afterByte; // 2 bytes
out.writeInt(b);
if (timeByte == 31) {
out.writeInt(timeSecs);
}
if (stdOffsetByte == 255) {
out.writeInt(stdOffset);
}
if (beforeByte == 3) {
out.writeInt(offsetBefore.getTotalSeconds());
}
if (afterByte == 3) {
out.writeInt(offsetAfter.getTotalSeconds());
}
}
private void drawTimeSections() {
if (sectionMap.isEmpty()) return;
ZonedDateTime time = tile.getTime();
DayOfWeek day = time.getDayOfWeek();
boolean isAM = time.get(ChronoField.AMPM_OF_DAY) == 0;
double offset = 90;
double angleStep = 360.0 / 60.0;
boolean highlightSections = tile.isHighlightSections();
for (TimeSection section : sectionMap.keySet()) {
LocalTime start = section.getStart();
LocalTime stop = section.getStop();
boolean isStartAM = start.get(ChronoField.AMPM_OF_DAY) == 0;
boolean isStopAM = stop.get(ChronoField.AMPM_OF_DAY) == 0;
boolean draw = isAM ? (isStartAM || isStopAM) : (!isStartAM || !isStopAM);
if (!section.getDays().contains(day)) { draw = false; }
if (!section.isActive()) { draw = false; }
if (draw) {
double sectionStartAngle = (start.getHour() % 12 * 5.0 + start.getMinute() / 12.0 + start.getSecond() / 300.0) * angleStep + 180;
double sectionAngleExtend = ((stop.getHour() - start.getHour()) % 12 * 5.0 + (stop.getMinute() - start.getMinute()) / 12.0 + (stop.getSecond() - start.getSecond()) / 300.0) * angleStep;
if (start.getHour() > stop.getHour()) { sectionAngleExtend = (360.0 - Math.abs(sectionAngleExtend)); }
Arc arc = sectionMap.get(section);
arc.setCenterX(clockSize * 0.5);
arc.setCenterY(clockSize * 0.5);
arc.setRadiusX(clockSize * 0.45);
arc.setRadiusY(clockSize * 0.45);
arc.setStartAngle(-(offset + sectionStartAngle));
arc.setLength(-sectionAngleExtend);
arc.setType(ArcType.OPEN);
arc.setStrokeWidth(clockSize * 0.04);
arc.setStrokeLineCap(StrokeLineCap.BUTT);
arc.setFill(null);
if (highlightSections) {
arc.setStroke(section.contains(time.toLocalTime()) ? section.getHighlightColor() : section.getColor());
} else {
arc.setStroke(section.getColor());
}
}
}
}
private static boolean isEqual(LocalTime lt, java.sql.Time t) {
return lt.getHour() == t.getHours() &&
lt.getMinute() == t.getMinutes() &&
lt.getSecond() == t.getSeconds();
}