问题描述
在我的应用中,我以 yyyy-MM-dd
的形式接收来自网络服务的日期(例如 2016-03-05
),我需要将其格式化为[缩写月份] [日期],例如 3月5日
。另外,我有一个开始日期和结束日期,并希望将它们显示为日期范围,除非两个日期都相同。
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:
-
当我的结束日期是开始日期的第二天时,
formatDateRange()
仅显示格式化的开始日期。例如,如果开始日期是
2016-03-05
,结束日期是2016-03-06
,则该方法返回3月5日
(但应为3月5日-3月6
)。为什么会发生这种情况?
When my end date is the day after my start date,
formatDateRange()
only shows the formatted start date. For example, if start date is2016-03-05
and end date is2016-03-06
, the method returnsMar 5
(but it should beMar 5 - Mar 6
). Why does this happen?
当结束日期在同一月份时,则不显示该月份。例如,如果开始日期为 2016-03-05
,结束日期为
2016-03-12
,该方法返回 3月5日至12日
。有没有办法让它显示 3月5日-3月12日
?
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 is2016-03-12
, the method returns Mar 5 - 12
. Is there a way to make it show Mar 5 - Mar 12
instead?
这是我的代码:
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);
推荐答案
tl; dr
tl;dr
LocalDate.parse( inputStart )
.format( DateTimeFormatter.ofPattern( "MMM d" ).withLocale( Locale.US ) )
+ " - " +
LocalDate.parse( inputStop )
.format( DateTimeFormatter.ofPattern( "MMM d" ).withLocale( Locale.US ) )
详细信息
您可以使用java.time类非常简单地执行此操作,而不用麻烦的旧旧日期时间类( Date
, SimpleDateFormat
)和外部库DateUtils。
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.
您的输入日期字符串使用标准格式。解析/生成字符串时,java.time类默认使用标准格式。因此,无需指定格式设置模式。
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.
类表示没有日期和时区的仅日期值。 / p>
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" );
要生成仅包含缩写的月份名称和月份的字符串,请使用 DateTimeFormatter
。
To generate a string with just the abbreviated month name and day-of-month, use DateTimeFormatter
.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM d" );
指定来确定(a)用于翻译名称的人类语言日期,月份名称等;以及(b)决定缩写,大写,标点,分隔符等问题的文化规范。
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.
问 LocalDate
生成代表其值的字符串。
Ask the LocalDate
to generate a string representing its value.
String output = start.format( f ) + " - " + stop.format( f ) ;
MonthDay
听起来像您可能感兴趣的类,如果需要使用一个月和一个月中的一天但没有任何年份的概念。
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 ) ;
或使用 Month
枚举指定
MonthDay start = MonthDay.of( Month.JANUARY , 23 ) ;
MonthDay stop = MonthDay.of( Month.FEBRUARY , 14 ) ;
要生成标准ISO 8601格式的字符串,请调用 toString
。
To generate a string in standard ISO 8601 format, call toString
.
String output = start.toString() ;
或使用与上述相同的 DateTimeFormatter
。
String output = start.format( f );
ISO 8601定义了使用斜杠字符指示时间跨度的格式。因此,您相同的月日值范围将是:
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() ;
关于java.time
框架是内置于Java 8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,,& 。
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
.
的Joda-Time 项目建议迁移到类。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见。并在Stack Overflow中搜索许多示例和说明。规范为。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
从哪里获取java.time类?
Where to obtain the java.time classes?
- ,,然后是
- 内置。
- 具有捆绑实现的标准Java API的一部分。
- Java 9添加了一些次要功能和修复。
- 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.time功能都反向移植到Java 6& nofollow noreferrer> ThreeTen-Backport 。
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- 项目专门为Android改编了 ThreeTen-Backport (上面提到)。
- 请参阅。
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容打下了基础。您可能会在这里找到一些有用的类,例如,,和。
这篇关于格式化日期范围时,DateUtils.formatDateRange()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!