-3
long epoch = System.currentTimeMillis() / 1000;
String dateStr = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.0Z'").format(new java.util.Date(epoch * 1000));
System.out.println(dateStr);

Can anyone please tell me how to get the time stamp of 4 weeks from the current one? I'm working on payroll testing.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
Sunset Bloom
  • 111
  • 1
  • 2
  • 8

3 Answers3

5

Subtract the number of milliseconds in 4 weeks from the current time.

long now = System.currentTimeMillis();
long fourWeeksAgo = now - 1000 * 60 * 60 * 24 * 28;
brianestey
  • 8,202
  • 5
  • 33
  • 48
3

java.util.Calendar provides a means of manipulating dates so that code is readable:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.WEEK_OF_YEAR, -4);
long fourWeeksAgo = calendar.getTime().getTime();
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1
java.util.Calendar c = java.util.Calendar.getInstance();
c.add(java.util.Calendar.WEEK_OF_MONTH, -4);
Slihp
  • 763
  • 5
  • 12