6

I need System.currentTimeMillis() to be converted into 3 variables:

int Years;
int DaysMonths;
int MinutesHours;

What I mean by DayMonths, and MinutesHours is that, let's say for example we have 2 hours. Then MinutesHours should equale 120 (because 2hours = 120 minutes). But once it reaches 24hours, it should be 0 and passe it to DaysMonths. Same thing for the DaysMonths variable.

And I also need to know how to get this thing reversed. Using this 3 variables, I need an other method to get the System.currentTimeMillis() from them.

I'm having hard time to explain this, but I hope you know what I mean. I really hate dealing with time in java. It's not my thing, but I need it really bad for a game project.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Reacen
  • 2,312
  • 5
  • 23
  • 33
  • 2
    http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html – Brian Roach Nov 04 '11 at 18:51
  • 1
    Brian did you even read ? I mean hours and minutes should be mixed in one variable, there is no information about that on your link, and no need to give negative feedback. – Reacen Nov 04 '11 at 18:54
  • You certainly can't get milliseconds back after you've thrown away even the seconds in the conversion. Also, getting this stuff right is hard, having to deal with DST, leap seconds etc. Use a library. – Daniel Fischer Nov 04 '11 at 18:55
  • 1
    @Reacen: Given the individual values and an understanding of Calendar, it's not exactly hard to combine things: `minutesHours = hours * 60 + minutes` etc... How much *did* you try before asking the question? I'd personally recommend using Joda Time instead of java.util.Calendar, mind you... – Jon Skeet Nov 04 '11 at 18:55
  • This kind of seems like simple math, although if you're not dealing with absolute dates, you'd need to decide how to handle a "month" not having a consistent number of days. – Dave Newton Nov 04 '11 at 18:56
  • Also note that "DaysMonths" is ambiguous - you need to define *exactly* what you mean, given that months can vary in length. Are you actually after "day of year"? – Jon Skeet Nov 04 '11 at 18:57
  • http://en.wikipedia.org/wiki/Multiplication & http://en.wikipedia.org/wiki/Division_(mathematics) Hint: There's 86400 seconds in a day. – Brian Roach Nov 04 '11 at 18:59
  • @Jon Skeet: Yes. It is too much math to do, I wanted to use loops to calculate that manually to avoid the math thing. – Reacen Nov 04 '11 at 18:59
  • Sounds like you should create some sort of `Date` extension that has methods for you to get this sort of information that you want. The class will be the one to internally manage these 3 variables, which will be used by your methods to compute the various values you want. Some other state will likely be needed to support going backwards. – Nate W. Nov 04 '11 at 19:00
  • @BrianRoach Oh, sure, just gloss over the whole leap second controversy. – Dave Newton Nov 04 '11 at 19:00
  • @Reacen: What exactly is "too much math to do"? Multiplying hours by 60 and adding minutes? Note that Calendar already lets you get the day of year... – Jon Skeet Nov 04 '11 at 19:01
  • 1
    @Reacen "To avoid the math thing?" ... Because doing it with loops would be what, easier? – Dave Newton Nov 04 '11 at 19:02
  • not everyone is good at math. I was told I will need to do some kind of math like this: variable %= (1000*60). Which i'm really not good at. – Reacen Nov 04 '11 at 19:12
  • 1
    no math, no programming skills – bestsss Nov 04 '11 at 19:40

2 Answers2

17

Create a Calendar object:

long millis=System.currentTimeMillis();
Calendar c=Calendar.getInstance();
c.setTimeInMillis(millis);

After this you can get the fields from the Calendar object:

int hours=c.get(Calendar.HOUR);
int minutes=c.get(Calendar.MINUTE);

Then:

int MinutesHours=(hours*60)+minutes;

To go back, you can use the set method in Calendar:

Calendar c=Calendar.getInstance();
c.set(Calendar.MINUTE,minutes);
long millis=c.getTimeInMillis();
The Heist
  • 1,444
  • 1
  • 16
  • 32
Andres Olarte
  • 4,380
  • 3
  • 24
  • 45
2

Please consider using this library.

http://joda-time.sourceforge.net/

It has a lot of usefull date and time manipulating functions which are really simple!

 DateTime dt = new DateTime(2005, 3, 26, 12, 0, 0, 0);
  DateTime plusPeriod = dt.plus(Period.days(1));
  DateTime plusDuration = dt.plus(new Duration(24L*60L*60L*1000L));
r0ast3d
  • 2,639
  • 1
  • 14
  • 18