简体   繁体   English

Java SWT DateTime:确定星期几

[英]Java SWT DateTime: Determine day of Week

I have a DateTime widget with 3/9/2017. 我有一个3/9/2017的DateTime小部件。 Based on the documentation for DateTime, I don't see a way to determine the day of the week. 根据DateTime的文档,我没有确定星期几的方法。 I'll eventually need a string parsed in this format "Wed Feb 22 14:57:34 UTC 2017" from the DateTime widget, but the first step is to get the day of the week. 我最终将需要从DateTime小部件中以这种格式“ Wed Feb 22 14:57:34 UTC 2017”解析的字符串,但是第一步是获取星期几。 Is there a way to do this outside of making my own function? 除了实现自己的功能之外,还有其他方法可以做到吗? And if not, what would you recommend as the best approach for the function, since days of the week are not consistent to dates from year to year? 如果不是这样,由于一周中的某几天与年份之间的日期不一致,您会推荐什么作为该功能的最佳方法?

Let me know if you need any addition information. 让我知道您是否需要任何其他信息。

Thank you! 谢谢!

Use java.util.Calendar : 使用java.util.Calendar

    Calendar c = Calendar.getInstance();
    c.setTime(yourDate);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
  • if you need the output to be Tue rather than 3 (Days of week are indexed starting at 1), instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) ( EE meaning "day of week, short version") 如果您需要输出为Tue而不是3(索引星期几从1开始),而不是通过日历,只需重新格式化字符串: new SimpleDateFormat("EE").format(date)EE表示“星期几,简短版”)

Documentation 文献资料

tl;dr tl; dr

LocalDate.of( 2017 , Month.MARCH , 9 )
    .getDayOfWeek()
    .getDisplayName( TextStyle.FULL , Locale.ITALY )

Or… 要么…

OffsetDateTime.now( ZoneOffset.UTC )
    .format( DateTimeFormatter.RFC_1123_DATE_TIME )

java.time java.time

The modern approach uses the java.time classes that supplanted the troublesome old date-time classes. 现代方法使用了java.time类, 该类取代了麻烦的旧日期时间类。

The DayOfWeek enum defines seven objects, one for each day of the week, Monday-Sunday. DayOfWeek枚举定义了七个对象,一个对象用于每周的周一至周日的每一天。

The LocalDate class represents a date-only value without time-of-day and without time zone. LocalDate类表示没有日期和时区的仅日期值。

DayOfWeek dow = LocalDate.now().getDayOfWeek() ;

Generate a string of the localized name. 生成本地化名称的字符串。

String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;  // Or Locale.US etc.

To generate your longer string for a moment, use DateTimeFormatter to specify a custom pattern, use a built-in pattern, or automatically localize. 要暂时生成较长的字符串,请使用DateTimeFormatter指定自定义模式,使用内置模式或自动本地化。

String output = OffsetDateTime.now( ZoneOffset.UTC ).format( DateTimeFormatter.RFC_1123_DATE_TIME ) ;

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 在哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM