Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Java格式化日期范围?_Java_Gwt_Date Format - Fatal编程技术网

如何用Java格式化日期范围?

如何用Java格式化日期范围?,java,gwt,date-format,Java,Gwt,Date Format,我有两个约会——开始和结束。我想对它们进行格式化,这样,如果月份匹配,它们会崩溃为“8月20-23日”,如果它们在月末中断,则仍然可以正确格式化,如“9月20日-10月1日”。有没有实现这一点的库,或者我必须使用单独的日期格式手工编写显示日期范围的代码规则?检查java的类。您可以构造一个SimpleDataFormat,将日期模式作为字符串传递。要添加到Jeroen的答案中,您将使用SimpleDataFormat两次,范围的每一端一次 默认情况下,Java中没有day range这样的类型,

我有两个约会——开始和结束。我想对它们进行格式化,这样,如果月份匹配,它们会崩溃为“8月20-23日”,如果它们在月末中断,则仍然可以正确格式化,如“9月20日-10月1日”。有没有实现这一点的库,或者我必须使用单独的日期格式手工编写显示日期范围的代码规则?

检查java的类。您可以构造一个SimpleDataFormat,将日期模式作为字符串传递。

要添加到Jeroen的答案中,您将使用SimpleDataFormat两次,范围的每一端一次

默认情况下,Java中没有day range这样的类型,因此没有一种日期格式适用于这种情况下的字符串表示

在我看来,最好是创建TimeSpan或TimeDiff类型,并将方法重写为string。或者,如您所建议的,如果您还需要两个DateFormmater,请创建一个DateFormmater

这里有一个解决方案,它使用Java中处理日期的最佳库(上次我检查时)。格式很简单,毫无疑问可以通过使用自定义DateFormatter实现来改进。这也会检查年份是否相同,但不会输出年份,这可能会造成混淆

import org.joda.time.DateTime;

public class DateFormatterTest {

    public static void main(String[] args) {

        DateTime august23rd = new DateTime(2010, 8, 23, 0, 0, 0, 0);
        DateTime august25th = new DateTime(2010, 8, 25, 0, 0, 0, 0);
        DateTime september5th = new DateTime(2010, 9, 5, 0, 0, 0, 0);

        DateFormatterTest tester = new DateFormatterTest();
        tester.outputDate(august23rd, august25th);
        tester.outputDate(august23rd, september5th);

    }

    private void outputDate(DateTime firstDate, DateTime secondDate) {
        if ((firstDate.getMonthOfYear() == secondDate.getMonthOfYear()) && (firstDate.getYear() == secondDate.getYear())) {
            System.out.println(firstDate.getDayOfMonth() + " - " + secondDate.getDayOfMonth() + " " + firstDate.monthOfYear().getAsShortText());
        } else {
            System.out.println(firstDate.getDayOfMonth() + " " + firstDate.monthOfYear().getAsShortText() + " - " + secondDate.getDayOfMonth() + " " + secondDate.monthOfYear().getAsShortText());
        }
    }
}
输出:

8月23日至25日


8月23日至9月5日

我对其他答案感到失望。Joda time在GWT中不起作用,SimpleDateFormat也不起作用。无论如何,我已经知道GWT中的DateTimeFormat。主要问题是date objects getMonth()函数已被弃用,而且似乎没有比较月份和/或年份的好方法。此解决方案不包括年度检查(可以通过更改monthFormatter轻松添加),但这对于我的案例并不重要

public final class DateUtility
{
    public static final DateTimeFormat MONTH_FORMAT = DateTimeFormat.getFormat("MMM");
    public static final DateTimeFormat DAY_FORMAT = DateTimeFormat.getFormat("dd");
    public static final DateTimeFormat DAY_MONTH_FORMAT = DateTimeFormat.getFormat("dd MMM");

    public static final String DASH = " - ";

    /**
     * Returns a "collapsed" date range String representing the period of time
     * between two Date parameters. Example: August 19 as a {@code startDate}
     * and August 30 as an {@code endDate} will return "19 - 30 AUG", August 28
     * as a {@code startDate} and September 7 as an {@code endDate} will return
     * "28 AUG - 07 SEP". This means if you pass this two dates which cannot
     * collapse into a shorter form, then the longer form is returned.  Years
     * are ignored, and the start and end dates are not checked to ensure we
     * are not going backwards in time (Aug 10 - July 04 is not checked).
     * 
     * @param startDate
     *            the start Date in the range.
     * @param endDate
     *            the end Date in the range.
     * @return a String representation of the range between the two dates given.
     */
    public static String collapseDate(Date startDate, Date endDate) {
        String formattedDateRange;

        // Get a comparison result to determine if the Months are the same
        String startDateMonth = MONTH_FORMAT.format(startDate);
        String endDateMonth = MONTH_FORMAT.format(endDate);

        if (startDateMonth.equals(endDateMonth))
        {
            formattedDateRange = DAY_FORMAT.format(startDate) + DASH
                    + DAY_MONTH_FORMAT.format(endDate).toUpperCase();
        }
        else
        {
            // Months don't match, split the string across both months
            formattedDateRange = DAY_MONTH_FORMAT.format(startDate).toUpperCase()
                    + DASH + DAY_MONTH_FORMAT.format(endDate).toUpperCase();
        }
        return formattedDateRange;
    }
}

今年怎么样?例如,从2010年8月20日到2011年8月23日,情况如何?从2010年12月31日到2011年1月1日怎么样?2012年1月1日?首先,这个类可以格式化一个日期,但不能格式化一个范围,其次,格式化程序是相当静态的,它不能根据输入改变格式。+1-我只是写了我的推荐使用。现在你得到了我的支持票:)@SorcyCat——据我所说,它是有效的。如果他们只是讨论在测试中注释两行以使测试gwt兼容,那么我认为这是值得尝试的。@Andreas_D:有多个项目试图将joda移植到gwt,但它们有不同类型的问题。另请参见有关GWT组的信息。对我来说,在GWT中使用JodaAPI还不现实。OP可以使用一个简单的正则表达式来检查月份是否匹配,并相应地进行修剪。回答得很好。在我的情况下,如果结束日期是明年,范围可能会影响。示例:20年3月至21年1月4日。我做了一个要点,添加了年份处理,并将datetimeformat类替换为simpledateformat: