SessionTimeUtils
Utilities for session time management. Handles ES futures session times (9:30 AM - 4:00 PM ET).
SessionTimeUtils.java
package com.bookmap.ordermanagement.util;
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* Utilities for session time management.
*
* Handles ES futures session times (9:30 AM - 4:00 PM ET).
*/
public final class SessionTimeUtils {
private static final ZoneId ET_ZONE = ZoneId.of("America/New_York");
private static final LocalTime SESSION_START = LocalTime.of(9, 30);
private static final LocalTime SESSION_END = LocalTime.of(16, 0);
private SessionTimeUtils() {
// Utility class, no instantiation
}
/**
* Returns true if current time is within regular trading hours.
*/
public static boolean isWithinSession() {
LocalTime now = LocalTime.now(ET_ZONE);
return !now.isBefore(SESSION_START) && now.isBefore(SESSION_END);
}
/**
* Returns true if current time is within regular trading hours.
* @param timestamp Unix timestamp in milliseconds
*/
public static boolean isWithinSession(long timestamp) {
LocalTime time = Instant.ofEpochMilli(timestamp)
.atZone(ET_ZONE)
.toLocalTime();
return !time.isBefore(SESSION_START) && time.isBefore(SESSION_END);
}
/**
* Returns the session start time for today.
*/
public static ZonedDateTime getTodaySessionStart() {
return ZonedDateTime.of(LocalDate.now(ET_ZONE), SESSION_START, ET_ZONE);
}
/**
* Returns the session end time for today.
*/
public static ZonedDateTime getTodaySessionEnd() {
return ZonedDateTime.of(LocalDate.now(ET_ZONE), SESSION_END, ET_ZONE);
}
/**
* Formats a timestamp for logging.
*/
public static String formatTimestamp(long timestamp) {
return Instant.ofEpochMilli(timestamp)
.atZone(ET_ZONE)
.format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"));
}
/**
* Returns milliseconds until session start.
*/
public static long getMillisUntilSessionStart() {
ZonedDateTime now = ZonedDateTime.now(ET_ZONE);
ZonedDateTime sessionStart = getTodaySessionStart();
if (now.isAfter(sessionStart)) {
// Already past today's start, return 0
return 0;
}
return Duration.between(now, sessionStart).toMillis();
}
/**
* Returns milliseconds until session end.
*/
public static long getMillisUntilSessionEnd() {
ZonedDateTime now = ZonedDateTime.now(ET_ZONE);
ZonedDateTime sessionEnd = getTodaySessionEnd();
if (now.isAfter(sessionEnd)) {
return 0;
}
return Duration.between(now, sessionEnd).toMillis();
}
/**
* Checks if it's time for session reset.
* Returns true if we just crossed session start (9:30 AM ET) since last check.
*
* @param lastCheckTime The last time we checked (milliseconds since epoch)
* @param currentTime The current time (milliseconds since epoch)
* @return true if session start occurred between lastCheckTime and currentTime
*/
public static boolean shouldResetSession(long lastCheckTime, long currentTime) {
LocalTime lastCheck = Instant.ofEpochMilli(lastCheckTime)
.atZone(ET_ZONE)
.toLocalTime();
LocalTime current = Instant.ofEpochMilli(currentTime)
.atZone(ET_ZONE)
.toLocalTime();
// Check if we crossed 9:30 AM
return lastCheck.isBefore(SESSION_START) && !current.isBefore(SESSION_START);
}
/**
* Returns the current time in Eastern Time zone.
*/
public static ZonedDateTime nowET() {
return ZonedDateTime.now(ET_ZONE);
}
/**
* Returns the current local time in Eastern Time.
*/
public static LocalTime currentTimeET() {
return LocalTime.now(ET_ZONE);
}
/**
* Returns the session start time (9:30 AM ET).
*/
public static LocalTime getSessionStartTime() {
return SESSION_START;
}
/**
* Returns the session end time (4:00 PM ET).
*/
public static LocalTime getSessionEndTime() {
return SESSION_END;
}
}