Class SubscriptionDateUtils

java.lang.Object
com.broadleafcommerce.subscriptionoperation.service.SubscriptionDateUtils

public class SubscriptionDateUtils extends Object
An util class for time related calculations.
Author:
Sunny Yu
  • Constructor Details

    • SubscriptionDateUtils

      public SubscriptionDateUtils()
  • Method Details

    • getCustomerTimeZone

      @Nullable public static String getCustomerTimeZone(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
      Gets the customer timezone from the given Cart if specified.
      Parameters:
      cart - the Cart to get the timezone from
      Returns:
      the customer timezone from the given Cart, if specified
    • getCustomerTimeZone

      @Nullable public static String getCustomerTimeZone(@NonNull @NonNull com.broadleafcommerce.order.client.domain.Order order)
      Gets the customer timezone from the given Order if specified.
      Parameters:
      order - the Order to get the timezone from
      Returns:
      the customer timezone from the given Order, if specified
    • truncateToBeginningOfDay

      public static Instant truncateToBeginningOfDay(@NonNull @NonNull Instant instantToTruncate, @Nullable String timezoneString)
      Truncates the given Instant to ChronoUnit.DAYS in the specified timezone. If not specified, ZoneOffset.UTC is used.

      This is used to align the subscription dates in customer's perspectives, which is particularly important in timeframes where the local time is on a different day than UTC. If the Instant was truncated to ChronoUnit.DAYS in ZoneOffset.UTC and the customer is in a different timezone, it may result in loss/gain of hours.

      For example, if the customer is in the EEST timezone, which is 3 hours ahead of UTC, and signs up for the subscription at 2am 8/11/2025 EEST time, which is 11pm 8/10/2025 in UTC. Truncating the Instant to ChronoUnit.DAYS in UTC would result to 2025-08-10 00:00:00.000000. However, in the customer's perspective, the subscription sign up date should have been on 8/11/2025. This method is to help truncating to DAYS accurately based on the customer's timezone, which in this case would result in 2025-08-10 21:00:00.000000.

      Returns:
      the Instant that is truncated to ChronoUnit.DAYS by the customer's timezone if specified, otherwise ZoneOffset.UTC is used