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.
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();
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With