Example to show Today, this week, last week and last month information.
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.List; import com.jtspider.cb.domain.Contact; public class CbApplication { public static void main(String[] args) { CbApplication cb=new CbApplication(); cb.today(); cb.thisWeek(); cb.lastWeek(); cb.lastMonth(); } DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy"); List<Contact> list = Arrays.asList(new Contact(1L, "Lakshman", LocalDate.parse("06-08-1984", dtf)), new Contact(2L, "Swathi", LocalDate.parse("04-05-1984", dtf)), new Contact(3L, "Rajesh", LocalDate.parse("06-06-1984", dtf)), new Contact(4L, "Manoj", LocalDate.parse("07-06-1984", dtf)), new Contact(5L, "Nagesh NM", LocalDate.parse("02-06-1984", dtf)), new Contact(6L, "Chiru", LocalDate.parse("05-06-1984", dtf)), new Contact(12L, "Nagarjun", LocalDate.parse("04-06-1984", dtf)), new Contact(13L, "Pawan Kalyan", LocalDate.parse("02-06-1984", dtf)), new Contact(14L, "Hrithik", LocalDate.parse("02-06-1984", dtf)), new Contact(6L, "Lakshman", LocalDate.parse("06-08-1984", dtf)), new Contact(7L, "Swathi", LocalDate.parse("04-05-1984", dtf)), new Contact(8L, "Rajesh", LocalDate.parse("05-06-1984", dtf)), new Contact(9L, "Manoj", LocalDate.parse("05-05-1984", dtf)), new Contact(10L, "Nagesh NM", LocalDate.parse("04-06-1984", dtf)), new Contact(11L, "Manoj Kumar", LocalDate.parse("03-06-1984", dtf))); private void today() { System.out.println("----------------------- Today ------------------------"); list.stream().filter(e -> checkDate(e.getDob())).forEach(System.out::println); } private void thisWeek() { System.out.println("----------------------- This Week ------------------------"); list.stream().filter(e -> checkThisWeek(e.getDob())).forEach(System.out::println); } private void lastWeek() { System.out.println("----------------------- Last Week ------------------------"); list.stream().filter(e -> checkLastWeek(e.getDob())).forEach(System.out::println); } private void lastMonth() { System.out.println("---------------------------Last Month--------------------"); list.stream().filter(e -> lastMonth(e.getDob())).forEach(System.out::println); } private boolean lastMonth(LocalDate e) { return e.getMonth() == LocalDate.now().minusMonths(1).getMonth(); } private boolean checkLastWeek(LocalDate e) { LocalDate now = LocalDate.now().minusDays(LocalDate.now().getDayOfWeek().getValue()); int start = now.getDayOfMonth() < 7 ? 1 :now.getDayOfMonth(); int last = now.getDayOfMonth(); return e.getDayOfMonth() >= start && e.getDayOfMonth() <= last && e.getMonth() == now.getMonth(); } private boolean checkThisWeek(LocalDate e) { LocalDate localDate = LocalDate.now(); int day = localDate.with(DayOfWeek.MONDAY).getDayOfMonth(); return e.getDayOfMonth() >= day && e.getDayOfMonth() < localDate.getDayOfMonth() && e.getMonth() == localDate.getMonth(); } private boolean checkDate(LocalDate e) { LocalDate date = LocalDate.now(); return e.getMonth() == date.getMonth() && date.getDayOfMonth() == e.getDayOfMonth(); } }
No comments:
Post a Comment