2

I am new in java and I have a requirement where I need to create a method which has to output the timestamp for current datetime + 2 months. Basically in my code I have used the timestamp which is hardcoded and it is getting expired in evry 2 months so I want to replace the hardcode value with the output of a method which will calculate the timestamp of after 2 months from now and I can pass the output to my method instead of hardcoding. Can someone please help me with the utility to meet my requirement .

rules.add(CreateDiscountV8.createDiscountV8Rule(1564185600000l, 1640952000000l, 0, ruleEffectiveTimes, "P", "AC", "E", "AC", 0l, Long.MAX_VALUE, 0l, Long.MAX_VALUE,"bexdl", "x-in-y",null, 100, DEFAULT_MIN_TRIP_SEGMENTS, DEFAULT_MAX_TRIP_SEGMENTS, false, 1));

I am trying something like this but getting error in compiling it.

public class GetDynamicTimestamp {
    public static EndDateTimestamp getEndDate()
    {
        long currentTimestamp = System.currentTimeMillis();
        long enddatetimestamp = currentTimestamp + 200000000l;
        return enddatetimestamp;
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Saurabh
  • 43
  • 1
  • 6
  • 1
    Does this answer your question? [How do I add one month to current date in Java?](https://stackoverflow.com/questions/4905416/how-do-i-add-one-month-to-current-date-in-java) (Check the Java 8 answer) – jhamon Jul 06 '21 at 13:31
  • `ZonedDateTime.now(ZoneId.systemDefault()).plusMonths(2)`. If you necessarily need an old-fashioned `java.sql.Timestamp`, then `Timestamp.from(zdt.toInstant())` where `zdt` is the `ZonedDateTime` from before. – Ole V.V. Jul 06 '21 at 14:31
  • I am trying something like this but getting error in compiling it. ```public class GetDynamicTimestamp { public static EndDateTimestamp getEndDate() { long currentTimestamp = System.currentTimeMillis(); long enddatetimestamp = currentTimestamp + 200000000l; return enddatetimestamp; } }``` – Saurabh Jul 07 '21 at 06:32
  • Is `EndDateTimestamp` your own class? Why not just use a class from java.time for the end timestamp? – Ole V.V. Jul 07 '21 at 15:42
  • Providing more information to your question is a good idea and welcome. It’s always best to do it as edits to the question (this is especially true for code since code is unreadavble in a comment). – Ole V.V. Jul 07 '21 at 15:43
  • Also 200 000 000 milliseconds equals a little more than 2 days. I think that 2 months can be anything between 5 094 000 000 and 5 360 400 000 millis, maybe even less or more. – Ole V.V. Jul 07 '21 at 16:12

1 Answers1

2

Solution using java.time, the modern Date-Time API:

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(getEndDate());
    }

    public static long getEndDate() {
        return OffsetDateTime.now(ZoneOffset.UTC)
                .plusMonths(2)
                .toInstant()
                .toEpochMilli();
    }
}

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

What went wrong with your code

In your code, the return type of the function, getEndDate is EndDateTimestamp whereas you are returning a long value. Also, it's not a good idea to perform the calculations manually if there are specialized API to achieve the same.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110