如何获得一周中的本地化

时间:2013-05-05 17:44:39

标签: android simpledateformat

我无法在SimpleDateFormat

中找到符号

如何获取当天的本地化名称?

例如:星期一:1。星期几;星期二:2月,星期三:3。星期日.....

我希望获得演示文稿编号“1”而不是星期一......

2 个答案:

答案 0 :(得分:1)

根据other questions,您不需要SimpleDateFormat来获取一周中的数字日期 - 这是由Calendar直接通过DAY_OF_WEEK字段(从1到1)提供的7其中1是SUNDAY,7是星期六):

Calendar today = Calendar.getInstance();
int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
// Day of Week is a number between 1 and 7 where 1 is Sunday.
int dayOfWeekMondayFirst = (dayOfWeek + 5) % 7 + 1;

答案 1 :(得分:0)

tl; dr

对于数字1-7,表示今天的周一至周日:

LocalDate.now().getDayOfWeek().getValue() 

有关星期几的本地化名称:

LocalDate                          // Represent a date-only, without time-of-day and without time zone.
.now(                              // Get today’s current date as seen in the wall-clock time used by the people of a particular region (a time zone).
    ZoneId.of( "Africa/Tunis" )    // Specify time zone.
)                                  // Returns a `LocalDate`.
.getDayOfWeek()                    // Returns one of seven `DayOfWeek` enum objects.
.getDisplayName(                   // Localize the name of the day-of-week.
     TextStyle.FULL ,              // How long or abbreviated should the localized string be.
     Locale.UK                     // Specify a `Locale` to determine the human language and cultural norms to use in localizing.
)                                  // Returns a string.
  

星期一

java.time

现代方法使用 java.time 类,该类早就取代了可怕的旧式日期时间类,例如SimpleDateFormat

要获取今天的星期几,我们需要日期。

LocalDate

LocalDate类表示没有日期,没有time zoneoffset-from-UTC的仅日期值。

时区对于确定日期至关重要。在任何给定时刻,日期都会在全球范围内变化。例如,Paris France午夜之后的几分钟是新的一天,而Montréal Québec仍然是“昨天”。

如果未指定时区,则JVM隐式应用其当前的默认时区。该默认值可能在运行时(!)期间change at any moment,因此您的结果可能会有所不同。最好将desired/expected time zone明确指定为参数。

continent/region的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用2-4个字母的缩写,例如ESTIST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

如果要使用JVM的当前默认时区,请提出要求并作为参数传递。如果省略,则会隐式应用JVM的当前默认值。最好明确一点,因为默认值可能会在运行时的任何时候被JVM中任何应用程序的任何线程中的任何代码更改。

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

DayOfWeek

DayOfWeek枚举预定义了七个对象的集合,每个星期中的每个对象一个。

LocalDate对象询问其DayOfWeek

DayOfWeek dow = ld.getDayOfWeek() ;

询问DayOfWeek对象以自动本地化其名称。 DayOfWeek::getDisplayName方法将日期的名称翻译成Locale指定的任何人类语言,例如Locale.USLocale.CANADA_FRENCH

String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ); 
  

lundi

或者,使用美国英语。

String output = dow.getDisplayName( TextStyle.FULL , Locale.US ); 
  

星期一

要获取星期几的数量(星期一至星期日为ISO 8601标准的1-7,请向DayOfWeek枚举对象获取其值。

int dowNumber = dow.getValue() ;  // 1-7 for Monday-Sunday.

要将数字1-7作为较大的格式设置的一部分,请按照DateTimeFormatter类的指示使用ec


关于 java.time

java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat

目前位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

在哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore