How to find every week dates between startDate and endDate passing string like " Wed, Thu" which is dynamically passed

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

How to find every week dates between startDate and endDate passing string like " Wed, Thu" which is dynamically passed

问题

当我使用字符串"Mon, Tue, Wed, Thu, Fri, Sat, Sun"来传递startDate和endDate,并且还附带了一周中的某些天,我想要在startDate和endDate之间,根据传递的每周天数获得日期。

天数可以是整个星期,也可以是传递给方法的自定义天数。

  1. 我的尝试代码
public List<LocalDate> getWeeklyDateByStringofDays(String DaysofWeek, LocalDate startDate, LocalDate endDate) {
    List<String> daysOfWeekList = Arrays.asList(DaysofWeek.split(","));
    // 我不知道该如何做
}
英文:

when i passing startDate and endDate with day of week like as string "Mon ,Tue, Wed, Thu, Fri, Sat, Sun" then i want date of every weekly based on passing day of week between startDate and endDate.

Days is may be fully week or custom day from passing into method.

1.My try code

public List&lt;LocalDate&gt; getWeeklyDateByStringofDays(String DaysofWeek, LocalDate startDate, LocalDate endDate) {
    List&lt;String&gt; daysOfWeekList = Arrays.asList(DaysofWeek.split(&quot;,&quot;));
    // How can do it no idea
}

答案1

得分: 3

你可以从LocalDate中以三个字母的格式(例如Mon)获取DayOfWeek,并且可以使用以下方式检查是否包含在daysOfWeek中:

localdate.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US))

然后,你可以使用Stream.iterate迭代日期范围,并且如果LocalDateDayOfWeek包含在你的daysOfWeek中,则将这些日期收集到列表中:

public List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate,
        LocalDate endDate) {
    final int days = (int) startDate.until(endDate, ChronoUnit.DAYS);
    return Stream.iterate(startDate, d -> d.plusDays(1))
            .limit(days)
            .filter(d -> daysOfWeek.contains(d.getDayOfWeek()
                                .getDisplayName(TextStyle.SHORT, Locale.US)))
            .collect(Collectors.toList());
}
英文:

You can get DayOfWeek from LocalDate in the format of three letters(e.g.Mon) and check contains in daysOfWeek like

localdate.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US))

And iterate over the range using Stream.iterate and filter if DayOfWeek of LocalDate contains in your daysOfWeek then collect localdates in list

  public List&lt;LocalDate&gt; getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate,
      LocalDate endDate) {
    final int days = (int) startDate.until(endDate, ChronoUnit.DAYS);
    return Stream.iterate(startDate, d -&gt; d.plusDays(1))
        .limit(days)
        .filter(d -&gt; daysOfWeek.contains(d.getDayOfWeek()
                                          .getDisplayName(TextStyle.SHORT, Locale.US)))
        .collect(Collectors.toList());
  }

答案2

得分: 2

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

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getWeeklyDateByStringofDays("Mon, Tue, Wed", LocalDate.parse("2020-09-14"),
                LocalDate.parse("2020-12-14")));
    }

    static List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate, LocalDate endDate) {
        // Split the string on optional whitespace followed by comma which in turn may
        // be followed by optional whitespace
        String[] daysOfWeekList = daysOfWeek.split("\\s*,\\s*");

        // The list to be populated with desired dates and returned
        List<LocalDate> result = new ArrayList<>();

        // Formatter to get only day name e.g. Mon from the date
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH);

        for (String day : daysOfWeekList) {
            // Loop starting with the startDate until the endDate with a step of one day
            for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
                if (date.format(dtf).equals(day)) {
                    result.add(date);
                }
            }
        }

        // Sort the list
        Collections.sort(result);

        return result;
    }
}

输出:

[2020-09-14, 2020-09-15, 2020-09-16, 2020-09-21, 2020-09-22, 2020-09-23, 2020-09-28, 2020-09-29, 2020-09-30, 2020-10-05, 2020-10-06, 2020-10-07, 2020-10-12, 2020-10-13, 2020-10-14, 2020-10-19, 2020-10-20, 2020-10-21, 2020-10-26, 2020-10-27, 2020-10-28, 2020-11-02, 2020-11-03, 2020-11-04, 2020-11-09, 2020-11-10, 2020-11-11, 2020-11-16, 2020-11-17, 2020-11-18, 2020-11-23, 2020-11-24, 2020-11-25, 2020-11-30, 2020-12-01, 2020-12-02, 2020-12-07, 2020-12-08, 2020-12-09, 2020-12-14]

注意: 如果参数中的星期几名称可以是任何大小写形式,请在上述代码中将 date.format(dtf).equals(day) 替换为 date.format(dtf).equalsIgnoreCase(day)

