views:

692

answers:

3

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?

+1  A: 

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.

S.Lott
-1 Using Java's Date and Calendar classes...
victor hugo
+1 he has a point you don't really need the date classes
wds
+1  A: 

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
MicSim
not homework :) just unfamiliar with the java calender classes and was having one of those brain farts...
AtariPete
I just looked up your profile ... Sorry Ataripete ;-)
MicSim
no problem. thanks for taking the benefit of the doubt and answering the question regardless.
AtariPete
+2  A: 

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
victor hugo