鉴于此代码和我的假设,即2013年11月的第三个星期五是第15个:
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上运行它的终端的副本:
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上运行的终端的副本:
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(TM)SE运行时环境(版本1.7.0_55-b13)在Linux dist上运行它,他也得到了第22个。
我完全不知道为什么会这样 - 有人可以说清楚。
答案 0 :(得分:5)
问题可能是特定于语言环境。检查日历文档
第一周
日历使用两个来定义特定于语言环境的七天工作周 参数:一周的第一天和第一天的最小天数 一周(从1到7)。
我建议您在两个平台上打印calendar.getMinimalDaysInFirstWeek()
并进行比较