这个java日期算术代码在不同平台上给出了不同的答案 [英] This java date arithmetic code gives different answers on different platforms

查看:90
本文介绍了这个java日期算术代码在不同平台上给出了不同的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此代码和我的假设,即2013年11月的第三个星期五是第15个:

Given this code and my assumption that the third Friday of November 2013 is the 15th:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import static java.lang.String.format;

public class Wat {

    public static void main(String[] args) {
        final TimeZone utc = TimeZone.getTimeZone("UTC");
        final GregorianCalendar cal = new GregorianCalendar(utc);

        //This is inlined code that sets up a date
        cal.set(Calendar.YEAR, 2013);
        cal.set(Calendar.MONTH, 10);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        //This is my third-friday-in-month code
        cal.set(Calendar.WEEK_OF_MONTH, 3);
        cal.set(Calendar.DAY_OF_WEEK, 6);

        final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        formatter.setTimeZone(utc);
        final String thirdFridayOfNovember2013 = formatter.format(cal.getTime());
        System.out.println(
            format("Third Friday of November 2013 is %s? %s!", 
                thirdFridayOfNovember2013, 
                thirdFridayOfNovember2013.equals("15-11-2013")));
    }
}

这是我的终端上运行它的副本Linux:

Here is a copy from my terminal of running it on Linux:

ubuntu@ip-10-47-142-178:~$ uname -a
Linux ip-10-47-142-178 3.2.0-61-virtual #93-Ubuntu SMP Fri May 2 21:54:33 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
ubuntu@ip-10-47-142-178:~$ java -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)
ubuntu@ip-10-47-142-178:~$ java -classpath . Wat
Third Friday of November 2013 is 15-11-2013? true!
ubuntu@ip-10-47-142-178:~$

这是一个从我在os x上运行的终端复制:

Here is a copy from my terminal of running on os x:

gasp $ uname -a
Darwin Mikes-MacBook-Pro-2.local 13.1.0 Darwin Kernel Version 13.1.0: Wed Apr  2 23:52:02 PDT 2014; root:xnu-2422.92.1~2/RELEASE_X86_64 x86_64
:gasp $ java -version
java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
:gasp $ java -classpath . Wat
Third Friday of November 2013 is 22-11-2013? false!
:gasp $

我的同事接下来我用Java(Linux)运行它)SE运行时环境(版本1.7.0_55-b13),他也得到了第22个。

My colleague next me ran it on a Linux dist with Java(TM) SE Runtime Environment (build 1.7.0_55-b13) and he also got the 22nd.

我完全不知道为什么会这样 - 有人可以请揭晓问题。

I am completely at a loss as to why this is happening - can somebody please shed some light.

推荐答案

问题可能是特定于语言环境。检查日历文档

The issue could be locale specific. Check Calendar documentation


第一周

First Week

日历定义特定于语言环境的七天一周使用两个
参数:一周的第一天和第一个
周的最小天数(从1到7)。

Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7).

我建议你在两个平台上打印 calendar.getMinimalDaysInFirstWeek()并比较

I suggest you print calendar.getMinimalDaysInFirstWeek() in both platforms and compare

这篇关于这个java日期算术代码在不同平台上给出了不同的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