Java中获取本周内所有日期
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中获取本周内所有日期相关的知识,希望对你有一定的参考价值。
在Java中获取本周内所有日期,并按中国人传统,周一为第一天开始计算
import java.text.SimpleDateFormat;import java.util.Calendar;
public class DataTest
public static void main(String[] args)
printWeekdays();
private static final int FIRST_DAY = Calendar.MONDAY;
private static void printWeekdays()
Calendar calendar = Calendar.getInstance();
setToFirstDay(calendar);
for (int i = 0; i < 7; i++)
printDay(calendar);
calendar.add(Calendar.DATE, 1);
private static void setToFirstDay(Calendar calendar)
while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY)
calendar.add(Calendar.DATE, -1);
private static void printDay(Calendar calendar)
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd EE");
System.out.println(dateFormat.format(calendar.getTime()));
参考技术A SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
// 今天是一周中的第几天
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK );
if (c.getFirstDayOfWeek() == Calendar.SUNDAY)
c.add(Calendar.DAY_OF_MONTH, 1);
// 计算一周开始的日期
c.add(Calendar.DAY_OF_MONTH, -dayOfWeek);
for (int i=1;i<=7;i++)
c.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(sdf.format(c.getTime()));
参考技术B public static void main(String[] args)
Calendar cal = Calendar.getInstance();
int date = cal.get(Calendar.DAY_OF_MONTH);
int n = cal.get(Calendar.DAY_OF_WEEK);
if (n == 1)
n = 7;
else
n = n - 1;
System.out.println("当天为本周第" + n + "天");
// 日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 1; i <= 7; i++)
cal.set(Calendar.DAY_OF_MONTH, date + i - n);
System.out.println("本周第" + i + "天:" + sdf.format(cal.getTime()));
结果:
当天为本周第3天
本周第1天:2014-02-17
本周第2天:2014-02-18
本周第3天:2014-02-19
本周第4天:2014-02-20
本周第5天:2014-02-21
本周第6天:2014-02-22
本周第7天:2014-02-23
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;
如果每周指的是从星期一开始,那么这个程序可以达到你的要求。
如果每周指得是从星期日开始,那么请说明,我再修改。
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);
就这么简单
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中获取本周内所有日期的主要内容,如果未能解决你的问题,请参考以下文章
javascript获取当天日期,计算出该天所属周,列出本周从周一至周日的所有日期,求大神帮助!