英文:

You can do it as follows:

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

public class Main {
	public static void main(String[] args) {
		// Test
		System.out.println(getWeeklyDateByStringofDays(&quot;Mon, Tue, Wed&quot;, LocalDate.parse(&quot;2020-09-14&quot;),
				LocalDate.parse(&quot;2020-12-14&quot;)));
	}

	static List&lt;LocalDate&gt; getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate, LocalDate endDate) {
		// Split the string on optional whitespace followed by comma which in turn may
		// be followed by optional whitespace
		String[] daysOfWeekList = daysOfWeek.split(&quot;\\s*,\\s*&quot;);

		// The list to be populated with desired dates and returned
		List&lt;LocalDate&gt; result = new ArrayList&lt;&gt;();

		// Formatter to get only day name e.g. Mon from the date
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(&quot;EEE&quot;, Locale.ENGLISH);

		for (String day : daysOfWeekList) {
			// Loop starting with the startDate until the endDate with a step of one day
			for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
				if (date.format(dtf).equals(day)) {
					result.add(date);
				}
			}
		}

		// Sort the list
		Collections.sort(result);

		return result;
	}
}

Output:

[2020-09-14, 2020-09-15, 2020-09-16, 2020-09-21, 2020-09-22, 2020-09-23, 2020-09-28, 2020-09-29, 2020-09-30, 2020-10-05, 2020-10-06, 2020-10-07, 2020-10-12, 2020-10-13, 2020-10-14, 2020-10-19, 2020-10-20, 2020-10-21, 2020-10-26, 2020-10-27, 2020-10-28, 2020-11-02, 2020-11-03, 2020-11-04, 2020-11-09, 2020-11-10, 2020-11-11, 2020-11-16, 2020-11-17, 2020-11-18, 2020-11-23, 2020-11-24, 2020-11-25, 2020-11-30, 2020-12-01, 2020-12-02, 2020-12-07, 2020-12-08, 2020-12-09, 2020-12-14]

Note: If the day names in the parameter can be in any case, replace date.format(dtf).equals(day) with date.format(dtf).equalsIgnoreCase(day) in the code given above.

答案3

得分: 1

  1. 将您的 DaysOfWeek 输入字符串解析为 List<DayOfWeek> 对象。例如,首先调用 .split("\\s*,\\s*") 来获取包含 MonTue 等的字符串数组。

  2. 将字符串 "Mon" 转换为 DayOfWeek.MONDAY。如下所示。

  3. 创建一个循环以在开始和结束之间循环遍历每个日期:for (LocalDate d = start; d.isBefore(end); d = d.plusDays(1))。对于每个日期,获取它代表的星期几,并检查是否在您的列表中。

如何将 "Mon" 转换为 DOW.MONDAY?

您可以创建自己的哈希映射,将字符串映射到 DayOfWeek 值。或者,您可以依赖于 Java 的日期解析,但这有点复杂:

Locale where = Locale.forLanguageTag("en"); 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE", where);
TemporalAccessor accessor = formatter.parse("monday");
DayOfWeek dow = DayOfWeek.from(accessor);

请注意,您必须指定语言。毕竟,在全球范围内有很多种说法来表示“星期一” How to find every week dates between startDate and endDate passing string like " Wed, Thu" which is dynamically passed

英文:
  1. Take your DaysOfWeek input string and parse it into a List&lt;DayOfWeek&gt; object. For example, by first calling .split(&quot;\\s*,\\s*&quot;) to obtain a string array containing Mon, Tue, etcetera.

  2. Turn the string &quot;Mon&quot; into DayOfWeek.MONDAY. See below.

  3. Create a for loop to loop through every date between start and end: for (LocalDate d = start; d.isBefore(end); d = d.plusDays(1)). Per date, fetch the day of week it represents, and check that it is in your list.

How do I turn "Mon" into DOW.MONDAY?

You can make your own hashmap that maps strings to DayOfWeek values. Alternatively, you can rely on java's date parsing, but it's a little tricky:

Locale where = Locale.forLanguageTag(&quot;en&quot;); 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;EEEE&quot;, where);
TemporalAccessor accessor = formatter.parse(&quot;monday&quot;);
DayOfWeek dow = DayOfWeek.from(accessor);

note how you'll have to specify language, of course. Across the planet there are quite a few ways to say 'monday', after all How to find every week dates between startDate and endDate passing string like " Wed, Thu" which is dynamically passed

huangapple
  • 本文由 发表于 2020年9月14日 15:37:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63879972.html
匿名

发表评论

匿名网友

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

确定