📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
- LocalDate
- YearMonth
- MonthDay
- Year
1. LocalDate
1.2 LocalDate class Examples
LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth) {
return LocalDate.of(year, month, dayOfMonth);
}
LocalDate getLocalDateFromClock() {
LocalDate localDate = LocalDate.now();
return localDate;
}
LocalDate getNextDay(LocalDate localDate) {
return localDate.plusDays(1);
}
LocalDate getPreviousDay(LocalDate localDate) {
return localDate.minus(1, ChronoUnit.DAYS);
}
DayOfWeek getDayOfWeek(LocalDate localDate) {
DayOfWeek day = localDate.getDayOfWeek();
return day;
}
LocalDate getFirstDayOfMonth() {
LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
return firstDayOfMonth;
}
LocalDateTime getStartOfDay(LocalDate localDate) {
LocalDateTime startofDay = localDate.atStartOfDay();
return startofDay;
}
public static void recurringDate(LocalDate today) {
LocalDate dateOfBirth = LocalDate.of(2010, 01, 14);
MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(today);
if (currentMonthDay.equals(birthday)) {
System.out.println("Many Many happy returns of the day !!");
} else {
System.out.println("Sorry, today is not your birthday");
}
}
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
private static final DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("d-MMM-yyyy");
private static final DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("d/MM/yyyy");
public static void main(String[] args) {
//default format
System.out.println("Default format of LocalDate = " + LocalDate.now());
// The ISO date formatter that formats or parses a date without an
// offset, such as '20111203'
LocalDate date = LocalDate.now();
System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));
System.out.println(date.format(DateTimeFormatter.ISO_DATE));
System.out.println(formatter.format(LocalDate.parse("16/08/2016", formatter)));
System.out.println(formatter1.format(LocalDate.parse("16-Aug-2016", formatter1)));
System.out.println(formatter2.format(LocalDate.parse("16/08/2016", formatter2)));
}
Default format of LocalDate = 2018-07-11
20180711
2018-07-11
16/08/2016
16-Aug-2016
16/08/2016
2. YearMonth
YearMonth date = YearMonth.now();
System.out.printf("%s: %d%n", date, date.lengthOfMonth());
YearMonth date2 = YearMonth.of(2010, Month.FEBRUARY);
System.out.printf("%s: %d%n", date2, date2.lengthOfMonth());
YearMonth date3 = YearMonth.of(2012, Month.FEBRUARY);
System.out.printf("%s: %d%n", date3, date3.lengthOfMonth());
2013-06: 30
2010-02: 28
2012-02: 29
3. MonthDay
MonthDay date = MonthDay.of(Month.FEBRUARY, 29);
boolean validLeapYear = date.isValidYear(2010);
4. Year
boolean validLeapYear = Year.of(2012).isLeap();
Comments
Post a Comment
Leave Comment