java 一周的第一天_java获取上个星期第一天和最后一天

package com.goldcn.jzgmanageplat.b2b.controller;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import com.goldcn.common.utils.CommonUtils;

public class aa {

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

aa tt = new aa();

System.out.println("获取上周一日期:" + tt.getYesterday());

System.out.println("获取上周日日期:" + tt.getPreviousWeekSunday());

}

public static int getMondayPlus() {

Calendar cd = Calendar.getInstance();

int dayOfWeek = cd.get(7) - 1;

if (dayOfWeek == 1) {

return 0;

}

return (1 - dayOfWeek);

}

public String getPreviousWeekSunday() {

int weeks =- 1;

int mondayPlus = getMondayPlus();

GregorianCalendar currentDate = new GregorianCalendar();

currentDate.add(5, mondayPlus + weeks);

Date monday = currentDate.getTime();

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

String preMonday = sdf.format(monday);

//System.out.println("上周最后一天:"+CommonUtils.parseDateToLong(preMonday+" 23:59:59", "yyyy-MM-dd hh:mm:ss"));

return preMonday;

}

public String getPreviousWeekday() throws Exception {

int week=-1;

int mondayPlus = getMondayPlus();

GregorianCalendar currentDate = new GregorianCalendar();

currentDate.add(5, mondayPlus + 7 * week);

Date monday = currentDate.getTime();

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

String preMonday = sdf.format(monday);

//System.out.println("上周第一天:"+CommonUtils.parseDateToLong(preMonday+" 00:00:00", "yyyy-MM-dd hh:mm:ss"));

return preMonday;

}

public String getYesterday(){

Calendar cal=Calendar.getInstance();

cal.add(Calendar.DATE,-1);

Date time=cal.getTime();

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

String yesterday = sdf.format(time);

return yesterday;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值