【问题标题】:Wrong "week of year" in AndroidAndroid中错误的“一年中的一周”
【发布时间】:2011-09-01 16:08:53
【问题描述】:

从日期返回的“一年中的周数”是错误的。

这是我的代码:

Calendar c = Calendar.getInstance();
c.setTime(my_date);
int num_week = c.get(Calendar.WEEK_OF_YEAR);

如果 my_date(日期类型)是 01/01/2011,我认为“一年中的一周”是 1。但它返回 52。

我尝试使用这些方法进行测试,但没有得到任何结果:

c.setFirstDayOfWeek(6);
c.setMinimalDaysInFirstWeek(1)

如果有趣的话,我来自西班牙,我们的一周从星期一开始。

为了获得正确的结果,我需要做些什么吗?

谢谢!

【问题讨论】:

  • 一年中的第一周和最后一周取决于区域设置 - 请参阅上面的链接以获取重复的问题和解释。
  • @Metro:但是将第一周的最少天数设置为 1应该 解决它。

标签: android date calendar


【解决方案1】:

这可能是 Android/Harmony 特有的。例如,这适用于桌面 Java:

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2011, 0, 1, 0, 0, 0);
        System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // Prints 52
        calendar.setMinimalDaysInFirstWeek(1);
        System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // Prints 1
    }
}

您能否确认完全相同的代码(模日志记录选项)在 Android 上记录了两次 52?

【讨论】:

  • 发生了什么:Calendar.getInstance(Locale.SPAIN)?
  • @Metro:new Locale("es") 我两次都得到 1。使用new Locale("es", "ES"),我得到 52 和 1。
  • 有趣。我很想知道这是否是特定于 Android 的。
  • 很好奇并针对 Android 2.3 上的每个可用语言环境运行您的示例代码。所有值都以 52/52 错误返回。然后针对Android 3.2运行。除了 "fa_x" 和大部分 "ar_x" 之外的所有语言环境再次以 52/52 错误返回(fa_x,ar_x 返回 1/1)。
  • @Metro:感谢您的测试。好奇的。您是否在桌面上尝试过 Apache Harmony 项目?
【解决方案2】:

这里可以通过oracle查看参考

https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html

我已经设置了一个快速解决方案来查找当天的周数。您可以按照自己的方式进行更改和优化。也可以根据你方便的 GMT 值设置

public static int getWeeksOfMonth() {

    DATESTATUS = false;
    VALUESTATUS = false;
    int weekCount;
    WEEK_OF_MONTH= -1;

    // get the supported ids for GMT+04:00 (Pacific Standard Time)
    String[] ids = getAvailableIDs(4 * 60 * 60 * 1000);
    // if no ids were returned, something is wrong. get out.
    if (ids.length == 0)
        return WEEK_OF_MONTH;

    // create a Pacific Standard Time time zone
    SimpleTimeZone pdt = new SimpleTimeZone(4 * 60 * 60 * 1000, ids[0]);

    // create a GregorianCalendar with the Pacific Daylight time zone
    // and the current date and time
    Calendar calendar = new GregorianCalendar(pdt);
    Date trialTime = new Date();
    calendar.setTime(trialTime);

    weekCount = calendar.get(Calendar.WEEK_OF_YEAR);

    return recursiveWeekCountCheck(calendar, weekCount);
}

private static int recursiveWeekCountCheck(Calendar calendar, int weekCount) {
    if (calendar.get(Calendar.MONTH) == Calendar.DECEMBER && weekCount == 1) {
        DATESTATUS = true;
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        weekCount = calendar.get(Calendar.WEEK_OF_YEAR);
        recursiveWeekCountCheck(calendar, weekCount);
    }
    if (!VALUESTATUS){
        VALUESTATUS = true;
        if (DATESTATUS) {
            weekCount++;
            WEEK_OF_MONTH = weekCount;
        } else {
            WEEK_OF_MONTH = weekCount;
        }
    }
    return WEEK_OF_MONTH;
}

最后只需调用方法getWeeksOfMonth();

【讨论】:

    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 2011-12-10
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多