如何将字符串转换为日期范围

问题描述 投票:0回答:1

我想将以下格式的字符串转换为java.util.Date范围,开始和结束日期:

January 16th – 21st, 2025
January 16th – February 21st, 2025
January 16th – February 21st, 2025
December 16th 2024 – January 2nd 2025
November 16th – 19th

您有不同月份和不同年份的示例,以及没有年份但应使用当前年份的示例

您知道我应该如何处理所有这些格式吗?

java date
1个回答
0
投票

这个怎么样 - 通过 chatgpt。不要指望它能直接起作用 - 看看它作为一个例子。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateRangeConverter {

    public static void main(String[] args) {
        // List of date range strings
        String[] dateRanges = {
                "January 16th – 21st, 2025",
                "January 16th – February 21st, 2025",
                "January 16th – February 21st, 2025",
                "December 16th 2024 – January 2nd 2025",
                "November 16th – 19th"
        };

        // Parse and print the date ranges
        List<Date[]> parsedDateRanges = new ArrayList<>();
        for (String dateRange : dateRanges) {
            try {
                parsedDateRanges.add(parseDateRange(dateRange));
            } catch (ParseException e) {
                System.out.println("Error parsing date range: " + dateRange);
                e.printStackTrace();
            }
        }

        // Print out the parsed date ranges
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (Date[] range : parsedDateRanges) {
            System.out.println("Start: " + outputFormat.format(range[0]) + ", End: " + outputFormat.format(range[1]));
        }
    }

    // Parse date ranges in the format provided
    private static Date[] parseDateRange(String dateRange) throws ParseException {
        SimpleDateFormat yearMonthDayFormat = new SimpleDateFormat("MMMM d yyyy");
        SimpleDateFormat yearOnlyFormat = new SimpleDateFormat("MMMM d yyyy");

        // Remove suffixes from days for parsing
        dateRange = dateRange.replaceAll("th|st|nd|rd", "");

        String[] parts = dateRange.split("–");

        Date startDate;
        Date endDate;

        // Check if the range spans two months or just days within the same month
        if (parts.length == 2) {
            String startPart = parts[0].trim();
            String endPart = parts[1].trim();

            // Parse start and end dates depending on whether they include month/year
            if (endPart.matches("\\d{1,2}, \\d{4}$")) { // "21st, 2025" or "February 21st, 2025"
                startDate = yearMonthDayFormat.parse(startPart + " 2025");
                endDate = yearMonthDayFormat.parse(endPart);
            } else if (endPart.matches("\\d{1,2}$")) { // "21st" without month
                String[] startTokens = startPart.split(" ");
                String month = startTokens[0];
                startDate = yearMonthDayFormat.parse(startPart + " 2025");
                endDate = yearMonthDayFormat.parse(month + " " + endPart + " 2025");
            } else { // If format includes both month and year
                startDate = yearMonthDayFormat.parse(startPart + " 2025");
                endDate = yearMonthDayFormat.parse(endPart);
            }
        } else {
            // Single date case (e.g., December 16th 2024 – January 2nd 2025)
            String[] tokens = parts[0].split(" ");
            startDate = yearOnlyFormat.parse(parts[0]);
            endDate = yearOnlyFormat.parse(parts[1]);
        }

        return new Date[]{startDate, endDate};
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.