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: Computing 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 […]
Displaying the names of the days of the week – Working with Date and Time
76. Displaying the names of the days of the week One of the hidden gems in Java is java.text.DateFormatSymbols. This class is a wrapper for date-time formatting data such as the names of the days of the week, and the names of the months. All these names are localizable.Typically you will use DateFormatSymbols via a […]
Calculating the middle of the month – Working with Date and Time
79. Calculating the middle of the month Let’s imagine that we have a LocalDate and we want to calculate from it another LocalDate representing the middle of the month. This can be achieved in seconds if we know that the LocalDate API has a method named lengthOfMonth() which returns an integer representing the length of […]