69. Getting the first and last day of a quarter Let’s assume that we represent the first and last day of a quarter via this simple class: public final class Quarter { private final Date firstDay; private final Date lastDay; …} Next, we have a java.util.Date and we want the first and the last day […]
Category: Revealing a common mistake with Strings
Computing pregnancy due date – Working with Date and Time
71. Computing pregnancy due date I’m not an expert in the field but let’s start with these two constants: public static final int PREGNANCY_WEEKS = 40;public static final int PREGNANCY_DAYS = PREGNANCY_WEEKS * 7; Let’s consider the first day as a LocalDate and we want to write a calculator that prints the pregnancy due date, […]
Extracting the count of milliseconds since midnight – Working with Date and Time
73. Extracting the count of milliseconds since midnight So, we have a date-time (let’s say a LocalDateTime or LocalTime) and we want to know how many milliseconds have been passed from midnight to this date-time. Let’s consider that the given date-time is right now: LocalDateTime now = LocalDateTime.now(); Midnight is relative to now, is in […]