how to get last 7days starting time stamp and ending time stamp in java

Hi Experts,

I want to get  last one week timestamp
current date :2015/08/05
now i want to get the time stamp for last 7days
2015/07/29--->starttime 1438128010  endtime  1438214399
2015/07/30--->starttime 1438214400  endtime  1438300799
2015/08/05--->starttime 1438732800  endtime  1438819199

i dont want date i need only timestamp starttime and endtime
for last one week whenver i call this method.

can some suggest me how to do
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


		long start = new SimpleDateFormat("yyyy/MM/dd").parse("2015/07/29").getTime();
		long end = start + TimeUnit.DAYS.toMillis(7) - TimeUnit.SECONDS.toMillis(1) ;
		System.out.println("Start " + start/1000 + " End " + end/1000) ;

Open in new window

(I took off 1 second from 7 days as you appear to want 11:59:59 at the end).

Also dividing the final results by 1000 as these timestamps are in milliseconds and you appear to want the seconds version.

srikoteshAuthor Commented:
Hi Doug,

i need 1 week timestamps
as i mentioned if i give date as 2015/08/6
my code has to get the last one week timestamps
start time to endtime
from 2015/07/29 to 2015/08/06 .

date should be dynamic.
we can give any date it has to fetch last 7days time stamps
Oh sorry - I thought you just wanted to map one value at a time.

Try this for all 7 days.  I also shifted to GMT as that seems to be what you're also wanting.


		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd") ;
		formatter.setTimeZone(TimeZone.getTimeZone("GMT")) ;
		long oneWeekAgo = formatter.parse("2015/08/05").getTime() - TimeUnit.DAYS.toMillis(7) ;

		for (int days = 1 ; days <= 7 ; days++) {
			long start = oneWeekAgo + TimeUnit.DAYS.toMillis(days) ;
			long end = start + TimeUnit.DAYS.toMillis(1) - TimeUnit.SECONDS.toMillis(1);
			System.out.println("Start " + start / 1000 + " End " + end / 1000);

Open in new window

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Java 8 version:

        Instant now =;
        for(int i = 6;i>=0;i--) {
            Instant then = now.minus(Period.ofDays(i));

Open in new window

Ooh Java 8 is cool :)

srikoteshAuthor Commented:
Hi Doug,

I am getting the expected output.
i remove gmt timezone.

thanks for giving this code java8.
i got this output:

when i converted this to date i got like this
1438905600000--->8/7/2015, 5:30:00 AM
is it possible to change -->8/7/2015, 12:00:00 AM
1438992000000--->8/7/2015, 11:59:59 PM
srikoteshAuthor Commented:
Hi Experts,

apart from the time stamp i need name of a day.
Monday-->starttime--->1438387200000 endtime->1438473600000
Tuesday-->starttime--->1438473600000 endtime->1438560000000

can some one suggest me how to do this one as well.
Try this:

		SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE") ;

		for (int days = 1 ; days <= 7 ; days++) {
			long start = oneWeekAgo + TimeUnit.DAYS.toMillis(days) ;
			long end = start + TimeUnit.DAYS.toMillis(1) - TimeUnit.SECONDS.toMillis(1);
			String today = dayFormat.format(new Date(start)) ;
			System.out.println(today + " Start " + start / 1000 + " End " + end / 1000);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
getting expected output:
Saturday Start 1438367400 End 1438453799
Sunday    Start 1438453800 End 1438540199
Monday   Start 1438540200 End 1438626599
srikoteshAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.