-->

DateUtils.formatDateRange() issues when formatting

2019-09-06 00:45发布

问题:

In my app, I receive dates from a webservice in the form yyyy-MM-dd (e.g. 2016-03-05) and I need to format them as [abbreviated month] [date], e.g. Mar 5. Additionally, I have a start and end date and want to show them as a date range, unless both dates are the same.

Currently I'm using DateUtils.formatDateRange(), which should take care of my requirements and provide proper localization, but I'm running into two problems:

  1. When my end date is the day after my start date, formatDateRange() only shows the formatted start date. For example, if start date is 2016-03-05 and end date is 2016-03-06, the method returns Mar 5 (but it should be Mar 5 - Mar 6). Why does this happen?

  2. When the end date is in the same month, the month is not shown. For example, if start date is 2016-03-05 and end date is 2016-03-12, the method returns Mar 5 - 12. Is there a way to make it show Mar 5 - Mar 12 instead?

Here is my code:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate, endDate;
try {
    startDate = sdf.parse(startDateString);
    endDate = sdf.parse(endDateString);
} catch (ParseException ignored) {
    return null;
}

int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH;
return DateUtils.formatDateRange(context, startDate.getTime(), endDate.getTime(), flags);

回答1:

In the first case, the date formatter is taking your end date in the range as exclusive (as opposed to inclusive in the range). If you simply add one millisecond to the end date, you will see the ranges you expect, because now the date range includes the point in time at midnight for the end date.

In the second case, I'm afraid you are up to the current locale rules for date formatting. You pretty much have to accept what Android thinks is the best formatting, or come up with your own rules for each locale that you want to support.



回答2:

tl;dr

LocalDate.parse( inputStart )
         .format( DateTimeFormatter.ofPattern( "MMM d" ).withLocale( Locale.US ) )
+ " - " + 
LocalDate.parse( inputStop )
         .format( DateTimeFormatter.ofPattern( "MMM d" ).withLocale( Locale.US ) )

Mar 5 - Mar 6

Details

You can do this quite simply with the java.time classes rather than the troublesome old legacy date-time classes ( Date, SimpleDateFormat ) and the external library DateUtils.

Your input date strings use the standard ISO 8601 format. The java.time classes use the standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate start = LocalDate.parse( "2017-01-23" );
LocalDate stop = LocalDate.parse( "2017-02-14" );

To generate a string with just the abbreviated month name and day-of-month, use DateTimeFormatter.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM d" );

Specify a Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

f = f.withLocale( Locale.US ) ;  // Or Locale.CANADA_FRENCH, Locale.UK, Locale.ITALY, etc.

Ask the LocalDate to generate a string representing its value.

String output = start.format( f ) + " - " + stop.format( f ) ;

Jan 23 - Feb 14

MonthDay

Sounds like you may be interested in the MonthDay class if needing to work with the concept of a month and a day-of-month but without any year.

MonthDay md = MonthDay.of( 1 , 23 ) ;

Or use the Month enum to specify the month argument.

MonthDay start = MonthDay.of( Month.JANUARY , 23 ) ;
MonthDay stop = MonthDay.of( Month.FEBRUARY , 14 ) ;

To generate a string in standard ISO 8601 format, call toString.

String output = start.toString() ;

--01-23

Or use the same DateTimeFormatter seen above.

String output = start.format( f );

Jan 23

The ISO 8601 defines a format indicating a span of time using a slash character. So your same range of month-day values would be:

String output = start.toString() + "/" + stop.toString() ;

--01-23/--02-14


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.