从GregorianCalendar获取星期几

时间:2011-09-23 03:25:51

标签: android date calendar gregorian-calendar

我有一个约会,我需要知道一周中的某一天,所以我使用了一个GregorianCalendar对象,但我找回了一些不正确的日期。

GregorianCalendar calendar = new GregorianCalendar(year, month, day);
int i = calendar.get(Calendar.DAY_OF_WEEK);

我做错了什么?

谢谢!

编辑解决方案:

mont--;
GregorianCalendar calendar = new GregorianCalendar(year, month, day);
int i = calendar.get(Calendar.DAY_OF_WEEK);

    if(i == 2){
        dayOfTheWeek = "Mon";           
    } else if (i==3){
        dayOfTheWeek = "Tue";
    } else if (i==4){
        dayOfTheWeek = "Wed";
    } else if (i==5){
        dayOfTheWeek = "Thu";
    } else if (i==6){
        dayOfTheWeek = "Fri";
    } else if (i==7){
        dayOfTheWeek = "Sat";
    } else if (i==1){
        dayOfTheWeek = "Sun";
    }

3 个答案:

答案 0 :(得分:13)

TimeZone timezone = TimeZone.getDefault();
Calendar calendar = new GregorianCalendar(timezone);
calendar.set(year, month, day, hour, minute, second);

String monthName=calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());//Locale.US);
String dayName=calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());//Locale.US);

答案 1 :(得分:3)

约达时间

我使用Joda-Time库进行所有与日期/时间相关的操作。 Joda考虑到您的语言环境并相应地获得结果:

import org.joda.time.DateTime;
DateTime date = new DateTime(year, month, day, 0, 0, 0);

DateTime date = DateTime().now();

星期几(int):

date.getDayOfWeek();

使用toString()DateTimeFormat选项的星期几(短字符串):

date.toString("EE");

答案 2 :(得分:0)

TL;博士

myGregCal                  // `GregorianCalendar` is a legacy class, supplanted by the modern `java.time.ZonedDateTime` class.
    .toZonedDateTime()     // Convert to `ZonedDateTime`.
    .getDayOfWeek().       // Extract a `DayOfWeek` enum object, one of seven pre-defined objects, one for each day of the week.
    .getDisplayName(       // Automatically localize, generating a `String` to represent the name of the day of the week.
        TextStyle.SHORT ,  // Specify how long or abbreviated.
        Locale.US          // Locale determines the human language and cultural norms used in localization.
    )                      // Returns a `String` object.
  

周一

LocalDate.now( ZoneId.of( "Africa/Tunis" ) )              // Get current date for people in a certain region, without time-of-day and without time zone.
.getDayOfWeek()                                           // Extract a `DayOfWeek` enum object.
.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH )  // Generate a string representing that day-of-week, localized using the human language and cultural norms of a particular locale.
  

Lundi将军

java.time

通过调用添加到旧类的新方法,将麻烦的旧遗留java.util.GregorianCalendar对象转换为现代java.time对象。

ZonedDateTime zdt = myGregCal.toZonedDateTime();

获取该时区内那个时刻的DayOfWeek枚举对象。

DayOfWeek dow = zdt.getDayOfWeek();
  

dow.toString():WEDNESDAY

在代码周围传递这些DayOfWeek对象,而不是传递1-7之类的整数或像“MON”这样的字符串。通过使用枚举对象,您可以使代码更加自我记录,提供类型安全性,并确保一系列有效值。

要向用户演示,请让DayOfWeek对象将星期几的名称翻译为Locale中定义的人类语言。

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

mercredi

关于 java.time

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

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

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

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

从哪里获取java.time类?

相关问题