tl;dr
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ; // The time zone of your business context.
LocalDate input = Instant.ofEpochMilli( yourMillis ).atZone( z ).toLocalDate() ;
LocalDate today = LocalDate.now( z ) ;
LocalDate weekAgo = today.minusDays( 6 ) ;
if ( input.isAfter( today ) ) { … error }
else if ( input.isEqual( today ) ) { return "Today" ; }
else if ( ! input.isBefore( weekAgo ) ) { return input.getDayOfWeek().getDisplayName( TextStyle.FULL , Locale.US ) ; }
else if ( input.isBefore( weekAgo ) ) { return input.format( DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ) ; }
else { … error, unreachable point }
Details
DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
Instead, for a point on the timeline in UTC, use Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Your example of first moment of 1970 UTC happens to be the epoch reference used by the java.time framework. And it happens to be defined as a constant.
Instant start = Instant.EPOCH ;
Add your count of milliseconds.
Instant later = start.plusMillis( yourMillis ) ;
But if your count of milliseconds is always a count since the epoch reference, then you can shorten that code above.
Instant instantInput = Instant.ofEpochMilli( yourMillis ) ; // Determine a moment in UTC from a count of milliseconds since 1970-01-01T00:00Z.
Apparently your goal is to compare dates or day-of-week. Both of those require a time zone. You ignore this crucial issue in your Question. For any given moment, the date varies around the globe by zone. At this moment right now is Monday in the United States, but in New Zealand it is “tomorrow” Tuesday.
Apply a ZoneId
to get a ZonedDateTime
. Same moment, same point on the timeline, but seen through the wall-clock time used by the people of a particular region (a time zone).
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdtInput = instantInput.atZone( z ) ;
Extract the date-only, without a time-of-day and without a time zone.
LocalDate ldInput = zdtInput.toLocalDate() ;
Extract the day-of-week, represented by the DayOfWeek
enum.
DayOfWeek dowInput = ldInput.getDayOfWeek() ;
Subtract six days from now. Represent the six days as TemporalAmount
, either six calendar days in a Period
or six chunks of 24-hours as a Duration
.
Period sixDays = Period.ofDays( 6 ) ;
LocalDate today = LocalDate.now( z ) ;
LocalDate sixDaysBeforeToday = today.minus( sixDays ); // Or LocalDate.now( z ).plusDays( 6 )
Compare. Let's simplify your branching logic. We will work chronologically in reverse order through five cases: future, today, past six days, prior, impossible.
if( ldInput.ifAfter( today ) ) {
System.out.println( "ERROR - input is in the future. Not expected." ) ;
} else if( ldInput.isEqual( today ) ) {
return "Today" ;
} else if ( ! ldInput.isBefore( sixDaysBeforeToday ) ) { // A shorter way of asking "is equal to OR later" is asking "is NOT before".
return dowInput.getDisplayName( TextStyle.FULL , Locale.US ) ; // Let java.time localize the name of the day-of-week.
} else if ( ldInput.isBefore ( sixDaysBeforeToday ) ) {
return ldInput.format( DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ) ;
} else {
System.out.println( "ERROR - Reached impossible point." ) ; // Defensive programming.
}
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
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.