OffsetTime Bridges Zoneless Times
Carry a time-of-day with a fixed UTC offset in OffsetTime, keeping wall-clock through withOffsetSameLocal and the instant through withOffsetSameInstant.
Code
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class DailyCloseTime {
public static void main(String[] args) {
// 营业结束时间 17:30,固定 +08:00:只有时间没有日期,也没有时区规则
OffsetTime close = OffsetTime.of(LocalTime.of(17, 30), ZoneOffset.ofHours(8));
System.out.println("close: " + close);
// withOffsetSameLocal:钟表时间不变,偏移换掉(17:30+08 → 17:30+05)
OffsetTime relabeled = close.withOffsetSameLocal(ZoneOffset.ofHours(5));
System.out.println("same local: " + relabeled);
// withOffsetSameInstant:同一时刻换个偏移表达(17:30+08 → 14:30+05)
OffsetTime sameInstant = close.withOffsetSameInstant(ZoneOffset.ofHours(5));
System.out.println("same instant: " + sameInstant);
// 比较 isAfter/isBefore 时按"同一隐含日期"对齐时刻后再比
OffsetTime earlier = OffsetTime.of(LocalTime.of(9, 0), ZoneOffset.ofHours(-5));
System.out.println("close after earlier instant: " + close.isAfter(earlier));
// 挂上日期升格为 OffsetDateTime
System.out.println("with date: " + close.atDate(java.time.LocalDate.of(2026, 11, 23)));
}
}How it works
The moving parts are the class DailyCloseTime and the function main.
What to watch for
- The toolchain alone is enough — no dependency resolution happens.
Using it
The entry-point signature is public static void main(String[] args)
The demo output is labeled (close:, same local:, same instant:); each label names the call that printed it.
Key statements
OffsetTime earlier = OffsetTime.of(LocalTime.of(9, 0), ZoneOffset.ofHours(-5));
OffsetTime close = OffsetTime.of(LocalTime.of(17, 30), ZoneOffset.ofHours(8));
OffsetTime sameInstant = close.withOffsetSameInstant(ZoneOffset.ofHours(5));
Interactive Tools for This Topic
Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.ISO 8601 Duration ParserParse ISO 8601 durations like PT1H30M into year, month, week, day, and time components with exact or approximate total seconds.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does OffsetTime Bridges Zoneless Times print when run?
- No setup needed: run the file and look for labeled lines like `close:`, `same local:`, `same instant:` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.