错误的“一年中的星期”在安卓中

发布于 2024-12-02 09:33:10 字数 419 浏览 0 评论 0原文

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

这是我的代码:

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)

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

我需要做些什么才能获得正确的结果吗?

谢谢!

The number of "week of year" returned from a Date is wrong.

This is my code:

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

If my_date (type Date) is 01/01/2011, I supposed that "week of year" is 1. But it returned 52.

I try to test with these methods but I don't obtain anything:

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

If It's interesting, I'm from Spain, and our week begin on Monday.

Have I to do anything for obtain right results?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡の花香 2024-12-09 09:33:10

这可能是 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 两次吗?

This may be Android/Harmony-specific. For example, this works for me with desktop 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
    }
}

Can you confirm that the exact same code (modulo logging options) logs 52 twice on Android?

以可爱出名 2024-12-09 09:33:10

这里可以查看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();

Here you can view the reference by oracle

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

And I have placed a quick solution to find the week count of current day. You can alter and optimize as your way. Also set according to your convenient GMT value

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;
}

At the end just call the method getWeeksOfMonth();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文