java如何得到上周一和上周日的日期

java如何得到上周一和上周日的日期,第1张

/

获取上周五时间

/

public Date lastFirday() {

//作用防止周日得到本周日期

Calendar calendar = CalendargetInstance();

while (calendarget(CalendarDAY_OF_WEEK) != CalendarMONDAY) {

calendaradd(CalendarDAY_OF_WEEK, -1);

}

int dayOfWeek = calendarget(CalendarDAY_OF_WEEK) - 1;

int offset = 7 - dayOfWeek;

calendaradd(CalendarDATE, offset - 9);

return DateTimeUtilgetFirstDayOfWeek(calendargetTime(), 6);//这是从上周日开始数的到本周五为6

}

/

获取上周一时间

/

public Date lastMonday() {

Calendar calendar = CalendargetInstance();

while (calendarget(CalendarDAY_OF_WEEK) != CalendarMONDAY) {

calendaradd(CalendarDAY_OF_WEEK, -1);

}

int dayOfWeek = calendarget(CalendarDAY_OF_WEEK) - 1;

int offset = 1 - dayOfWeek;

calendaradd(CalendarDATE, offset - 7);

return DateTimeUtilgetFirstDayOfWeek(calendargetTime(), 2);

}

public static void main(String[] agrs) {

String newtime="2013-8-4";

Systemoutprintln(newtime+""+testDate(newtime));

}

