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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 获得本周一到周五的日期相关的知识,希望对你有一定的参考价值。

比如说现在 怎么获得 2013-4-8 到2013-4-12

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;


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


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

参考技术A 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];

参考技术B

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

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

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

就这么简单

参考技术C 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));
参考技术D 这个是万年历,可以参考一下
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");



java quartz中CronExpression表达式,场景:每隔2周的周一到周五执行的写法。急。。。。。。。。。。。。

参考技术A :40 执行某个JOB
trigger.setEndTime(endTime) 根据日期的不同动态生成endTime 就可以做到了
单纯用一个表达式很难办到
参考技术B 每隔2周的周一到周五 早上9点执行

0 0 9 ? * 1-5/14

以上是关于java 获得本周一到周五的日期的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell 从本周五开始获得 12 个月的周五

java - 如何从Java日历中获取指定周的星期六日期? [关闭]

java quartz中CronExpression表达式,场景:每隔2周的周一到周五执行的写法。急。。。。。。。。。。。。

c#winform中怎么获得上周一的日期

java获取本周 上周的所有日期

sqlserver 写一个函数,能获取两个日期之间的周一到周五的总天数