Java Programming

Java 8 – Sort a List Using Lambda Expression

Introduction Java 8 introduced lambda expressions, which provide a concise and functional way to express behavior as an anonymous method. One of the common use cases for lambda expressions is sorting a list. Prior to Java 8, sorting a list involved creating an anonymous Comparator class, but with lambda expressions, this process is simplified and …

Java 8 – Sort a List Using Lambda Expression Read More »

Java 8 – Implement Runnable Using Lambda

Introduction Java 8 introduced lambda expressions, which provide a concise and functional way to implement interfaces with a single abstract method, known as functional interfaces. One of the most common use cases for lambda expressions is implementing the Runnable interface, which is used to execute code in a separate thread. Traditionally, implementing Runnable required creating …

Java 8 – Implement Runnable Using Lambda Read More »

Java – Get Year, Month, Day, Hours, Minutes, Seconds, and Milliseconds of LocalDateTime

Introduction When working with date and time values in Java, you might need to extract specific components, such as the year, month, day, hours, minutes, seconds, and milliseconds, from a LocalDateTime object. These values are often required for various operations, such as formatting dates, performing calculations, or storing individual components in a database. Java 8’s …

Java – Get Year, Month, Day, Hours, Minutes, Seconds, and Milliseconds of LocalDateTime Read More »

Java 8 – Difference Between LocalDate, LocalTime, and LocalDateTime

Introduction Java 8 introduced the java.time package, which provides a modern and comprehensive API for date and time operations. Three important classes in this package are LocalDate, LocalTime, and LocalDateTime. Each of these classes serves a different purpose and provides a way to handle specific aspects of date and time without involving time zones. In …

Java 8 – Difference Between LocalDate, LocalTime, and LocalDateTime Read More »

Java 8 – How to Get the First and Last Day of the Month

Introduction With the introduction of the java.time package in Java 8, handling date and time operations has become more straightforward and powerful. One common requirement is determining the first and last day of a given month. This can be useful for tasks such as generating monthly reports, scheduling events, or calculating date ranges. In this …

Java 8 – How to Get the First and Last Day of the Month Read More »

Java 8 – How to Get the Current Time in Milliseconds

Introduction Java 8 introduced the java.time package, which provides a modern and comprehensive framework for handling date and time. However, there are still many scenarios where you need to get the current time in milliseconds, such as when measuring elapsed time or interacting with systems that use timestamps. Java provides several ways to get the …

Java 8 – How to Get the Current Time in Milliseconds Read More »

Java 8 – How to Get the Number of Days Between Two Dates

Introduction Java 8 introduced the java.time package, which provides a more robust and flexible API for handling date and time. One common task is calculating the number of days between two dates, which is useful in various applications such as scheduling, logging, or calculating time intervals. The java.time package simplifies this process with classes like …

Java 8 – How to Get the Number of Days Between Two Dates Read More »

Java 8 – Convert Date to LocalDate

Introduction With the introduction of the java.time package in Java 8, handling date and time operations has become more intuitive and flexible. While java.util.Date was commonly used in earlier versions of Java, LocalDate is now the preferred class for representing a date without time zone information. Converting a Date to a LocalDate is a necessary …

Java 8 – Convert Date to LocalDate Read More »

Java 8 – Convert Date to LocalDate

Introduction Java 8 introduced the java.time package, which provides a modern and comprehensive framework for handling date and time. Prior to Java 8, the java.util.Date class was commonly used to represent date and time. However, with the new API, LocalDate became the preferred choice for representing dates without time zones. Converting a Date to LocalDate …

Java 8 – Convert Date to LocalDate Read More »

Java 8 – Calculate Age from Birthdate

Introduction With the introduction of the java.time package in Java 8, handling dates and times in Java has become more intuitive and powerful. One common task is calculating a person’s age based on their birthdate. This is often required in applications involving user profiles, forms, or any system that tracks personal information. In this guide, …

Java 8 – Calculate Age from Birthdate Read More »

Java 8 – Convert LocalDateTime to Timestamp

Introduction With the introduction of the java.time package in Java 8, developers gained a more modern and powerful API for handling date and time. The LocalDateTime class represents a date and time without a time zone, making it suitable for most date-time operations. However, when interacting with legacy systems or databases, you may still need …

