Get First and Last Day of Week, Month or Year in Java
Learn to use Java 8 LocalDate and TemporalAdjusters class to get the first and the last day of the current week, month or year with examples.
Java provides the java.time package (introduced in Java 8) for handling date and time. This package offers comprehensive date and time functionality, making it easier to work with date and time values accurately and effectively.
Learn More.
Learn to use Java 8 LocalDate and TemporalAdjusters class to get the first and the last day of the current week, month or year with examples.
In this Java tutorial, learn the different approaches for calculating the number of weeks between two dates using the Java Date and Time API.
In this Java tutorial, we will explore various methods to display the name of the day of the week, catering to different scenarios and requirements. Note that we should be careful of using the int values assigned to weekdays as they differ from method to method. 1. Display the Day …
In this Java date-time tutorial, we’ll explore how to split a date-time range into equal intervals. Conceptually, splitting a date-time range into equal intervals involves dividing the entire range into smaller, equally-sized sub-ranges. In short, we need to determine the start and end points of each sub-range. 1. Splitting Date-time …
Learn to implement and use a StopWatch in Java with a custom implementation for simple uses and Apache Commons StopWatch class for advanced uses.
Learn to calculate the elapsed time since midnight (in seconds or milliseconds) using Java 8 classes such as LocalTime, LocalDateTime and ZonedDateTime.
Learn to use new Java 8 APIs to obtain the current quarter, the quarter from a given date and the start and end date of a quarter.
Learn to get the Month information in either number or name, and to convert it into another using Java Date-time API and simple examples.
Since Java 16, we can use the additional parameter [B] to format a date-time object with the date-time information using the DateTimeFormatter class.
Learn about the available zone ids and zone offsets in Java 8 or later. These zone ids and offsets represent an instant/timestamp that is local to the specified timezone. 1. Using ZoneId and ZoneOffset Java 8 introduced new date and time APIs (JSR-310), including ZoneId and ZoneOffset. In Java, the …
Learn to convert a specified Instant (in UTC) to ZonedDateTime (at some zone), and ZonedDateTime to Instant with easy-to-follow Java examples.
Learn to convert a given Instant to LocalDateTime, LocalDate, or LocalTime instances in the current system timezone or a specified timezone in Java. 1. Difference between Instant and LocalDateTime An Instant is an instantaneous point on the time-line without any timezone information associated with it. Internally, it uses the system …
The legacy Java API had been a big pain point for Java developers for quite a long time. With the release of Java 8 release (JSR-310), package java.time introduced the new immutable classes that solved the issue with the original classes. Following articles have been published to help you start with some very common …
Learn to add or subtract hours, minutes or seconds from a given date and time in Java using various date-time classes and their plus methods.
Learn to get the year, month and day from a given date in Java using the new LocalDate classes as well as legacy Date and Calendar classes.
Learn to set the default time zone used by the JVM using an environment variable, JVM argument and TimeZone class.
Learn to find the day of the week for a given date using the legacy Date and Calendar classes as well as the new Java 8 Date API. 1. Overview There may be a requirement to show the day of the week (Monday to Sunday) in the UI, and then …
Learn to get the start and the end of a date using Java date API, e.g., LocalDateTime and ZonedDateTime. We may require to get this information in many cases. For example, we have to filter the events that occurred at different times in a single day based on event timestamps. …
For any application supporting multiple locales and timezones, it is often the requirement to show the date and timestamp adjusted to the user’s local timezone offset.
Learn about ZonedDateTime and OffsetDateTime classes in Java, and what are the main differences between both classes in depth. 1. Understanding Zone Id and Zone Offsets Before jumping to the internals of the classes, let’s make sure we understand the difference between the zone identifier and zone offsets. Most programming …
Learn to convert a given duration in milliseconds to hours, minutes and seconds; and format to to HH:mm:ss and any other custom pattern.
Starting with JDK 8, we have a comprehensive date-time API containing classes such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetDateTime, and OffsetTime. These classes allow easy formatting of the date-time output via DateTimeFormatter.ofPattern(). The API also allows further customize the output based on specified Locale information to support localization and internationalization features. In …
Java supports creating and modifying the date and time using primarily two packages java.time and java.util. The package java.time was part of Java 8 release (JSR-310) that introduced the new immutable classes solving the shortcomings of the legacy java.util.Date and java.util.Calendar classes. 1. Legacy Date Time API (Prior to Java …
Learn to check if a given Date is a weekend in Java. We will learn to check using java.util.Date as well as Java 8 java.time.LocalDate classes. In the given examples, we are assuming that a weekend is either Saturday or Sunday. The rest five days of the week are weekdays. …
Learn to create and use the Period class that was introduced as part of the new Date Time API in Java 8. The Period class represents the period of time in date-based values such as days, months, years, weeks, or years in the ISO-8601 calendar system such as “1 year …
Learn to use Java TemporalAdjusters that help with complex date-time calculations such as calculating recurring meeting dates, processing weekly reports, sending automated monthly report outs, etc. 1. Overview In the new Java Date API, the Temporal interface represents a date, time, or a combination of both. For example, LocalDate, LocalDateTime …
Learn to format a given date to a specified formatted string in Java. We will learn to use inbuilt patterns and custom patterns with DateTimeFormatter and SimpleDateFormat. 1. Formatting with DateTimeFormatter [Java 8] Since Java 8, We can use DateTimeFormatter for all types of date and time related formatting tasks. …
Learn to validate whether a given string contains a date value in Java using various date validation techniques available in Java 8.
Learn to get all the dates between the two given dates. We will see the solutions in Java 7, Java 8, and Java 9. 1. LocalDate.datesUntil() (since Java 9) LocalDate‘s datesUntil() method returns a sequential ordered stream of all dates between two given dates. The returned stream starts from startDate …
In Java, we can use the ChronoUnit and LocalDate.until() methods to calculate the number of days between two given dates. Other solutions exist, but these are the simplest and do not require importing an additional library. 1. Using ChronoUnit to Find the Number of Days between Two Dates Using ChronoUnit.DAYS.between() …
Learn to find all business days between two given dates in Java. The business days are considered all weekdays, excluding all holidays falling on weekdays. Once we have a List of business dates, we can use the list.size() API to get the total count of business days. The given examples …
Learn to add or subtract a given number of business days to new date times classes in Java such as LocalDate, LocalDateTime and ZonedDateTime. The given example takes the holidays list as well into consideration. Read More: Add or Subtract Days to/from Date 1. Adding Business Days It uses two …
Learn to determine which day of the week is a given date in Java. The weekdays are considered all 7 days from Sunday, Monday till Saturday. 1. DayOfWeek Enum DayOfWeek is an enum representing the seven days of the week – Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. 2. …
TemporalQuery is a standard way of querying the temporal objects (LocalDate, LocalDateTime etc) for making better business decisions. In Java 8, all major date-time classes implement Temporal and TemporalAccessor interfaces so TemporalQuery can be run against all those Java classes. 1. TemporalQuery interface In the new Java Date API, Temporal …
Java examples to get the next day or the previous day for any given day. The example uses legacy java.util.Date class as well as java.time.LocalDate class from Java 8. We can use this example code to calculate tomorrow’s and yesterday’s dates based on the date of today. 1. Using LocalDate …
Learn to convert a string to date time instance classes e.g. ZonedDateTime or OffsetDateTime classes, using DateTimeFormatter class in Java. 1. Instant, OffsetDateTime and ZonedDateTime Classes In Java 8, OffsetDateTime and ZonedDateTime – both store an instant on the universal timeline to nanosecond precision. OffsetDateTime adds to the instant the …
Learn to parse date and time strings into instances of LocalDate and LocalDateTime using strict style using ResolverStyle.STRICT parameter with DateTimeFormatter instance.
Learn to compare two LocalDateTime instances to find out which date represents an older timestamp in comparison to other timestamp. LocalDateTime class is part of java.time package added in Java 8.
Learn to compare two LocalDate instances to find out which date represents an older date in comparison to second date. LocalDate class is part of java.time package added in Java 8.
Learn to format date and time in either 12 hours pattern. The formatted date string will have the AM/PM information as applicable to the timestamp.
Java examples to format LocalDate to String using inbuilt and custom patterns using FormatType and format(formatter) API.
Java programs to find if a given year is a leap year using new Java 8 APIs (LocalDate and Year), legacy Java 7 GregorianCalendar, and custom code.
Learn to compare two instances of ZonedDateTime either in the same timezone or in different timezones in Java 8. 2. Comparing at Same Instant As we know an instance of ZonedDateTime is a point in the universal timeline with an offset. So to compare two such instances, logically, both instances …
Learn to format a ZonedDateTime to string using ZonedDateTime.format (DateTimeFormatter) method in Java 8. Convert ZonedDateTime to String example.
Learn to convert from LocalDate to ZonedDateTime and from ZonedDateTime to LocalDate in Java 8. As we know, LocalDate represents a calendar date without the time and the zone information. ZonedDateTime instance contains all three information i.e. date, time and zone. ZonedDateTime = LocalDate + time + timezone 1. LocalDate …
Learn to convert from LocalDate to LocalDateTime and from LocalDateTime to LocalDate in Java 8.
Java 8 ZonedDateTime class represents an instant in the universal timeline with the timezone information. In this tutorial, we will learn to parse a string to ZonedDateTime object using its parse() method. 1. Parsing a Date Time with ZonedDateTime.parse() Java program to convert a given string into ZonedDateTime instance. After …
Java LocalDateTime class represents an instant in local timeline i.e. without any timezone id. Learn to convert string to LocalDateTime.
Java LocalDate class represents a calendar date without time (hour/minute/seconds) and timezone information. Learn to convert a string to LocalDate object in Java.
Learn about the ZonedDateTime class in Java, how to create its instances and other use cases such as parsing, formatting and adding duration and periods.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.