tl;dr
It should return: 5, which is Friday, … 6 is the result given back.
The terrible Calendar
class has many problems.
- One problem is its use of shifting behavior implicitly depending on the JVM’s current default locale. When your code runs on a JVM set to United States locale, you will find Thursday to be a different day of the week of Sunday-Saturday, than in Europe or other places where a week is Monday-Sunday.
- Another problem is crazy zero-based counting, 0-6 for days of the week, and 0-11 for months.
Use java.time instead. The counting is sane (1-7 for Monday-Sunday), and the time zone behavior can be made predictable and explicit.
Important: Time zone is crucial. For more eastern zones, your moment appears as Friday. For more western zones, your moment appears as Thursday.
Instant // Represent a moment in UTC.
.ofEpochMilli( // Parse your count-from-epoch 1970-01-01T00:00:00Z.
1_564_088_400_000L // Use underscores where you like, to make numeric literals more readable.
) // Returns an `Instant` object.
.atZone( // Adjust from UTC to some time zone.
ZoneId.of( "Asia/Tokyo" ) // Specify time zone using `Continent/Region` format, never 2-4 letter pseudo-zones such as `EST` or `CST` or `IST`.
) // Returns a `ZonedDateTime` object.
.getDayOfWeek() // Returns a `DayOfWeek` object.
.getDisplayName( // Generate automatically-localized string for the name of the day-of-week.
TextStyle.FULL , // How long or abbreviated.
Locale.US // Locale determines (a) human language for translation, and (b) cultural norms for issues such as abbreviation, punctuation, capitalization.
) // Returns a `String`.
See this code run live at IdeOne.com.
Friday
If you insist on a number 1-7 for Monday-Sunday, call getValue
instead of .getDisplayName
.
.getValue()
5
Avoid legacy date-time classes
I am currently working on app which needs to works with dates, hours and such.
Then you should stop using the terrible date-time classes that were obsoleted years ago by the adoption of JSR 310. Use only the modern java.time classes. Never use Date
or Calendar
.
Count-from-epoch
from a given date (in milliseconds).
If you mean a count of milliseconds since the epoch reference of first moment of 1970 in UTC, then parse as an Instant
. An Instant
represents a moment in UTC, a specific point on the timeline.
Instant instant = Instant.ofEpochMilli( 1_564_088_400_000L ) ;
See this code run live at IdeOne.com.
instant.toString(): 2019-07-25T21:00:00Z
Date
getting the right day of the week
Determining a date from a moment requires the context of a time zone. For any given moment the date varies around the globe by zone. The date can simultaneously be “tomorrow” in Paris France while “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of Continent/Region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
To adjust a moment into a time zone, apply a ZoneId
to your Instant
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
If you want to view the date through the lens of UTC rather than some other time zone, use OffsetDateTime
class.
ZoneOffset offset = ZoneOffset.UTC ; // Constant for UTC (an offset of zero hours-minutes-seconds).
OffsetDateTime odt = instant.atOffset( offset ) ;
If you want to go this route (UTC), replace zdt
in code below with this odt
.
Day of week
Interrogate for the day-of-week using the DayOfWeek
enum.
DayOfWeek dow = zdt.getDayOfWeek() ;
or example, for the date: 1564088400000 It should return: 5,
No, I suggest you work with smart objects rather than dumb integers. Rather than using 5
to mean Friday
(which, by the way, would mean Thursday
in the United States), pass DayOfWeek
enum objects around your code base.
To report the day-of-week to use, localize.
String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;
Or, if you insist, generate a number using the ISO 8601 numbering scheme of 1-7 for Monday-Sunday.
int dayOfWeekNumber = dow.getValue() ;
private final String[] mDaysOfTheWeek = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
No need to roll-your-own. Use the DayOfWeek
enum.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.