简体   繁体   中英

List all weeks,months and year in JAVA/Android

Is it possible to list the all weeks/date given two date range for example:

Date from 1/1/2013 to 1/1/2020 result will be:

  • 1-7,2013
  • 8-14,2013
  • 15-21,2013 and soon til 2020 and same with month.

Please try out this for the case of weeks(check if you can optimize).

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // to provide month range dynamically

    Calendar calendar = Calendar.getInstance();
    Date minDate = calendar.getTime();
    calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
    Date maxDate = calendar.getTime();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    String startDate = dateFormat.format(minDate);
    String endDate = dateFormat.format(maxDate);

    List<Date> dates = getDates(startDate, endDate); // to get dates between range
    int prevIdentifier = 0;
    int identifier;
    String initDate, finalDate;
    List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates); 
    SimpleDateFormat dformatter = new SimpleDateFormat("dd");
    SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
    initDate = dformatter.format(weekDays.get(0).getDate());
    finalDate = dformatter.format(weekDays.get(0).getDate());
    String outputData = "";
    for (WeekDay weekDay : weekDays) {
        identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
        if (prevIdentifier != 0 && identifier != prevIdentifier) {
            if (outputData.equalsIgnoreCase(""))
                outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
            else
                outputData += "  *  " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
            initDate = dformatter.format(weekDay.getDate());
        } else {
            finalDate = dformatter.format(weekDay.getDate());
        }
        prevIdentifier = identifier;
    }
    System.out.println("OUTPUT DATA :" + outputData);

}

 public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
    List<WeekDay> listOfWeeks = new ArrayList<>();
    WeekDay weekDay;
    for (Date date : listOfDates) {
        weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
        listOfWeeks.add(weekDay);
    }
    return listOfWeeks;
}

public class WeekDay {

    Date date;
    String weekIdentifier;

    public WeekDay(Date Date, String WeekIdentifier) {
        this.date = Date;
        this.weekIdentifier = WeekIdentifier;
    }

    public Date getDate() {
        return date;
    }

    public String getWeekIdentifier() {
        return weekIdentifier;
    }

}

private static List<Date> getDates(String dateString1, String dateString2) {
    ArrayList<Date> dates = new ArrayList<Date>();
    DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");

    Date date1 = null;
    Date date2 = null;

    try {
        date1 = df1.parse(dateString1);
        date2 = df1.parse(dateString2);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);


    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);

    while (!cal1.after(cal2)) {
        dates.add(cal1.getTime());
        cal1.add(Calendar.DATE, 1);
    }
    return dates;
}

java.time

I would use a LocalDate for start date (eg, 1/1/2013) and one for end date (1/1/2020). To represent the period you want (week, month or year) I might use either the appropriate ChronoUnit constant or — more flexibly — a Period . The mentioned classes are from java.time, the modern Java date and time API. A pretty simple loop will iterate through your start dates (Jan 1, Jan 8, Jan 15, etc.). Subtract 1 from each start date (except the first) to get the end dates (Jan 8 minus 1 day gives Jan 7, etc.). Happy coding. And as Scary Wombat already said, if you have any problems, please ask a different and better question here.

Question: Can I use java.time on Android?

Yes, java.time works nicely on Android devices. It just requires at least Java 6 .

  • In Java 8 and later and on new Android devices (from API level 26, I'm told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It's called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM