java传进入参数年和月得到这个月份每一周的起始日期和截止日期

lanse_anqier 2011-06-16 03:43:31
java传进入参数年和月得到这个月份每一周的起始日期和截止日期例如: 2011年2月
第1周2.1-2.6
第2周2.7-2.13
第3周2.14-2.20
第4周2.21-2.27
第5周2.28-2.28
...全文
669 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
shine333 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lanse_anqier 的回复:]

请修改下要按照中国的星期计算方法 一周日期为 周一 至 周日 多谢!!!
[/Quote]
cal.setFirstDayOfWeek(Calendar.MONDAY);
小绵羊 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 shine333 的回复:]

引用 8 楼 alexandertech 的回复:

引用 7 楼 lanse_anqier 的回复:

最好给代码,谢谢啦~~~


我给的就是源代码呵,你拷上去一运行就看到结果了
[/Quote]
坦克哥这是啥表情
飞跃颠峰 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lanse_anqier 的回复:]

请修改下要按照中国的星期计算方法 一周日期为 周一 至 周日 多谢!!!
[/Quote]


import java.util.Calendar;

public class getCalendar {
public static void main(String[] args) {
int year=2011;
int month=5;
Calendar c = Calendar.getInstance();
c.set(year, month-1, 1);
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayinweek = c.get(Calendar.DAY_OF_WEEK);
dayinweek = dayinweek==1?7:dayinweek-1;
int weekcount = 1;
for (int i=1; i<=days; i++) {
if (i==1 || (i-1+dayinweek)%7==1)
System.out.print("第" + weekcount + "周 " + month + "." + i );
if (i==days || (i-1+dayinweek)%7==0) {
System.out.print(" - " + month + "." + i);
weekcount++;
System.out.println();
}
}
}
}
lanse_anqier 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xuanyuanyilu 的回复:]
Java code
import java.util.Calendar;

public class GetWeek {

public static void getWeek(String year,String month){
Calendar cal = Calendar.getInstance();
cal.clear();
……
[/Quote]

请修改下要按照中国的星期计算方法 一周日期为 周一 至 周日 多谢!!!
lanse_anqier 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 alexandertech 的回复:]
引用 7 楼 lanse_anqier 的回复:

最好给代码,谢谢啦~~~


我给的就是源代码呵,你拷上去一运行就看到结果了
[/Quote]


请修改下要按照中国的星期计算方法 一周日期为 周一 至 周日 多谢!!!
shine333 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 alexandertech 的回复:]

引用 7 楼 lanse_anqier 的回复:

最好给代码,谢谢啦~~~


我给的就是源代码呵,你拷上去一运行就看到结果了
[/Quote]
飞跃颠峰 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lanse_anqier 的回复:]

最好给代码,谢谢啦~~~
[/Quote]

我给的就是源代码呵,你拷上去一运行就看到结果了
lanse_anqier 2011-06-16
  • 打赏
  • 举报
回复
最好给代码,谢谢啦~~~
淫生杯具 2011-06-16
  • 打赏
  • 举报
回复
自己写的一个日历测试,传入年月,在控制台打印出日历的样子

