gpt4 book ai didi

java - 在 Java 中创建自定义周计数器?

转载 作者:行者123 更新时间:2023-12-01 15:47:40 27 4
gpt4 key购买 nike

我正在尝试创建一个自定义周计数器,但遇到了很多麻烦,并且感觉我做错了。该方法应接受 yyyy-MM-dd 格式的字符串日期并返回周数。周计数器从 2000 年 10 月 1 日开始。一周从周五开始,周四结束。前 2 位数字代表年份,后 2 位数字代表星期。因此,本周将是 1143(11 代表年份,43 代表自 10 月 1 日以来的周数)。

这是我到目前为止所得到的:

public static String get_week(String date){

try{
Calendar first_dt = Calendar.getInstance();
first_dt.set(1999, 10, 01);
long first_dt_milliseconds = first_dt.getTimeInMillis();

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date format_date = (Date)formatter.parse(date);

SimpleDateFormat month = new SimpleDateFormat("MM");
SimpleDateFormat year = new SimpleDateFormat("yyyy");

long drop_dt_milliseconds = format_date.getTime() - first_dt_milliseconds;
long drop_dt_years = drop_dt_milliseconds / (24 * 60 * 60 * 1000) / 365;

Calendar year_ago = Calendar.getInstance();
year_ago.set(Integer.parseInt(year.format(format_date))-1, 10, 01);

long year_ago_milliseconds = year_ago.getTimeInMillis();

long year_ago_diff = format_date.getTime() - year_ago_milliseconds;
year_ago_diff = year_ago_diff / (24 * 60 * 60 * 1000) / 7;

if (month.format(format_date).equals("10") || month.format(format_date).equals("11") || month.format(format_date).equals("12")){
date = drop_dt_years+1+""+year_ago_diff;
}
else{
date = year_ago_diff;
}
}catch(Exception e){
e.printStackTrace();
}

return date;
}

最佳答案

我用了Joda-Time因为它比 Java 的内置日期和时间齿轮更容易混淆

编辑 - 新代码,在 ChssPly 的建议中滚动,并修复了 10 月 1 日到 1 月 1 日之间的几周的问题。另请查看 X-Zero 的建议,在 Joda-Time 中创建自定义年表,可能是一个有趣的方法。

import org.joda.time.DateMidnight;
import org.joda.time.Weeks;
import org.joda.time.Years;

public class Main {

private String getWeek (DateMidnight dt2) {
DateMidnight dt = new DateMidnight(2000,10,1);

// First get the number of elapsed years, ChssPly76's way
int yearz = Years.yearsBetween(dt, dt2).getYears();
/*
* We now need the number of weeks in the current year, which can be
* calculated using the Weeks class.
*/
int yearOffset = 1;
// But if the new date is Oct 1 thru Dec 12 year must remain the same
if (!dt2.isBefore (new DateMidnight(dt2.getYear(),10,1))) {
yearOffset = 0;
}

int weekz = Weeks.weeksBetween(dt.withYear(dt2.getYear()-yearOffset), dt2).getWeeks();
return(yearz + " " + weekz);
}

private void test (DateMidnight testDate) {
System.out.println("For date " + testDate + " years/weeks = " + getWeek(testDate));
}

private void run() {
test (new DateMidnight());
test (new DateMidnight(2010,10,8));
test (new DateMidnight(2010,9,30));
test (new DateMidnight(2000,10,1));
}

public static void main(String[] args) {
new Main().run();
}
}

哪些输出

For date 2011-07-26T00:00:00.000+02:00 years/weeks = 10 42
For date 2010-10-08T00:00:00.000+02:00 years/weeks = 10 1
For date 2010-09-30T00:00:00.000+02:00 years/weeks = 9 52
For date 2000-10-01T00:00:00.000+02:00 years/weeks = 0 0

可能稍微复杂一点的返回对象会更好......

关于java - 在 Java 中创建自定义周计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6822656/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com