Posts

Showing posts with the label Java8LocalDate

How to find the day of the week represented by a particular date

In this article, I will be demonstrating how you can find the day of the week represented by a particular date. You can use Java 8 LocalDate class for this. The following code demonstrates this: LocalDate date = LocalDate.now();DayOfWeek dayOfWeek = LocalDate.of(2019, 8, 11);System.out.println("dayOfWeek="+dayOfWeek); The LocalDate class has a method called getDayOfWeek. This returns a DayOfWeek enum which corresponds to the day of the week.  Here a LocalDate object corresponding to 11th August 2019 is created which corresponds to a Sunday. So when you execute this code, it prints the following output: dayOfWeek=SUNDAY

Java 8 LocalDate Class explained with code samples

Another new feature by Java 8 is the DateTime API. Some of the important classes in the DateTime API are the LocalDate, LocalTime and LocalDateTime . In this article, I will be explaining the Java 8 LocalDate class in detail. Edit Introduction The LocalDate class represents a date. It is present in the java.time package. It has several methods that allow you to perform date manipulation easily. Creating LocalDate There are several static methods on the LocalDate class that allow you to obtain a LocalDate instance. The following code demonstrates this: public class LocalDateDemo { public static void main(String[] args) { LocalDate date1 = LocalDate.now(); System.out.println("Today's date is "+date1); LocalDate date2 = LocalDate.parse("2019-07-15"); System.out.println("Date2 is "+date2); LocalDate date3 = LocalDate.of(2017,05,17); System.out.println("Date3 is "+date3 ); LocalDate date4 = LocalDate.ofYear...