-1

I am getting the current time in epoch. How can I add 1 month in future?

Date date = new Date();
int epoch = date.getTime();

Datatype for epoch - integer($int64)

To be precise: I want to add 30 days from current time.

I am using a tool that allows Groovy and Java code to be embedded. I used Date class because I can easily import java.text.DateFormat; and import java.text.SimpleDateFormat;. The tool that I have doesn't support Instant.

8
  • 2
    There's no such thing, believe it or not, as a "1 month" as a duration. The reason for that is that months can be different lengths depending on the month and and year. So what do you want to add? 30 days? The same day and time as today but next month? You have to define precisely how far you want to jump forward from the current time. Commented Nov 1, 2020 at 0:00
  • 1
    Nit: epoch's just an ordinary English word, it doesn't need capitalization. Commented Nov 1, 2020 at 0:12
  • 1
    30 days and a month is (usually) not the same. Which of them do you want? An int is Java is always 32 bits. A 64 bits integer is called a long. Which of them do you want? Commented Nov 1, 2020 at 7:02
  • 1
    I recommend you don’t use Date. That class is poorly designed and long outdated. Instead use ZonedDateTime and Instant, both from java.time, the modern Java date and time API. Commented Nov 1, 2020 at 7:03
  • 1
    Which tool have you got that doesn’t support classes that have been with standard Java for 6 years 7 months? Can you upgrade it to an approximately current version? Commented Nov 1, 2020 at 7:39

4 Answers 4

8

Since Java 8, use java.time for time usage

As epoch seconds, adding 30 days:

Instant.now().plus(30, ChronoUnit.DAYS).getEpochSecond()

As epoch milliseconds, adding 30 days:

Instant.now().plus(30, ChronoUnit.DAYS).toEpochMilli()
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, the tool that I have doesn't support your answer but I have upvoted it.
Thanks a lot. Here is a variant with Date otherwise: long epochSecondsPlus30Days = (new Date()).getTime() / 1000L + (30 * 24 * 3600);
How’s your answer different than mine that was posted 15 min before yours?
I did not see how it was 15 min ago, but when I saw it first time it was written as Instant.now().plus(30, TimeUnit.DAYS). Otherwise when I look at it now only the conversion to seconds or millis.
Other variant of Date and epoch millis: long epochMillisPlus30Days = (new Date()).getTime() + (30 * 24 * 3600 * 1000L)
|
1

You don’t want to use Date, use date time API.

Instant.now().plus(30, ChronoUnit.DAYS)

5 Comments

which java library do I need to import? I am using a tool that allows groovy and java code to be embedded. I used Date function because I can easily import java.text.DateFormat; import java.text.SimpleDateFormat;
There’s no library you need to import, it’s all part of the JDK 8 onwards. Just use your IDE to do the imports, I don’t have to list them for you.
I agree in using java.time, the modern Java date and time API. Adding days to an Instant assumes that a day is always 24 hours, which it isn’t, so your code gives inaccurate results around summer time transitions and other time anomalies.
@OleV.V. On the contrary, the whole point of the date time API is not having to worry about which day falls 13 milliseconds short of 24 hours. In other words, we don't care, unless there is a reason to.
Well, only the OP can tell what they intended exactly when they said first “1 month” and then “30 days”. Thanks for clarifying what you intended on your part.
-2

Here's how I'd do it:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 30);
long epoch = calendar.getTimeInMillis();
System.out.println(epoch);

Result:

1606785580218

This is milliseconds. You can divide by 1000 to get the version in seconds. NOTE: The seconds version will fit in an int whereas the milliseconds version requires a long. So it's OK to do this if you want the seconds version in an int:

long epoch = ...
int epochSeconds = (int)(epoch / 1000);

BTW, Unix time (Epoch time, POSIX time) is defined as the number of seconds since 1 January 1970 UTC. Some systems will return the value in milliseconds to provide more accuracy, but such a value is officially a fractional Epoch time multiplied by 1000.

12 Comments

