Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

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

Hi Experts,

I want to get  last one week timestamp
Example:
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
Thanks,
Avatar of dpearson
dpearson

Try:

		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.

Doug
Avatar of srikotesh

ASKER

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
SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooh Java 8 is cool :)

Doug
Hi Doug,

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

HI CEHJ,
thanks for giving this code java8.
i got this output:
1438387200000->1438473600000
1438473600000->1438560000000
1438560000000->1438646400000
1438646400000->1438732800000
1438732800000->1438819200000
1438819200000->1438905600000
1438905600000->1438992000000

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
Hi Experts,

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

can some one suggest me how to do this one as well.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
getting expected output:
Saturday Start 1438367400 End 1438453799
Sunday    Start 1438453800 End 1438540199
Monday   Start 1438540200 End 1438626599
Good.
Excellent