currentTimeMillis()到年,日,分钟和后缀。 (硬) [英] currentTimeMillis() to Years, days, and minutes, and backwords. (hard)
问题描述
我需要将 System.currentTimeMillis()
转换为3个变量:
I need System.currentTimeMillis()
to be converted into 3 variables:
int Years;
int DaysMonths;
int MinutesHours;
我的意思是 DayMonths
,和 MinutesHours
就是这样,比方说我们有2个小时。然后 MinutesHours
应该等于120(因为2小时= 120分钟)。但是一旦它达到24小时,它应该是0并且将它传递给 DaysMonths
。
DaysMonths变量也是一样。
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.
而且我还需要知道如何让这个东西颠倒过来。使用这3个变量,我需要另一种方法从它们获取 System.currentTimeMillis()
。
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我很难解释这一点,但我希望你知道我的意思。我真的很讨厌在java中处理时间。这不是我的事,但我对游戏项目的需要非常糟糕。
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.
推荐答案
创建一个Calendar对象:
Create a Calendar object:
long millis=System.currentTimeMillis();
Calendar c=Calendar.getInstance();
c.setTimeInMillis(millis);
在此之后,您可以从Calendar对象中获取字段:
After this you can get the fields from the Calendar object:
int hours=c.get(Calendar.HOUR);
int minutes=c.get(Calendar.MINUTE);
然后:
int MinutesHours=(hours*60)+minutes;
要返回,您可以使用日历中的set方法:
To go back, you can use the set method in Calendar:
Calendar c=Calendar.getInstance();
c.set(Calendar.MINUTE,minutes);
long millis=c.getTimeInMillis();
这篇关于currentTimeMillis()到年,日,分钟和后缀。 (硬)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!