“此日期是每月的第三个星期四吗?” -Java库? [英] "is this date the third thursday of the month?" - Java Library?

查看:115
本文介绍了“此日期是每月的第三个星期四吗?” -Java库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在管道中有几十个积压请求,例如

I've got a few dozen backlog requests in the pipeline like

'I need this functionality to run on the third Thursday of every month, and the first Wednesday of every other month...'

我已经有一个每天运行的函数,我只需要添加 isThirdSundayOfMonth(date)位即可,然后结束。

I've already got a function that runs every day, i just need the: isThirdSundayOfMonth(date) bit to append onto then end.

花更少的时间来考虑公历和时区的细微差别,我的生活就会越好。

The less time I spend considering the nuances of the Gregorian calendar and timezones, the better my life is.

有人知道Java库可以简化这种计算吗?没有xml配置或框架或其他任何内容。只需一个.Jar和一个有文档的,可读的API就是完美的。

Anyone know a Java library that simplifies this sort of calculation? No xml config or frameworks or anything. Just a .Jar and a documented, readable API would be perfect.

任何帮助将不胜感激。

Any help would be much appreciated.

推荐答案

完整概述:



在Java-8中(新标准):

LocalDate input = LocalDate.now(); // using system timezone
int ordinal = 3;
DayOfWeek weekday = DayOfWeek.SUNDAY;

LocalDate adjusted = 
  input.with(TemporalAdjusters.dayOfWeekInMonth(ordinal, weekday));
boolean isThirdSundayInMonth = input.equals(adjusted);

在Joda-Time中(受欢迎的第三方图书馆):

LocalDate input = new LocalDate(); // using system timezone
int ordinal = 3;
int weekday = DateTimeConstants.SUNDAY;

LocalDate start = new LocalDate(input.getYear(), input.getMonthOfYear(), 1);
LocalDate date = start.withDayOfWeek(weekday);
LocalDate adjusted = (
  date.isBefore(start)) 
  ? date.plusWeeks(ordinal) 
  : date.plusWeeks(ordinal - 1);
boolean isThirdSundayInMonth = input.equals(adjusted);

使用 java.util.GregorianCalendar (旧标准):

Using java.util.GregorianCalendar (old standard):

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar input = new GregorianCalendar();
int ordinal = 3;
int weekday = Calendar.SUNDAY;

GregorianCalendar start =
    new GregorianCalendar(input.get(Calendar.YEAR), input.get(Calendar.MONTH), 1);
int dow = start.get(Calendar.DAY_OF_WEEK); // Sun=1, Mon=2, ...
int delta = (weekday - dow);
if (delta < 0) {
    delta += 7;
}
start.add(Calendar.DAY_OF_MONTH, delta + (ordinal - 1) * 7);
String comp1 = sdf.format(input.getTime());
String comp2 = sdf.format(start.getTime());
boolean isThirdSundayInMonth = comp1.equals(comp2);

即使使用最丑陋的库,也可能有解决方案;-)我使用字符串比较来摆脱任何时区影响或一天中的时间部分(包括毫秒)。仅基于年,月和日的字段进行现场比较也是个好主意。

Even with the ugliest library a solution is possible ;-) I have used a string comparison in order to get rid of any timezone effects or time-of-day-parts including milliseconds. A field-wise comparison based only on year, month and day-of-month is also a good idea.

使用Time4J(我自己的第三方-库):

PlainDate input = 
  SystemClock.inLocalView().today(); // using system timezone
Weekday weekday = Weekday.SUNDAY;

PlainDate adjusted = 
  input.with(PlainDate.WEEKDAY_IN_MONTH.setToThird(weekday));
boolean isThirdSundayInMonth = input.equals(adjusted);

这篇关于“此日期是每月的第三个星期四吗?” -Java库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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