Reputation: 4460
I am taking the Current date by using the following sets of lines.
java.util.Calendar calc = java.util.Calendar.getInstance();
int day = calc.get(java.util.Calendar.DATE);
int month = calc.get(java.util.Calendar.MONTH)+1;
int year = calc.get(java.util.Calendar.YEAR);
I am getting date but what if Mobile date is not of the current date. I am matching my mobile date with server date Their date is current and mobile date is not current.
Is their any validation if mobile date is not current then show notification.
Upvotes: 1
Views: 188
Reputation: 338785
java.time.Duration // Represent a span of time, unattached to the timeline, on a scale of hours-minutes-seconds-subsecond.
.between
(
Instant.ofEpochMilli( count ) , // Current time from a remote server, as a count of milliseconds from 1970-01-01T00:00Z.
Instant.now() // Current time as tracked by the clock on this local device.
)
For any given moment, both the time and the date varies around the globe by time zone. At any given moment, it can be “tomorrow” in Tokyo Japan while simultaneously “yesterday” in Toledo Ohio US.
You are using terribly flawed classes that were supplanted by the modern java.time classes built into Java 8 and later.
Android 26 and later comes with an implementation of java.time. For earlier Android, the latest tooling provides most of the java.time functionality via “API desugaring”.
LocalDate
To represent a moment, use java.time.LocalDate
.
Specify the time zone by which you want to perceive the date of the current moment.
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
LocalDate today = LocalDate.now( z ) ;
You are likely getting the current moment from a remote server as a count of milliseconds from the epoch reference of first moment of 1970 in UTC (an offset of zero hours-minutes-seconds).
You can convert that count into a moment via the Instant
class.
Instant instant = Instant.ofEpochMilli( count ) ;
To perceive a date, apply your desired time zone to get a ZonedDateTime
object.
ZonedDateTime zdt = instant.atZone( z ) ;
Extract the date.
LocalDate ld = zdt.toLocalDate() ;
Compare.
boolean sameDates = today.isEqual( ld ) ;
To see if the local device time differs from the remote server time, just count the difference in each count-from-epoch. Represent the difference using Duration
class.
Instant local = Instant.now() ;
Instant remote = Instant.ofEpochMilli( count ) ;
Duration duration = Duration.between( remote , local ) ;
Upvotes: 2
Reputation: 10349
The URL http://timeanddate.com gives current date and time for your current location.
If you can send HTTP request to this server you will get html or xml file. If you decode that file with parsers available in android you will get current date.
I didn't tried, but definitely possible. Try if you are interested.
Upvotes: 0
Reputation: 13588
You can just notify user to change time and date. I don't think you can set time and date programmatically.
Upvotes: 1