Computer Science, asked by bharti9344, 8 months ago

Write a Java expression to calculate
the number of minutes in a day.

Answers

Answered by jkanhaiya523
0

Answer:

To get the number of minutes into the day, get the first moment of the day, and then calculated elapsed time. ZonedDateTime startOfDay = now. toLocalDate(). atStartOfDay( z );

Explanation:

Time Zone

The other answers are incorrect in that they fail to account for time zone. If you want minutes since start of day, which day? The day starting in Kolkata, Paris, or Montréal? A 23-hour, 24-hour, 25-hour, or some other length day?

Specify a proper time zone name. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

Using java.time

Get the current moment, ZonedDateTime, for your desired/expected time zone by specifying a ZoneId.

ZoneId zoneId = ZoneId.of( "America/Montreal" );

ZonedDateTime now = ZonedDateTime.now( zoneId );

To get the number of minutes into the day, get the first moment of the day, and then calculated elapsed time.

ZonedDateTime startOfDay = now.toLocalDate().atStartOfDay( z );

Calculate elapsed time either as a Duration or use the ChronoUnit enum.

Duration duration = Duration.between( startOfDay , now );

long minutesIntoTheDay = duration.toMinutes();

…or…

long minutesIntoTheDay = ChronoUnit.MINUTES.between( startOfDay , now );

Example: Europe/Amsterdam

Here is an example showing the DST cutover (“Spring forward”) for the Netherlands in time zone Europe/Amsterdam this year of 2017, on March 26 at 2 AM.

LocalDate march26 = LocalDate.of ( 2017, Month.MARCH, 26 );

LocalTime twoAm = LocalTime.of ( 2, 0 );

ZoneId z = ZoneId.of ( "Europe/Amsterdam" );

ZonedDateTime start = march26.atStartOfDay ( z );

ZonedDateTime stop = ZonedDateTime.of ( march26, twoAm, z );

long minutes = ChronoUnit.MINUTES.between ( start, stop );

Duration duration = Duration.between ( start, stop );

long durationAsMinutes = duration.toMinutes ( );

int minuteOfDay = stop.get ( ChronoField.MINUTE_OF_DAY );

Dump to console.

System.out.println ( "start: " + start );

System.out.println ( "stop: " + stop );

System.out.println ( "minutes: " + minutes );

System.out.println ( "FYI: 4 * 60 = " + ( 4 * 60 ) + " | 3 * 60 = " + ( 3 * 60 ) + " | 2 * 60 = " + ( 2 * 60 ) );

System.out.println ( "duration.toString(): " + duration + " | durationAsMinutes: " + durationAsMinutes );

System.out.println ( "minuteOfDay: )

TAG ME A BRILLIANT

Similar questions