我有两个日期——开始和结束。我想对它们进行格式化,以便如果月份匹配,它们会折叠成“20-23 AUG”之类的东西,并且如果它们在月底突破,仍然可以正确格式化,比如“20 SEP - 1 OCT”。是否有任何库可以实现这一点,或者我是否必须手动编写代码规则以使用单个 DateFormats 显示日期范围?
5 回答
这是一个使用JodaTime的解决方案,这是处理 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 时间在 GWT 中不起作用,SimpleDateFormat 也不起作用。无论如何,我已经知道 GWT 中的 DateTimeFormat。主要问题是日期对象 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;
}
}
签出 java 的SimpleDateFormat类。您可以构造一个 SimpleDateFormat 将您的日期模式作为字符串传递。
要添加到 Jeroen 的答案,您将使用 SimpleDateFormat 两次,一次用于范围的每一端。
默认情况下,Java 中没有日期范围这样的类型,因此没有一个 DateFormats 适用于这种情况的字符串表示。
我认为最好的方法是创建 TimeSpan 或 TimeDiff 类型并将方法覆盖到字符串。或者,如果您还需要两个解析,请按照您的建议创建一个 DateFormmater。