如何从给定范围加载日期数组?

huangapple go评论88阅读模式
英文:

How to load array of dates from a given range?

问题

我正在寻找在我的RecyclerView中填充日期范围的最佳方法。为了阐明,我想要从当前日期到接下来的七天内获取所有日期,就像今天是29-07-2020,我想要列出从29-07-2020到04-07-2020的所有日期。现在在我的RecyclerView中,我想要获取每天的日期和月份名称。我尝试了下面的代码,它列出了我想要的日期,但我不确定如何单独获取每天的日期和月份。

List<String> dateList = new ArrayList<String>();
Calendar calendar = Calendar.getInstance();
for (int i=0; i<7; i++) { // Change 31 to 7 to get dates for the next 7 days.
    SimpleDateFormat dayDateFormat = new SimpleDateFormat("dd");
    SimpleDateFormat monthDateFormat = new SimpleDateFormat("MMMM");
    dateList.add(dayDateFormat.format(calendar.getTime()) + " " + monthDateFormat.format(calendar.getTime()));
    calendar.add(Calendar.DATE, 1);
    Log.i("Date" + i , dateList.get(i));
}

请注意,我已将代码中的循环次数从31更改为7,以获取接下来的7天日期。这段代码将每天的日期和月份名称添加到dateList中。

英文:

I'm looking for the best way to populate a range of dates in my recyclerview. To elucidate, I want to fetch the all the dates from the current date to next seven days like if today is 29-07-2020 I want to list all dates from 29-07-2020 to 04-07-2020. Now in my recycler I want to get the day date and month name. I tried the below code and it list the dates as i wish but I'm unsure how to get the day date and month individually.

List&lt;String&gt; dateList = new ArrayList&lt;String&gt;();
    Calendar calendar = Calendar.getInstance();
    for (int i=0; i&lt;31; i++) {
        calendar.add(Calendar.DATE, 1);
        SimpleDateFormat curFormater = new SimpleDateFormat(&quot;EEEE, MMMM dd, yyyy&quot;);
        dateList.add(curFormater.format(calendar.getTime()));

        Log.i(&quot;Date&quot; + i , dateList.get(i));
    }

答案1

得分: 0

你可以考虑使用https://developer.android.com/reference/java/util/Calendar#get(int)来获取月份,像这样:

calendar.get(Calendar.MONTH)

或者获取月份的日期,像这样:

calendar.get(Calendar.DAY_OF_MONTH)

英文:

You should probably consider https://developer.android.com/reference/java/util/Calendar#get(int) and get month like this:

calendar.get(Calendar.MONTH)

or day of the month like this:

calendar.get(Calendar.DAY_OF_MONTH)

答案2

得分: 0

更改传递给SimpleDateFormat的字符串以编辑日期格式。
仅显示日期和月份,您可以尝试:

SimpleDateFormat curFormater = new SimpleDateFormat("dd MMMM");

有关日期格式的更多信息在此处

英文:

Edit the format of the date by changing the String you're passing to SimpleDateFormat.
For displaying only date and month you can try :

SimpleDateFormat curFormater = new SimpleDateFormat(&quot;dd MMMM&quot;);

More on formatting date here

答案3

得分: 0

我建议您切换到现代日期时间 API,而不是使用过时的传统日期时间 API。

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();// 同样等同于 LocalDate.now(ZoneId.systemDefault());
        // 我建议您使用以下变体并应用所需的时区
        // LocalDate today=LocalDate.now(ZoneId.of("Etc/UTC"));

        List<String> dateList = new ArrayList<String>();

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM");

        // 迭代 7 次,从今天开始,每次迭代后日期增加 1 天
        for (LocalDate date = today; date.isBefore(today.plusDays(7)); date = date.plusDays(1)) {
            dateList.add(date.format(formatter));
        }

        // 显示 dateList 的内容
        dateList.forEach(System.out::println);
    }
}

输出结果:

Monday, August
Tuesday, August
Wednesday, August
Thursday, August
Friday, August
Saturday, August
Sunday, August

如果您的 Android 版本尚不兼容 Java 8,您可以使用ThreeTen-Backport进行回溯。您可能还需要帮助来设置它

然而,如果您希望继续使用传统的日期时间 API,可以按照以下方式进行:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        List<String> dateList = new ArrayList<>();

        // 仅定义日期名和月份名的格式
        SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM");

        // 迭代 7 次,从今天开始,每次迭代后日期增加 1 天
        for (int i = 1; i <= 7; i++) {
            dateList.add(curFormater.format(calendar.getTime()));
            calendar.add(Calendar.DATE, 1);
        }

        // 显示 dateList 的内容
        dateList.forEach(System.out::println);
    }
}

输出结果:

Monday, August
Tuesday, August
Wednesday, August
Thursday, August
Friday, August
Saturday, August
Sunday, August
英文:

I recommend you switch from the outdated legacy date-time API to the modern date-time API.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Main {
	public static void main(String[] args) {
		LocalDate today = LocalDate.now();// Same as LocalDate.now(ZoneId.systemDefault());
		// I suggest you use the following variant and apply the required time-zone
		// LocalDate today=LocalDate.now(ZoneId.of(&quot;Etc/UTC&quot;));

		List&lt;String&gt; dateList = new ArrayList&lt;String&gt;();

        // Define the format
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;EEEE, MMMM&quot;);

        // Iterate 7-times starting today with date incrementing by 1 after each iteration
		for (LocalDate date = today; date.isBefore(today.plusDays(7)); date = date.plusDays(1)) {
			dateList.add(date.format(formatter));
		}

		// Display the content of dateList
		dateList.forEach(System.out::println);
	}
}

Output:

Monday, August
Tuesday, August
Wednesday, August
Thursday, August
Friday, August
Saturday, August
Sunday, August

If the version of your Android is not compatible with Java-8 yet, you can backport using ThreeTen-Backport. You may also need help to set it up.

However, if you want to stick to the legacy date-time API, do it as follows:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Main {
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		List&lt;String&gt; dateList = new ArrayList&lt;&gt;();

		// Define the format for only day-name and month-name 
		SimpleDateFormat curFormater = new SimpleDateFormat(&quot;EEEE, MMMM&quot;);

		// Iterate 7-times starting today with date incrementing by 1 after each iteration
		for (int i = 1; i &lt;= 7; i++) {
			dateList.add(curFormater.format(calendar.getTime()));
			calendar.add(Calendar.DATE, 1);
		}

		// Display the content of dateList
		dateList.forEach(System.out::println);
	}
}

Output:

Monday, August
Tuesday, August
Wednesday, August
Thursday, August
Friday, August
Saturday, August
Sunday, August

huangapple
  • 本文由 发表于 2020年7月29日 16:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63149226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定