public class CreateCalendar {

public static final int EMPTYSEPARATE=5;
public static List<String> weekList=null;

public CreateCalendar(){
init();
}

public void init(){
weekList=new ArrayList<String>();
weekList.add("SUNDAY");
weekList.add("MONDAY");
weekList.add("TUESDAY");
weekList.add("WEDNESDAY");
weekList.add("THURSDAY");
weekList.add("FRIDAY");
weekList.add("SATURDAY");
}

public static boolean vildateDateFormat(String date){
Pattern pattern=Pattern.compile("^\\d{4}-{1}(0?[1-9]|1[0-2])$");
Matcher matcher=pattern.matcher(date);
return matcher.matches();
}

public static void main(String[] args) throws Exception {
String date="2012-12";
new CreateCalendar().run(date);
}

public void run(String date) throws Exception{
if(!vildateDateFormat(date))
throw new Exception("日期格式不匹配!正确的格式为yyyy-mm");
println("当前日期:"+date);
println("");
drawCalendar(date);
}

public void drawCalendar(String date){
calendarHeadPrint();
printlnLine();
calendarBodyPrint(date);
}

public void printVectileLine(){
//print("|");
}


private void calendarBodyPrint(String date) {
String[] dates=date.split("-");
Calendar calendar=new GregorianCalendar(new Integer(dates[0]),new Integer(dates[1])-1,1,0,0,0);
int maxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int minDay=calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
int dayOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
int firstDay=dayOfWeek;
if(firstDay>1){
for (int i = 0; i < firstDay-1; i++) {
if(i==0)
printVectileLine();
printEmpty(weekList.get(i).length());
printEmpty(CreateCalendar.EMPTYSEPARATE);
printVectileLine();
}
}
for (; minDay <= maxDay; minDay++) {
if(dayOfWeek==0)
printVectileLine();
datePrint(weekList.get(dayOfWeek-1), minDay);
printVectileLine();
if(dayOfWeek%7==0){
println("");
printlnLine();
dayOfWeek=0;
}else{
printEmpty(CreateCalendar.EMPTYSEPARATE);
}
dayOfWeek++;
}
}

public void datePrint(String weekDay,int date){
int dateLen=new Integer(date).toString().length();
int weekDayLen=weekDay.length();
printEmpty(weekDayLen/2);
print(new Integer(date).toString());
printEmpty(weekDayLen-weekDayLen/2-dateLen);
}

public void printEmpty(int count){
for (int i = 0; i < count; i++) {
print(" ");
}
}


public void calendarHeadPrint(){
for (int i=0;i<weekList.size();i++) {
String weekDay = weekList.get(i);
print(weekDay);
if(i!=weekList.size()-1)
printEmpty(CreateCalendar.EMPTYSEPARATE);
}
println("");
}

public void printlnLine(){
int weekDaysLen=0;
for (String weekDay : weekList) {
weekDaysLen+=weekDay.length();
}
weekDaysLen+=(weekList.size()-1)*EMPTYSEPARATE;
StringBuffer sb=new StringBuffer();
for (int i = 0; i < weekDaysLen; i++) {
sb.append("-");
}
println(sb.toString());
}

public void print(String str){
System.out.print(str);
}

public void println(String str){
System.out.println(str);
}

}

xuanyuanyilu 2011-06-16
  • 打赏
  • 举报
回复
import java.util.Calendar;

public class GetWeek {

public static void getWeek(String year,String month){
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, Integer.parseInt(year));
cal.set(Calendar.MONTH, Integer.parseInt(month)-1);

int index = 1;
String start = year+"-"+month+"-"+1;
String end = null;
for(int i = 1;i<=cal.getActualMaximum(Calendar.DAY_OF_MONTH);i++){

cal.set(Calendar.DAY_OF_MONTH, i);
int week=cal.get(Calendar.DAY_OF_WEEK);
//System.out.println(year+"-"+month+"-"+i+": "+"星期"+(week-1));

end = year+"-"+month+"-"+i;
if(week-1 == 6){
System.out.println("第"+index+"周: "+start+"至"+end);
start = year+"-"+month+"-"+(i+1);
index ++;
}
}
System.out.println("第"+index+"周: "+start+"至"+end);
}

public static void main(String[] args){
GetWeek.getWeek("2011","6");
}
}
飞跃颠峰 2011-06-16
  • 打赏
  • 举报
回复

import java.util.Calendar;

public class getCalendar {
public static void main(String[] args) {
int year=2011;
int month=5;
Calendar c = Calendar.getInstance();
c.set(year, month-1, 1);
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayinweek = c.get(Calendar.DAY_OF_WEEK);
int weekcount = 1;
for (int i=1; i<=days; i++) {
if (i==1 || (i-1+dayinweek)%7==1)
System.out.print("第" + weekcount + "周 " + month + "." + i );
if (i==days || (i-1+dayinweek)%7==0) {
System.out.print(" - " + month + "." + i);
weekcount++;
System.out.println();
}
}
}
}


licip 2011-06-16
  • 打赏
  • 举报
回复
Calendar
这类里有现存的方法,你去查查看。
Inhibitory 2011-06-16
  • 打赏
  • 举报
回复
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Test {
public static void main(String[] args) {
int year = 2011;
int month = 1; // 1月是0,2月是1

Calendar calendar = new GregorianCalendar(year, month, 1);
while (calendar.get(Calendar.MONTH) == month) {
if (calendar.get(Calendar.DAY_OF_WEEK) == calendar.getFirstDayOfWeek()) {
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
}

calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1);
}
}
}
yimail 2011-06-16
  • 打赏
  • 举报
回复
用java 的Calendar去处理吧,自己动手,结合网上搜索结果,你就能实现出来了

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