Prashanth Bafna
Prashanth Bafna

Reputation: 61

Converting current time to Seconds in Java

I have a value of 64800 which is 18:00 in seconds, so I am in need of converting current time to seconds format which I would need to compare if it is after the 64800 or before.

int minutes=64800;
long hours = TimeUnit.SECONDS.toHours(Long.valueOf(minutes));
long remainMinutes = minutes - TimeUnit.HOURS.toSeconds(hours);
System.out.println(String.format("%02d:%02d", hours, remainMinutes));

If I use System.currentTimeMillis I am unable to convert it

Upvotes: 1

Views: 1031

Answers (3)

Thomas
Thomas

Reputation: 88707

Summarizing my comments as well as Ole V.V's:

System.currentTimeMillis() will return the milliseconds since Jan 1st 1970 00:00,000 GMT so you can't use it directly if all you want is the seconds since the start of today.

Also note that due to timezones the current time millis for a certain local time of day may be different so calculating it yourself isn't advisable.

Instead you might use java.time.LocalTime in one of the following ways:

long currentSecondsToday = LocalTime.now().toSecondOfDay();
if( currentSecondsToday < 64800) { ... }

Or maybe more readable (thanks to Ole):

LocalTime target = LocalTime.ofSecondOfDay(64800);
if( LocalTime.now().isBefore(target) ) { ... }

Also note that LocalTime.now() will use your system's default timezone. To be independent from system settings you could use LocalTime.now(ZoneId.of("your desired timezone id")).

Additionally, you might want to use a more readable variant of defining the "target" time, e.g. 18:00, e.g. LocalTime.of(18,0) or LocalTime.parse("18:00", DateTimeFormatter.ofPattern("HH:mm")).

Upvotes: 2

Roberto
Roberto

Reputation: 9080

There is another way to get the current day seconds from 00:00 in case you want to do it using System.currentTimeMillis() and maths:

long epoch = System.currentTimeMillis() / 1000L; // Epoch in seconds
long todaySeconds = epoch % (24*3600); // Current day seconds from 00:00

With todaySeconds you can compare with 64800 or whatever other value, but IMPORTANT, the todaySeconds doesn't include timezone info, It's UTC time, if you need to compare with local time, then add the timezone offset (in seconds) using ZonedDateTime:

long tzOffsetSecs = java.time.ZonedDateTime.now().getOffset().getTotalSeconds();
todaySeconds += tzOffsetSecs;

To get the hour and minute:

long hour = todaySeconds / 3600;
long minute = todaySeconds % 3600 / 60;

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

You can use Duration to get the desired result.

Demo:

import java.time.Duration;

public class Main {
  public static void main(String[] args) {
    Duration duration = Duration.ofSeconds(64800);
    String desiredValue = String.format("%02d:%02d", duration.toHoursPart(), duration.toMinutesPart());
    System.out.println(desiredValue);
  }
}

Output:

18:00

Learn more about the modern Date-Time API from Trail: Date Time and about the Duration through Q/As tagged with duration.

Upvotes: 1

Related Questions