Dan
Dan

Reputation: 45

How Do I Display the X amount of days since January 1, 1970 in Java

I was wondering what is the best way to solve this problem on java ranch.

http://www.javaranch.com/geekWatch.jsp

Thanks

Upvotes: 1

Views: 920

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347314

Use either JodaTime or Java 8's Time API (or some other dedicated library)

LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
LocalDateTime then = LocalDateTime.of(1970, Month.JANUARY, 1, 0, 0);

Duration duration = Duration.between(then, now);
System.out.println(duration.toDays());

Which outputs

16748

Upvotes: 1

Doc
Doc

Reputation: 11651

The general idea can be

  1. Get the number of seconds(For this understand the Date API in java.) since Jan 1, 1970.
  2. Get the value of seconds in a day 24hrs*60min*60secs.

Now divide the value obtained in 1. by that of 2. This will give you the number of days.

Upvotes: 0

Related Questions