Getting "Unixtime" in Java

Getting unixtime in Java

Avoid the Date object creation w/ System.currentTimeMillis(). A divide by 1000 gets you to Unix epoch.

As mentioned in a comment, you typically want a primitive long (lower-case-l long) not a boxed object long (capital-L Long) for the unixTime variable's type.

long unixTime = System.currentTimeMillis() / 1000L;

Getting unix timestamp from Date()

getTime() retrieves the milliseconds since Jan 1, 1970 GMT passed to the constructor. It should not be too hard to get the Unix time (same, but in seconds) from that.

Fetching UNIX epoch time in java

There is no difference. This is just the result of the evolution of the date API over the years. There is now more than one way to do this.

As far as just getting epoch milliseconds, all three are fine. Things get more complicated as soon as formatting, calendars, timezones, durations and the like become involved.

How can I get Unix Timestamp automatically for every next day?

java.time

Like Samuel Marchant I recommend that you use java.time, the modern Java date and time API, for your date and time work.

If you want every day at 9, define that as a constant first:

private static final LocalTime TIME = LocalTime.of(9, 0);

Now your milliseconds values can be obtained in this way:

    ZonedDateTime zdt = ZonedDateTime.now(ZoneId.systemDefault()).with(TIME);
for (int i = 0; i < 10; i++) {
long timestampMillis = zdt.toInstant().toEpochMilli();
System.out.format("%-30s %13d%n", zdt, timestampMillis);

zdt = zdt.plusDays(1);
}

Output when I ran the code just now:

2021-11-11T09:00+01:00[Europe/Paris] 1636617600000
2021-11-12T09:00+01:00[Europe/Paris] 1636704000000
2021-11-13T09:00+01:00[Europe/Paris] 1636790400000
2021-11-14T09:00+01:00[Europe/Paris] 1636876800000
2021-11-15T09:00+01:00[Europe/Paris] 1636963200000
2021-11-16T09:00+01:00[Europe/Paris] 1637049600000
2021-11-17T09:00+01:00[Europe/Paris] 1637136000000
2021-11-18T09:00+01:00[Europe/Paris] 1637222400000
2021-11-19T09:00+01:00[Europe/Paris] 1637308800000
2021-11-20T09:00+01:00[Europe/Paris] 1637395200000

Please enjoy not only how much simpler but first and foremost how much more natural to read the code is compared to the code in your own answer. This is typical for java.time compared to the old and poorly designed classes from Java 1.0 and 1.1.

Link

Oracle Tutorial: Date Time explaining how to use java.time.

Java get current day from unix timestamp

You can use SimpleDateFormat to format your date:

long unixSeconds = 1372339860;
Date date = new Date(unixSeconds*1000L); // *1000 is to convert seconds to milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4")); // give a timezone reference for formating (see comment at the bottom
String formattedDate = sdf.format(date);
System.out.println(formattedDate);

You can also convert it to milliseconds by multiplying the timestamp by 1000:

java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);

After doing it, you can get what you want:

Calendar cal = Calendar.getInstance();
cal.setTime(dateTime);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH); //here is what you need
int day = cal.get(Calendar.DAY_OF_MONTH);

Get unix timestamp exactly 3 years from now

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.

Getting Unix timestamp from the past with java 8

"Two weeks ago" is dependent on your time zone (there may have been some DST changes etc.). So using Instant or LocalDateTime may create issues because they don't include any time zone information.

Assuming you want to do it in UTC, you can use:

ZonedDateTime twoWeeksAgo = ZonedDateTime.now(ZoneOffset.UTC).minusWeeks(2);
long unixTs = twoWeeksAgo.toEpochSecond();

You can specify a different time zone in place of ZoneOffset.UTC seen above. For example, ZoneId.of( "Asia/Kolkata" ).

Java: Date from unix timestamp

For 1280512800, multiply by 1000, since java is expecting milliseconds:

java.util.Date time=new java.util.Date((long)timeStamp*1000);

If you already had milliseconds, then just new java.util.Date((long)timeStamp);

From the documentation:

Allocates a Date object and
initializes it to represent the
specified number of milliseconds since
the standard base time known as "the
epoch", namely January 1, 1970,
00:00:00 GMT.

Why Java Unix time and Calendar calculate exact time?

1614766198 was a Unix time at 2021-03-03 10:10:00 (UTC+0)

This is not correct. The following UNIX command

TZ=UTC date -r 1614766198

outputs

Wed  3 Mar 2021 10:09:58 UTC

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

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

import java.time.Instant;

public class Main {
public static void main(String[] args) {
Instant instant = Instant.ofEpochSecond(1614766198);
System.out.println(instant);
}
}

Output:

2021-03-03T10:09:58Z

ONLINE DEMO

An Instant represents an instantaneous point on the timeline in UTC. The Z in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

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


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.



Related Topics



Leave a reply



Submit