c0dehunter
c0dehunter

Reputation: 6160

Get unix timestamp exactly 3 years from now

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

E.g.

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?

Upvotes: 0

Views: 1116

Answers (4)

Anonymous
Anonymous

Reputation: 86323

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

Upvotes: 3

sushildlh
sushildlh

Reputation: 9056

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.

Upvotes: 6

Ezzy
Ezzy

Reputation: 1483

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);

Upvotes: 0

Manish Ahire
Manish Ahire

Reputation: 580

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();

    }

Upvotes: 0

Related Questions