public static String testDate(String newtime) {

String dayNames[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

int week1[] = {7,1,2,3,4,5,6};

Calendar c = CalendargetInstance();// 获得一个日历的实例

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

try {

csetTime(sdfparse(newtime));

} catch (ParseException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

String a=dayNames[cget(CalendarDAY_OF_WEEK)-1];

int b=week1[cget(CalendarDAY_OF_WEEK)-1];

if(b!=7){

cset(CalendarDAY_OF_WEEK, CalendarMONDAY);

}else{

cadd(CalendarWEEK_OF_MONTH, -1);

cset(CalendarDAY_OF_WEEK, 2);

}

for(int i=1;i<b;i++){

if(i==1){

Systemoutprintln(sdfformat(cgetTime()));//本周一

}

cadd(CalendarDATE,1);

Systemoutprintln(sdfformat(cgetTime()));

}

Systemoutprintln("-------------");

return a;

}

测试结果:

2013-07-29

2013-07-30

2013-07-31

2013-08-01

2013-08-02

2013-08-03

2013-08-04

-------------

2013-8-4星期日

提倡了一下中文写,

static public void main(String 参数[]){

SimpleDateFormat 格式=new SimpleDateFormat("y年M月d日 E H时m分s秒",LocaleCHINA);

Calendar 日历=CalendargetInstance(LocaleCHINA);

//当前时间,貌似多余,其实是为了所有可能的系统一致

日历setTimeInMillis(SystemcurrentTimeMillis());

Systemoutprintln("当前时间:"+格式format(日历getTime()));

日历set(CalendarDAY_OF_WEEK, CalendarMONDAY);

Systemoutprintln("周一时间:"+格式format(日历getTime()));

}

===========

改到后天测了一下

输出

当前时间:2011年8月31日 星期三 12时32分40秒

周一时间:2011年8月29日 星期一 12时32分40秒

思路:首先是获取该月的实际天数,然后从1号遍历直至该月最后一天,利用Calendar类找出该日是一周中的第几天,

(1)若该日是周日,则视为一周结束,并用该日期减去6找到该周的开始日期,若相减结果小于等于1,则说明该周起始日期应该是本月1号。

(1)若该日不是周日,且是该月最后一天,则应该在上面计算的基础上再加一周,视月末为该周的结束日,该周起始日期应为改日期减去改日

在一周中的位置加2,例如:2013-09-30号是周一,则视为9月份中的第六周,开始日期是30-2+2

提示:Calendar 类中视周日为一周的开始,值是1

示例代码:

[java] view plain copy

public static void printfWeeks(String date) throws Exception {

// String date = "2013-09";

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");

Date date1 = dateFormatparse(date);

Calendar calendar = new GregorianCalendar();

calendarsetTime(date1);

int days = calendargetActualMaximum(CalendarDAY_OF_MONTH);

Systemoutprintln("days:" + days);

int count = 0;

for (int i = 1; i <= days; i++) {

DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

Date date2 = dateFormat1parse(date + "-" + i);

calendarclear();

calendarsetTime(date2);

int k = new Integer(calendarget(CalendarDAY_OF_WEEK));

if (k == 1) {// 若当天是周日

count++;

Systemoutprintln("-----------------------------------");

Systemoutprintln("第" + count + "周");

if (i - 6 <= 1) {

Systemoutprintln("本周开始日期:" + date + "-" + 1);

} else {

Systemoutprintln("本周开始日期:" + date + "-" + (i - 6));

}

Systemoutprintln("本周结束日期:" + date + "-" + i);

Systemoutprintln("-----------------------------------");

}

if (k != 1 && i == days) {// 若是本月最好一天,且不是周日

count++;

Systemoutprintln("-----------------------------------");

Systemoutprintln("第" + count + "周");

Systemoutprintln("本周开始日期:" + date + "-" + (i - k + 2));

Systemoutprintln("本周结束日期:" + date + "-" + i);

Systemoutprintln("-----------------------------------");

}

}

}

测试代码:

[java] view plain copy

public static void main(String[] args) throws Exception {

String s = "2013-";

for (int i = 1; i <= 12; i++) {

Systemoutprintln("");

Systemoutprintln("月份:" + i);

printfWeeks(s + i);

Systemoutprintln("");

}

// printfWeeks(s + 2);

}

第几周是该月的第几周吗 还是该年的第几周

下面的程序包含了2种情况, 请自行选择

package comcalender;

import javautilCalendar;

import javautilLocale;

/

输出某周的日期

演示Calendar的用法

Calendar的使用一般分2步,

1确定某天, 调用set函数

2可以取出该天的任意信息, 通过get函数

@author: zhuby

@create date:2011-1-17

/

public class WeekCalendar {

/

周计数,为全年中的周计数

@param year

@param weekCount

@return

/

public int[] getWeekDateByWeekInYear(int year,int weekCount){

Calendar cal=CalendargetInstance(LocaleCHINA);

//设置calendar的日期,此处可以确定某一天

calset(CalendarYEAR, year);

calset(CalendarWEEK_OF_YEAR, weekCount);

//获取该天的信息(理论而言,通过改变get函数的参数,可以获取该天的任意信息)

int day=calget(CalendarDAY_OF_YEAR);

int[] days=new int[7];

for(int i=0;i<7;i++){

days[i]=calget(CalendarDAY_OF_MONTH);

// Systemoutprint((calget(CalendarMONTH)+1)+"月"+calget(CalendarDAY_OF_MONTH)+"日 ");

day++;

calset(CalendarDAY_OF_YEAR, day);

}

return days;

}

/

周计数为月中的周计数

@param year

@param month

@param weekCount

@return

/

public int[] getWeekDateByWeekInMonth(int year,int month,int weekCount){

Calendar cal=CalendargetInstance(LocaleCHINA);

calset(CalendarYEAR, year);

calset(CalendarMONTH, month-1);

calset(CalendarWEEK_OF_MONTH, weekCount);

int day=calget(CalendarDAY_OF_YEAR);

int[] days=new int[7];

for(int i=0;i<7;i++){

days[i]=calget(CalendarDAY_OF_MONTH);

// Systemoutprint((calget(CalendarMONTH)+1)+"月"+calget(CalendarDAY_OF_MONTH)+"日 ");

day++;

calset(CalendarDAY_OF_YEAR, day);

}

return days;

}

public static void main(String[] args) {

WeekCalendar wc=new WeekCalendar();

for(int i=0;i<7;i++){

Systemoutprint(wcgetWeekDateByWeekInYear(2011, 7)[i]+" ");

}

Systemoutprintln();

for(int i=0;i<7;i++){

Systemoutprint(wcgetWeekDateByWeekInMonth(2011,4, 2)[i]+" ");

}

}

}

import javatextDateFormatSymbols;

import javautilCalendar;

import javautilGregorianCalendar;

public class Test {

/

@param args

/

public static void main(String[] args){

Calendar c_begin = new GregorianCalendar();

Calendar c_end = new GregorianCalendar();

DateFormatSymbols dfs = new DateFormatSymbols();

String[] weeks = dfsgetWeekdays();

c_beginset(2010, 3, 2); //Calendar的月从0-11,所以4月是3

c_endset(2010, 4, 20); //Calendar的月从0-11,所以5月是4

int count = 1;

c_endadd(CalendarDAY_OF_YEAR, 1); //结束日期下滚一天是为了包含最后一天

while(c_beginbefore(c_end)){

Systemoutprintln("第"+count+"周 日期:"+new javasqlDate(c_begingetTime()getTime())+", "+weeks[c_beginget(CalendarDAY_OF_WEEK)]);

if(c_beginget(CalendarDAY_OF_WEEK)==CalendarSUNDAY){

count++;

}

c_beginadd(CalendarDAY_OF_YEAR, 1);

}

}

}

public Calendar getWeekFirst(int year, int week){

Calendar calendar = CalendargetInstance();

calendarset(year, 0, 1);

int weeks = 0;

while((weeks = calendarget(CalendarWEEK_OF_YEAR))<= week){

calendaradd(CalendarMONTH, 1);

//Systemoutprintln(calendarget(CalendarWEEK_OF_YEAR));

}

calendaradd(CalendarMONTH, -1);

//Systemoutprintln(calendarget(CalendarWEEK_OF_YEAR));

while((weeks = calendarget(CalendarWEEK_OF_YEAR))< week){

calendaradd(CalendarDATE, 1);

}

return calendar;

}

参数,几几年,第几周

返回当前周数第一天的日期多对应的Calendar对象。你可以根据我的方法进行适当修改以满足你的要求

import javatextSimpleDateFormat;

import javautilCalendar;

public class Demo {

public static void main(String[] args) {

// 日期格式转换

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

// 当前日期

Calendar instance = CalendargetInstance();

// 调整到上周

instanceadd(CalendarWEDNESDAY, -1);

// 调整到上周1

instanceset(CalendarDAY_OF_WEEK, 2);

//循环打印

for (int i = 1; i <= 5; i++) {

Systemoutprintln("星期" + i + ":" + formatformat(instancegetTime()));

instanceadd(CalendarDAY_OF_WEEK, 1);

}

}

}

以上就是关于java如何得到上周一和上周日的日期全部的内容,包括:java如何得到上周一和上周日的日期、java 日期,根据今天的日期,输出这一周的其他日期,例如:2013-7-31,输出这周从第一天开始到今天的日期、java 获取当前日期时间和本周的星期一的日期时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9536433.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存