简体   繁体   English

仅使用 currentTimeMillis() 来获取日期

[英]using only currentTimeMillis() to get the date

So I'm totaly lost.所以我完全迷路了。 I have to print out the actual Date and time using only currentTimeMillis() and nothing else.我必须只使用currentTimeMillis()而没有其他任何东西打印出实际的日期和时间。 Getting the time was no problem:获取时间没问题:

long t = java.lang.System.currentTimeMillis();

long seconds = (t / 1000) % 60;
long minutes = (t / (1000 * 60)) % 60;
long hours = (t / (1000 * 60 * 60) % 24) + zone;

But how can I calculate the date taking leap years into consideration?但是如何计算考虑闰年的日期?

Edit: It's homework, hence this weird question.编辑:这是家庭作业,因此这个奇怪的问题。 And we are not allowed to use any other methodes besides currentTimeMillis().并且我们不允许使用除 currentTimeMillis() 之外的任何其他方法。 Operators and alike are fine.操作员等都很好。

There is no way to do this without using anything else .如果不使用其他任何东西,就无法做到这一点。 You have to be more clear about which restrictions there are and why?您必须更清楚有哪些限制以及为什么? Is this homework?这是家庭作业吗?

The simplest way I can think of is:我能想到的最简单的方法是:

long t = java.lang.System.currentTimeMillis();
System.out.println(new Date(t));

But I am sure that's not what you need.但我相信这不是你所需要的。 Please elaborate...请详细说明...

For the timezone related problem, following fix can be used.对于时区相关问题,可以使用以下修复程序。 However this is just one case: other cases need to be handled as well.然而,这只是一种情况:其他情况也需要处理。

//let's say time zone is +5:30
long zone = 6; //instead of 5, keeping value 6, thus added extra 30 minutes
long minuten = (t / (1000 * 60)) % 60;
long stunden = (t / (1000 * 60 * 60) % 24) + zone;
if(minuten < 30){ // this is to take care of the cases where hour has moved ahead
    stunden--;
    minuten+=30;
} else{
    minuten-=30; // else deduct those additional 30 minutes
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM