java中如何取得当天是本年的第多少周?

RT
2024-11-30 02:49:07
推荐回答(5个)
回答1:

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class GetWeek {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(getWeek(new Date()));
}

public static int getWeek(Date date) {
GregorianCalendar g = new GregorianCalendar();
g.setTime(date);
return g.get(Calendar.WEEK_OF_YEAR);//获得周数
}
}

回答2:

import java.util.Calendar;


public class Tesat3 {
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
int i = c.get(Calendar.WEEK_OF_YEAR);
System.out.println(i);
}

}

回答3:

Calendar curr = Calendar.getInstance();
int week=curr.get(Calendar.WEEK_OF_YEAR);
System.out.println("今天是今年的第几周:"+week);

回答4:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(format.format(System.currentTimeMillis()));
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));

回答5:

// jdk1.8 周日开一周第一天时
// ********格式化 每年的周数**************
int week = LocalDate.of(2019, 12, 30).get(WeekFields.ISO.weekOfWeekBasedYear());
int year = LocalDate.of(2019, 12, 30).get(WeekFields.ISO.weekBasedYear());
String formatWeek = new DecimalFormat("00").format(week);
System.out.println(year + "-" + formatWeek);// 2020-01
// ********格式化 每年的周数**************