欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取本周开始时间和下一周结束时间

程序员文章站 2022-04-25 19:34:17
...

package cn.wisesign;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Demo2 {
public static void main(String[] args) {
/* String str = “123”;
int indexOf = str.indexOf(“123”);
System.out.println(indexOf);
Map<String,String>map = new HashMap<String,String>();
map.put(“1”,“2”);
System.out.println(map);
map.replace(“1”,"");
System.out.println(map);
short s1=1;
s1+=1;*/
String beginDayOfWeek = getBeginDayOfWeek();
System.out.println(“本周开始时间”+beginDayOfWeek);
String beginDayOfWeek2 = getEndDayOfWeek();
System.out.println(“下周结束时间”+beginDayOfWeek2);
}
public static String getBeginDayOfWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
SimpleDateFormat sd = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
return sd.format(cal.getTime());
}

	// 获取下周结束时间
	public static String getEndDayOfWeek() {
	   Calendar cal = Calendar.getInstance();
	   SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   try {
	   cal.setTime(sd.parse(getBeginDayOfWeek()));
	   cal.set(Calendar.HOUR_OF_DAY, 23);
	   cal.set(Calendar.MINUTE, 59);
	   cal.set(Calendar.SECOND, 59);
	   cal.add(Calendar.DAY_OF_WEEK, 13);
	} catch (ParseException e) {
		e.printStackTrace();
	}
	   Date weekEndSta = cal.getTime();
	   return sd.format(weekEndSta);
	}

}

相关标签: data