2
votes

Overview

This is the scenario: I'm given a value which represents a portion of the total number of minutes that exist in a standard week (assuming 10,080 minutes a week and 1,440 minutes a day) starting midnight Saturday (so 0 minutes Sunday @ 12am).

I need to convert this minute value into an actual time value (like 8:35am) and I want to use Java's Date and/or Calendar classes rather than calculate this by hand.

Example

Below are some example input values:

  • 720 (720 minutes into the week) so 12pm on Sunday
  • 3840 (3840 minutes into the week) so 4pm on Tuesday

Using Java's Date and Calendar classes how do I retrieve time component for that relative day?

4
Joda Time answer coming in 3... 2... 1...Michael Myers

4 Answers

2
votes

Also, with a Calendar is really easy:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
int minutesToAdd = 720;
Calendar cal = Calendar.getInstance();

cal.setTime(dateFormat.parse("2009/06/21 00:00:00"));  // Next sunday
cal.add(Calendar.MINUTE, minutesToAdd);

Date result = cal.getTime();                           // Voila
1
votes

Divide by 24 hours in a day to get number of days. Remainder is number of minutes into that day.

Divide by 60 to get hour. Remainder is minutes into that hour.

Division and Modulus will get your answer in just a few lines of code. Since this sounds like homework, I'll leave the coding out of this answer.

1
votes

As it sounds like homework, here how it should work:

1) Create yourself a calendar instance for sunday, 0:00 (on any date you wish)
2) Now add your minutes with the appropiate function
3) Now retrieve the time parts from the object
0
votes

tl;dr

LocalDate.now( ZoneId.of( "America/Montreal" ) )
         .with( TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) )
         .atStartOfDay( ZoneId.of( "America/Montreal" ) )
         .plus( Duration.ofMinutes( 720 ) )

Using java.time

The modern way is with the java.time classes.

Which week? I will assume you want the Sunday of the current week.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z );
LocalDate sundayThisWeek = today.with( TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) );

Get the first moment of that date. Do not assume that means 00:00:00 as anomalies such as Daylight Saving Time (DST) may mean another time like 01:00:00.

ZonedDateTime zdt = sundayThisWeek.atStartOfDay( z ); // First moment of the day.

You say you are given an input of a number of minutes. Represent that as a Duration object.

Duration duration = Duration.ofMinutes( x );

Add the Duration object to your ZonedDateTime object.

720 (720 minutes into the week) so 12pm on Sunday

No, 720 minutes may result in some other time-of-day, such as 11 AM or 1 PM in the United States on the DST cutover day.

The objects do all the math for you, and handle anomalies such as Daylight Saving Time.

ZonedDateTime zdtLater = zdt.plus( duration );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.