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;
    }
}
6
  • 1
    Does this answer your question? How do I add one month to current date in Java? (Check the Java 8 answer) Commented Jul 6, 2021 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. Commented Jul 6, 2021 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; } } Commented Jul 7, 2021 at 6:32
  • Is EndDateTimestamp your own class? Why not just use a class from java.time for the end timestamp? Commented Jul 7, 2021 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). Commented Jul 7, 2021 at 15:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.