Java 8 – Convert LocalDateTime to Timestamp Read More »

Java 8 – Convert Timestamp to LocalDateTime

Introduction Java 8 introduced the java.time package, which provides a modern and comprehensive framework for handling date and time. Before Java 8, the java.sql.Timestamp class was commonly used to represent a timestamp, typically obtained from databases. With the introduction of LocalDateTime, the new date and time API provides a more powerful and flexible way to …

Java 8 – Convert Timestamp to LocalDateTime Read More »

Java 8 – Find the Day of the Week for a Given Date

Introduction Java 8 introduced the java.time package, which provides a modern and comprehensive framework for handling date and time. One common task is determining the day of the week for a specific date. This can be useful in various scenarios, such as scheduling, logging, or simply displaying the day of the week in a user …

Java 8 – Find the Day of the Week for a Given Date Read More »

Java 8 – Convert LocalDateTime to ZonedDateTime

Introduction Java 8 introduced a new date and time API in the java.time package, which allows for more effective handling of dates and times. One common task is converting a LocalDateTime to a ZonedDateTime. A LocalDateTime represents a date and time without a time zone, while a ZonedDateTime includes the time zone information. Converting a …

Java 8 – Convert LocalDateTime to ZonedDateTime Read More »

Java 8 – Convert LocalDateTime to Epoch Milliseconds

Introduction In Java 8, when dealing with date and time, you may encounter situations where you need to convert a LocalDateTime object into the number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT). This is often required when interfacing with systems that represent time as a long value or for storing date …

Java 8 – Convert LocalDateTime to Epoch Milliseconds Read More »

Java – Format LocalDateTime to dd/MM/yyyy HH:mm:ss

Introduction When working with date and time values in Java, you might need to format a LocalDateTime object into a specific string format, such as dd/MM/yyyy HH:mm:ss. This format is commonly used in many applications, especially in regions that follow the day-month-year convention. Java 8’s java.time package provides a powerful DateTimeFormatter class that makes formatting …

Java – Format LocalDateTime to dd/MM/yyyy HH:mm:ss Read More »

Java – Format LocalDateTime to dd-MM-yyyy HH:mm:ss

Introduction When working with both date and time values in Java, you often need to format a LocalDateTime object into a specific string format, such as dd-MM-yyyy HH:mm:ss. This format is commonly used in many applications for displaying or logging both date and time together. Java 8’s java.time package provides a powerful DateTimeFormatter class that …

Java – Format LocalDateTime to dd-MM-yyyy HH:mm:ss Read More »

Java – Format LocalTime to HH:mm:ss

Introduction When working with time values in Java, you might need to format a LocalTime object into a specific string format, such as HH:mm:ss. This format represents hours, minutes, and seconds, and is commonly used in various applications, particularly for displaying or logging time values. Java 8’s java.time package provides the DateTimeFormatter class, which makes …

Java – Format LocalTime to HH:mm:ss Read More »

Java – Format LocalDate to dd-MM-yyyy

Introduction When working with dates in Java, you often need to display a LocalDate in a specific format, such as dd-MM-yyyy. This format is commonly used in many applications, particularly in regions that follow the day-month-year convention. Java 8’s java.time package provides a powerful DateTimeFormatter class that makes formatting dates easy and intuitive. In this …

Java – Format LocalDate to dd-MM-yyyy Read More »

Java 8 – Get the First Day of the Month

Introduction Java 8 introduced the java.time package, which provides a modern and intuitive API for working with dates and times. A common requirement is to determine the first day of a given month, which can be useful in various applications, such as generating monthly reports, setting billing cycles, or scheduling events. Java 8’s LocalDate class …

Java 8 – Get the First Day of the Month Read More »

Java 8 – How to Add Hours to Time

Introduction Java 8 introduced the java.time package, which provides a modern and robust API for handling dates and times. Adding hours to a specific time is a common requirement, whether you’re scheduling events, calculating deadlines, or adjusting timestamps. Java 8’s LocalTime, LocalDateTime, and ZonedDateTime classes make it easy to add hours to a time value. …

Java 8 – How to Add Hours to Time Read More »

Scroll to Top