64590

DateUtils.formatDateRange() issues when formatting date range

Question:

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 <a href="http://file:///Users/jdkoren/android-sdk-macosx/docs/reference/android/text/format/DateUtils.html#formatDateRange(android.content.Context,%20long,%20long,%20int)" rel="nofollow">DateUtils.formatDateRange()</a>, which should take care of my requirements and provide proper localization, but I'm running into two problems:

<ol><li>

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?

</li> <li>

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?

</li> </ol>

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);

Answer1:

In the first case, the date formatter is taking your end date in the range as <em>exclusive</em> (as opposed to <em>inclusive</em> 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.

Answer2:

tl;dr

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

Mar 5 - Mar 6

</blockquote>

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 <a href="https://en.wikipedia.org/wiki/ISO_8601" rel="nofollow">ISO 8601</a> format. The java.time classes use the standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

The <a href="https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html" rel="nofollow">LocalDate</a> 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 <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Locale.html" rel="nofollow">Locale</a> 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 ) ; <blockquote>

Jan 23 - Feb 14

</blockquote> <h2>MonthDay</h2>

Sounds like you may be interested in the <a href="https://docs.oracle.com/javase/8/docs/api/java/time/MonthDay.html" rel="nofollow">MonthDay</a> 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() ; <blockquote>

--01-23

</blockquote>

Or use the same DateTimeFormatter seen above.

String output = start.format( f ); <blockquote>

Jan 23

</blockquote>

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() ; <blockquote>

--01-23/--02-14

</blockquote> <hr />

About java.time

The <a href="http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html" rel="nofollow">java.time</a> framework is built into Java 8 and later. These classes supplant the troublesome old <a href="https://en.wikipedia.org/wiki/Legacy_system" rel="nofollow">legacy</a> date-time classes such as <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Date.html" rel="nofollow">java.util.Date</a>, <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html" rel="nofollow">Calendar</a>, & <a href="http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html" rel="nofollow">SimpleDateFormat</a>.

The <a href="http://www.joda.org/joda-time/" rel="nofollow">Joda-Time</a> project, now in <a href="https://en.wikipedia.org/wiki/Maintenance_mode" rel="nofollow">maintenance mode</a>, advises migration to the <a href="http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html" rel="nofollow">java.time</a> classes.

To learn more, see the <a href="http://docs.oracle.com/javase/tutorial/datetime/TOC.html" rel="nofollow">Oracle Tutorial</a>. And search Stack Overflow for many examples and explanations. Specification is <a href="https://jcp.org/en/jsr/detail?id=310" rel="nofollow">JSR 310</a>.

Where to obtain the java.time classes?

<ul><li><a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8" rel="nofollow"><strong>Java SE 8</strong></a>, <a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9" rel="nofollow"><strong>Java SE 9</strong></a>, and later <ul><li>Built-in. </li> <li>Part of the standard Java API with a bundled implementation.</li> <li>Java 9 adds some minor features and fixes.</li> </ul></li> <li><a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_6" rel="nofollow"><strong>Java SE 6</strong></a> and <a href="https://en.wikipedia.org/wiki/Java_version_history#Java_SE_7" rel="nofollow"><strong>Java SE 7</strong></a> <ul><li>Much of the java.time functionality is back-ported to Java 6 & 7 in <a href="http://www.threeten.org/threetenbp/" rel="nofollow"><strong><em>ThreeTen-Backport</em></strong></a>.</li> </ul></li> <li><a href="https://en.wikipedia.org/wiki/Android_(operating_system)" rel="nofollow"><strong>Android</strong></a> <ul><li>The <a href="https://github.com/JakeWharton/ThreeTenABP" rel="nofollow"><strong><em>ThreeTenABP</em></strong></a> project adapts <em>ThreeTen-Backport</em> (mentioned above) for Android specifically.</li> <li>See <a href="https://stackoverflow.com/q/38922754/642706" rel="nofollow"><em>How to use ThreeTenABP…</em></a>.</li> </ul></li> </ul>

The <a href="http://www.threeten.org/threeten-extra/" rel="nofollow"><strong>ThreeTen-Extra</strong></a> 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 <a href="http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/Interval.html" rel="nofollow">Interval</a>, <a href="http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearWeek.html" rel="nofollow">YearWeek</a>, <a href="http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/YearQuarter.html" rel="nofollow">YearQuarter</a>, and <a href="http://www.threeten.org/threeten-extra/apidocs/index.html" rel="nofollow">more</a>.

Recommend

  • how to use d3.time.scale to create label for each hour of day and each day of week
  • How to get execution plan for a running query in postgresql?
  • Changing DateField to DateTimeField in Django
  • Convert Python date to Unix timestamp
  • How to shorten this expression using regex
  • How to enable Drag a Marker (Android Map Api v2) after a single Touch?
  • Sql indexes vs full table scan
  • How do I fix “The program issued a command but the command length is incorrect.” error when calling
  • How to add specific media query rule when media query combined
  • Error Processing Request: Mage registry key “_singleton/inchoo_notes/feed_updates” already exists
  • iOS 6 dateFromString returns wrong date
  • Disabling sound of embedded flash object with html
  • Migration to HRD - How to convert string-encoded keys to new application
  • HttpServletRequest getLocale returns OS locale not browser locale
  • Yii2: Finding file and getting path in a directory tree
  • KnockoutObservableArray with typed elements in TypeScript
  • Clear activity stack before launching another activity
  • DIV instruction jumping to random location?
  • Android changing fragment order inside FragmentPagerAdapter
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • How do I configure context broker accept post requests from my remote sensor?
  • Android activity accessing service's static reference before the service is ready
  • Disable Enter in editText android
  • Cannot resolve symbol 'MyApi'
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Android screen density dpi vs ppi
  • Regex thinks I'm nesting, but I'm not
  • How would I use PHP exceptions to define a redirect?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • How to extract text from Word files using C#?
  • Release, debug version and Authorization Google?
  • PHP: When would you need the self:: keyword?
  • Acquiring multiple attributes from .xml file in c#
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • need help with bizarre java.net.HttpURLConnection behavior
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • reshape alternating columns in less time and using less memory
  • How can I use threading to 'tick' a timer to be accessed by other threads?