java 获得本周一到周五的日期

比如说现在 怎么获得 2013-4-8 到2013-4-12
2024-11-30 04:41:10
推荐回答(5个)
回答1:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class WorkDay {
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd E");
for (Date date : getWeekDay()) {
System.out.println(dateFormat.format(date));
}
}
public static Date[] getWeekDay() {
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
Date[] dates = new Date[5];
for (int i = 0; i < 5; i++) {
dates[i] = calendar.getTime();
calendar.add(Calendar.DATE, 1);
}
return dates;
}
}

如果每周指的是从星期一开始,那么这个程序可以达到你的要求。


如果每周指得是从星期日开始,那么请说明,我再修改。

回答2:

package com.test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
//cal.set(2002, 2, 28);// 2002-03-28 星期四
System.out.println("今天是:" + cal.get(Calendar.YEAR)
+ (cal.get(Calendar.MONTH) + 1)//Month 值是基于 0 的。例如,0 表示 January
+ cal.get(Calendar.DAY_OF_MONTH));
System.out.println("星期" + getBigWeek(cal.get(Calendar.DAY_OF_WEEK)));
int flag = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE, 1-flag);
/////////////////////
System.out.println("本周从");
System.out.println("" + cal.get(Calendar.YEAR)
+ (cal.get(Calendar.MONTH) + 1)//Month 值是基于 0 的。例如,0 表示 January
+ cal.get(Calendar.DAY_OF_MONTH));
System.out.println("星期" + getBigWeek(cal.get(Calendar.DAY_OF_WEEK)));
/////////////////////////
System.out.println("到");
cal.add(Calendar.DATE, 6);
///////////////////////
System.out.println("" + cal.get(Calendar.YEAR)
+ (cal.get(Calendar.MONTH) + 1)//Month 值是基于 0 的。例如,0 表示 January
+ cal.get(Calendar.DAY_OF_MONTH));
System.out.println("星期" + getBigWeek(cal.get(Calendar.DAY_OF_WEEK)));
}
public static String getBigWeek(int dayofweek) {
//Calendar中1-星期天,2-星期一,3-星期二,4-星期三,5-星期四,6-星期五,7-星期六
String[] wee = { "", "天", "一", "二", "三", "四", "五", "六" };
return wee[dayofweek];
}
}

回答3:

不要这么麻烦好么,都闪开,我来

import java.time.DayOfWeek;
import java.time.LocalDate;

LocalDate today = LocalDate.now();
LocalDate toweekMonday = today.with(DayOfWeek.MONDAY);

就这么简单

回答4:

public static void main(String[] args) {
Calendar cal=new GregorianCalendar();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(new Date());
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
Date first=cal.getTime();
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 4);
Date last=cal.getTime();
SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("本周周一:"+formater.format(first));
System.out.println("本周周五:"+formater.format(last));
}

回答5:

这个是万年历,可以参考一下
import java.util.Scanner;
public class 万年历 {
static Scanner input = new Scanner(System.in);
static int sumYear = 0;
static int sumMonth = 0;
static int inputYear = 0;
static int inputMonth = 0;
static int firstDay = 0;
static int days = 0;
public static void main(String[] args) {
inputDate();
totalyear();
totalmonth();
printCalender();
}
public static void inputDate() {
System.out.println("请输入年份\n");
inputYear = input.nextInt();
System.out.println("请输入月份\n");
inputMonth = input.nextInt();
}
public static void totalyear() {
for (int year = 1900; year < inputYear; year++) {
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
sumYear = sumYear + 366;
} else {
sumYear = sumYear + 365;
}
}
}
public static void totalmonth()
{
for(int month=1;month<=inputMonth;month++)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
sumMonth = sumMonth+31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
sumMonth = sumMonth+30;
break;
case 2:
if(inputYear%400==0||inputYear%4==0&&inputYear%100!=0)
{
days=29;
sumMonth = sumMonth+29;
}
else
{
days=28;
sumMonth = sumMonth+28;
}
break;
}
}

}
public static void printCalender() {
System.out.println(" " + inputYear + "年" + inputMonth + "\n");
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
firstDay = (sumYear + sumMonth-days + 1) % 7;
for (int i = 0; i < firstDay; i++) {
System.out.print("\t");
}
for (int i = 1; i <= days; i++) {
System.out.print(i + "\t");
if ((sumYear + sumMonth-days + i) % 7 == 6) {
System.out.println("\n");
}
}
}
}