Android Java简单的时间戳到日期转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3661137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:13:30  来源:igfitidea点击:

Java simple Timestamp to Date conversion

androidunixdatetimestamp

提问by Tom G

I've been trying to find the answer to this for a while today and there's just so much contradictory information....

我今天一直试图找到这个问题的答案,但有很多相互矛盾的信息......

What I'd like to do is get a current unix timestamp in android, and then convert it to a format that allows me to getHours() and getMinutes().

我想要做的是在 android 中获取当前的 unix 时间戳,然后将其转换为允许我使用 getHours() 和 getMinutes() 的格式。

I'm currently doing this:

我目前正在这样做:

int time = (int) (System.currentTimeMillis());
Timestamp ts = new Timestamp(time);
mHour = ts.getHours();
mMinute = ts.getMinutes();

But it's not giving me a correct value for hour or minute (it's returning 03:38 for the current East-coast time of 13:33).

但它没有给我一个正确的小时或分钟值(它返回 03:38 当前东海岸时间 13:33)。

回答by Paul Burke

This works:

这有效:

final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
mHour = date.getHours();
mMinute = date.getMinutes();

回答by Mark B

Just use the Java Calendarclass.

只需使用 Java Calendar类。

Calendar c = new GregorianCalendar();  // This creates a Calendar instance with the current time
mHour = c.get(Calendar.HOUR);
mMinute = c.get(Calendar.MINUTE);

Also note that your Android emulator will return times in GMT for the current time. I advise testing this type of code on a real device.

另请注意,您的 Android 模拟器将返回当前时间的 GMT 时间。我建议在真实设备上测试这种类型的代码。

回答by shanmugamgsn

Take a look of this.

看看这个。

Hope this is wat you need.

希望这是你需要的。

Android Simple Date Format

Android 简单日期格式

Good Luck!!

祝你好运!!

回答by felix fan

int time = (int) (System.currentTimeMillis());

here you should use longinstead of int.

在这里你应该使用long而不是int

回答by Dawid Drozd

Use Timeclass form Google it is the best for this kind of job specialy it has good performance not like Calendar.

,使用T IME类形式谷歌是最适合这样的工作specialy它有不错的表现并不像日历。