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 );
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.
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 )
For Android, see the ThreeTenABP project for a back-port of most of the java.time functionality.
You need to multiply it by 1000 since Java and Unix time are not the same.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date dateFormat = new java.util.Date(1493193408L * 1000);
String weekday = sdf.format(dateFormat );
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