将unix时间转换为工作日 [英] Convert unix time to week day
问题描述
如何将时间从Unix时间戳转换为工作日?例如,我要将 1493193408
转换为星期三
。
How can I convert time from unix timestamp to week day? For example, I want to convert 1493193408
to Wednesday
.
我尝试了上面的代码,但始终显示星期日
..
I tryed code above, but It always shows Sunday
..
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date dateFormat = new java.util.Date(1493193408);
String weekday = sdf.format(dateFormat );
推荐答案
使用java.time
其他答案使用麻烦的旧日期时间类(现在已被遗留)取代了java.time类。
Using java.time
The other Answers use the troublesome old date-time classes, now legacy, supplanted by the java.time classes.
时区至关重要确定日期,从而获得星期几。
Time zone is crucial in determining a date, and therefore getting a day-of-week.
从UTC 1970年以来的秒数中获取即时信息。应用时区以获取ZonedDateTime。从那里提取DayOfWeek枚举对象。要求该对象自动本地化以生成其名称的字符串。
Get an Instant from your count of while seconds since the epoch of 1970 in UTC. Apply a time zone to get a ZonedDateTime. From there extract a DayOfWeek enumerate object. Ask that object to automatically localize to generate a string of its name.
Instant.ofEpochSecond( 1_493_193_408L )
.atZone( ZoneId.of( "America/Montreal" ))
.getDayOfWeek()
.getDisplayName( TextStyle.FULL , Locale.US )
对于Android,请参阅ThreeTenABP项目以获取大多数java.time功能的后向端口。
For Android, see the ThreeTenABP project for a back-port of most of the java.time functionality.
这篇关于将unix时间转换为工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!