0

Using Android and Java 8, how can I get unix timestamps back in time?

E.g.

  • Exactly 3 years back from now
  • Exactly 2 months back from now
  • Exactly 1 day back from now
  • etc

I know I can get current timestamp using System.currentTimeMillis() / 1000L (you could also use Instant.now().getEpochSecond() from the new java.time, but this requires Android API > 25). Now I need to get offset and substract it. I could use TimeUnit.Days.toSeconds(), but if I want to substract years, it does not have YEAR unit and I don't want to mess with leap years myself.

Is there a simple way to do this?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
c0dehunter
  • 6,412
  • 16
  • 77
  • 139

4 Answers4

6

Try this for getting timestamp using Calender....

For After 2 month.

    Calendar date= Calendar.getInstance();
    date.add(Calendar.MONTH, 2);//instead of 2 use -2 value in your case
    date.getTimeInMillis();

For after one day.

    Calendar date= Calendar.getInstance();
    date.add(Calendar.DAY_OF_MONTH, 1);//instead of 1 use -1 value in your case
    date.getTimeInMillis();

For after 3 years.

    Calendar date= Calendar.getInstance();
    date.add(Calendar.YEAR, 3);//instead of 3 use -3 value in your case
    date.getTimeInMillis();

Note:- Use Negative value for back dates.

Hope it solve your problems.

sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • These terrible classes were outmoded years ago with the adoption of JSR 310, being supplanted by the modern *java.time* classes. Never use `Calendar`, `Date`, etc. See [correct Answer](https://stackoverflow.com/a/53157578/642706) by Ole V.V. – Basil Bourque Nov 05 '18 at 16:27
3

java.time and ThreeTenABP

    long threeYearsAgo = OffsetDateTime.now(ZoneOffset.UTC)
            .minusYears(3)
            .toEpochSecond();
    System.out.println("Three years ago: " 
            + NumberFormat.getIntegerInstance().format(threeYearsAgo));

When I ran this just now on my computer I got this output:

Three years ago: 1,446,737,905

I am not completely sure what you get if today happens to be February 29 in a leap year; probably the same time of day on February 28 three years ago. If you need to be sure, check the documentation or try it out.

For months or days ago use the minusMonths or minusDays method of OffsetDateTime.

the new java.time … requires Android API > 25

Not really: java.time has been backported.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0
long now = System.currentTimeMillis() - 1000;
setMaxDate(now+(1000*60*60*24*3)   // 3 days from now




 public void showDatedialog() {
        DatePickerDialog dpd = new DatePickerDialog(DeliveryProduct.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        c.set(year, month, day);
                        String date = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
                        edDate.setText(date);
                        mYear = c.get(Calendar.YEAR);
                        mMonth = c.get(Calendar.MONTH);
                        mDay = c.get(Calendar.DAY_OF_MONTH);
                    }
                }, mYear, mMonth, mDay);
        dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

        long now = System.currentTimeMillis() - 1000;

        //dpd.getDatePicker().setMaxDate(now+(1000*60*60*24*3));  //for 3 days from now
        Calendar d = Calendar.getInstance();
        d.add(Calendar.MONTH, 1);
        //  dpd.getDatePicker().setMaxDate(d.getTimeInMillis());
        dpd.show();

    }
Manish Ahire
  • 550
  • 4
  • 19
0

Getting unix timestamp one year ago from current time.

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
long unixTimestamp = calendar.getTime().getTime() / 1000;

For month...

calendar.add(Calendar.MONTH, -1);
Ezzy
  • 1,423
  • 2
  • 15
  • 32