Can i use the datatype for epoch as int?
YES! See my constantly evolving answer!
In your comment under a different answer you said the crucial: my code chooses a representation of the current time in some known timezone (we don't care what it is), …. You need to care, or you cannot know whether you get the correct result.
Also I would prefer to be able to read more clearly from your code which known time zone you had chosen.
That's perfectly fair.
|
-3

So far all answers are wrong.

That's because what you want is impossible.

You're mixing entirely incompatible concepts. You're asking to add 1 entity that is devoid of timezone and political meaning ('epoch-millis', which explicitly means: No timezone info!) with a concept that cannot be nailed down as meaning anything particular unless you supply timezone and era.

You cannot add a month to an epoch milli. Not because java doesn't let you, or because it is hard to program. You can't do it for the same reason you cannot point at the corner in a circle. It's literally impossible, by definition.

'epoch millis' are a concept that is fundamentally about a moment in time. Things like 'when the sun flared up'. "When I clapped my hands together just now". This concept is best represented in java by an instance of java.time.Instant. It is also represented by a java.util.Date, which is funny, because this is nothing like a Date, and indeed, j.u.Date is an utterly stupid name, and the authors have belatedly realized this, which is why (almost) all of the methods it has are marked @Deprecated with a note describing that Date's very name is a total lie. j.u.Date does not represent dates.

A month, that's an entirely different can of worms. There's nothing solid about a month. It could be 28 days. 30 days. 31 days. 30 days and an extra second tossed in. It could be a month that doesn't even exist, or is only 11 days (when political areas switch timezones, you can get some really bizarre things happening).

So, how do you add 'a month' to any instant in time?

You can't. It's not possible. You'd have no idea what to add, because there is no way to figure out if it's 28 days, 29, 30, 31, let alone leap seconds and era weirdness (recently, samoa switched to the other side of the dateline, and as a consequence, their december was only 30 days. The famous 'october revolution' that introduced communism to russia happened in november, at least as far as the entire world (except the russians) were concerned, because only the russians were on the julian calendar where it was still october. As part of the whole 'communism will take over the world!' scheme one of the very first things they did was get on the gregorian same as the rest of the world, and as a consequence, this date does not exist, AT ALL, in russian history: 1 through 13 feb 1918. They just. . were skipped. One day you wake up in moscow, walk outside, ask somebody: Hey, what date is it (though probably in russian), and they say: Why, 31st of January, comrade. Next day you repeat that exercise and now it is February 19th. So, february 1918 in russia was 15 days long.

See why 'please add 1 month' is just not a thing that could possibly be done unless you tell me when and where? If you tell me 'in russia', then I still don't know if I add 15, or 31, or 30, or 29, or 28. If you tell me 'Februari, in 1918', well, in the rest of the world, februari was 28 days. Only in russia it was 15.

Now, adding 'a month' to some human-oriented date/time construct, such as 'well, right now, in amsterdam', ah, that works: "THIS month, the month it is right now, in amsterdam", that is a question that has an answer. But, 'epoch millis' is java-ese for: "An instant in time, devoid of any and all geographical information", and by missing geo info it is also impossible to know when that is relative to any timezone, and therefore, utterly impossible.

So, what CAN you do? Well, many things, but first you need to figure out what you want to do, and only then can somebody tell you how to do it:

  • I just want to add what feels like an average month, and end up with epoch-millis. Okay, then just add 2.629.800.000, which is a rough estimate (that's about 30.4375 days, which is roughly the average length of a month. I have no idea what possible purpose this would serve, but it's surely a better plan than adding 30 days, or 31 days, or 28 days.
  • I want to first translate this epoch-millis into a time as humans would say it (in turns of year, month, day, hour, minute, and second), and then just increment the month value by 1, and leave it at that, in e.g. a ZonedDateTime object. Okay, then first figure out which timezone you want, then turn your epoch-millis into a ZonedDateTime, and THEN we've arrived at a point where 'add a month to this please' even makes sense, so now we can do that: .plusMonths(1), voila.
  • Same as previous, but then convert that back to epoch millis. Okay, well, do the same thing, and call .toInstant() at the end, and toEpochMillis() on that.

Most other ideas boil down to: Your question makes no sense and cannot be answered.

NB: The above all use the java.time packages. All other options (java.util.Date and java.util.Calendar are broken and unwieldy; generally attempting to do this or any other date-related job in those APIs will either be impossible, will give wrong answers, or there is a way but it is hard to figure out how and the resulting code will be hard to maintain and hard to follow. Why would you voluntarily punch yourself in the face? Why would you voluntary use crap APIs? Don't do that.

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.