Joda time - all mondays between two dates

前端 未结 6 1952
我寻月下人不归
我寻月下人不归 2020-12-16 03:36

I am using Joda time api in a Spring 3.0 project for the very first time. Now I have a start and end date and I want to get the date for all mondays between these two dates.

相关标签:
6条回答
  • 2020-12-16 03:54

    I recently developed Lamma which is designed to solve this exact use case:

    Dates.from(2011, 11, 8).to(2011, 12, 30).byWeek().on(DayOfWeek.MONDAY).build();
    

    and you will get a List<Date> of:

    Date(2011,11,14)
    Date(2011,11,21)
    Date(2011,11,28)
    Date(2011,12,5)
    Date(2011,12,12)
    Date(2011,12,19)
    Date(2011,12,26)
    
    0 讨论(0)
  • 2020-12-16 03:55

    In Java 8 using Stream ,

    LocalDate startDate = LocalDate.of(2019, 2, 1);
    LocalDate endDate = LocalDate.of(2019, 2, 28);
    long numOfDays = ChronoUnit.DAYS.between(startDate, endDate);
    List<LocalDate> daysRange = Stream.iterate(startDate, date -> date.plusDays(1)).limit(numOfDays).filter( date -> date.getDayOfWeek()==DayOfWeek.MONDAY ).collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-12-16 04:01

    This code takes to string dates and gives the number of sundays and also all the sunday's dates

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class FindAllSundays {
    
    
    public static int getNumberofSundays(String d1, String d2) throws Exception { // object
                                                                                    // in
                                                                                    // Date
                                                                                    // form
    
        Date date1 = getDate(d1);
        Date date2 = getDate(d2);
    
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        int sundays = 0;
        while (c2.after(c1)) {
            // System.out.println(" came here ");
            //checks to see if the day1 ....so on next days are sundays if sunday goes inside to increment the counter
            if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                System.out.println(c1.getTime().toString() + " is a sunday ");
                sundays++;
    
            }
            c1.add(Calendar.DATE, 1);
        }
    
        System.out.println("number of sundays between 2 dates is " + sundays);
    
        return sundays;
    }
    // converts string to date 
    public static Date getDate(String s) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(s);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }
    
    public static void main(String[] arg) throws Exception {
        System.out.println(" " + getNumberofSundays("2005-10-07", "2006-10-01"));
    }
    

    }

    0 讨论(0)
  • 2020-12-16 04:03
    import java.time.LocalDate;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Get_time {
    
        public  ArrayList<LocalDate> getmondays(String s,String e)
        {
            LocalDate start = LocalDate.parse(s);
            LocalDate end = LocalDate.parse(e);
            List<LocalDate> totalDates_Mondays = new ArrayList<>();
    
            while (!start.isAfter(end)) {
                totalDates_Mondays.add(start);
                start = start.plusWeeks(1);
            }
            return (ArrayList<LocalDate>) totalDates_Mondays;
        }
    
        public static void main(String ...s1) {
    
            String mon_start = "1600-08-01";
            String mon_end= "2016-12-29";
            Get_time t=new Get_time();
            System.out.println(t.getmondays(mon_start,mon_end));
        }
    }
    
    0 讨论(0)
  • 2020-12-16 04:09
    LocalDate startDate = new LocalDate(2011, 11, 8);
    LocalDate endDate = new LocalDate(2012, 5, 1);
    
    LocalDate thisMonday = startDate.withDayOfWeek(DateTimeConstants.MONDAY);
    
    if (startDate.isAfter(thisMonday)) {
        startDate = thisMonday.plusWeeks(1); // start on next monday
    } else {
        startDate = thisMonday; // start on this monday
    }
    
    while (startDate.isBefore(endDate)) {
        System.out.println(startDate);
        startDate = startDate.plusWeeks(1);
    }
    
    0 讨论(0)
  • 2020-12-16 04:16

    FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

    Using java.time

    The LocalDate class is java.time is akin to the Joda-Time LocalDate. A date-only value, without time-of-day and without time zone. One difference is that java.time eschews constructors for factory methods.

    LocalDate start = LocalDate.of( 2011 , 11 , 8 );
    LocalDate stop = LocalDate.of( 2012 , 5 , 1 );
    

    Collect the Mondays.

    List<LocalDate> mondays = new ArrayList<>();
    

    The TemporalAdjuster interface provides for classes that manipulate date-time values. The TemporalAdjusters class (note the plural name) provides various implementations. We want the nextOrSame and next adjusters, passing the desired DayOfWeek.MONDAY enum object.

    LocalDate monday = start.with( TemporalAdjusters.nextOrSame( DayOfWeek.MONDAY ) );
    while( monday.isBefore( stop ) ) {
        mondays.add( monday );
        // Set up the next loop.
        monday = monday.plusWeeks( 1 );
    }
    

    By the way, usually the wise approach in handling a span of time is Half-Open where the beginning is inclusive while the ending is exclusive. So in the code above we are running up to, but not including, the stop date.


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

    0 讨论(0)
提交回复
热议问题