Reputation: 5905
I creating calendar instance by providing timezone and then setting day, month and year to calendar. But when I am trying to gettime in millisecond then it always returning dateTime in millisecond -1 day.
Here is How I am setting Calendar.
DatePickerDialog datePickerDialog = new DatePickerDialog(Objects.requireNonNull(getContext()), R.style.DialogTheme, (view, year, month, dayOfMonth) -> {
Calendar calendar = new DateUtils().getCountryCalendar();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, (month));
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long selectedDate = calendar.getTime().getTime();
}, mYear, mMonth, mDay);
I am setting bu it returning me selectedDate as 1624986000000 which is Tue Jun 29 2021 22:30:00
Here is getCountryCalendar() method.
If I am selecting 30th June then it should return 30th June in millisecond. I don't know why it's happening. Any suggestion are appreciated.
public Calendar getCountryCalendar() {
return Calendar.getInstance(TimeZone.getTimeZone("Asia/Bangkok"));
}
Upvotes: 0
Views: 552
Reputation: 339043
LocalDate
.of( y , m , d )
.atStartOfDay( ZoneId.of( "Asia/Bangkok" ) )
.toInstant()
.toEpochMilli()
The modern solution uses the java.time classes that years ago supplanted the legacy date-time classes such as Calendar
.
Build your date-only value.
LocalDate ld = LocalDate.of( y , m , d ) ;
Apparently you want the first moment of the day in that date as seen in a particular time zone. Do not hard-code a time of 00:00:00.0. That time of day may not exist on some dates in some time zones. Let java.time determine the first moment of the day.
ZoneId z = ZoneId.of( "Asia/Bangkok" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;
Apparently you want to convert that to a count of milliseconds from the epoch reference of first moment of 1970 as seen in UTC. The Instant
class represents a moment as seen in UTC.
long millis = zdt.toInstant().toEpochMilli() ;
Upvotes: 2
Reputation: 658
The timestamp 1624986000000
indeed is Jun 30, 00:00:00
by Asia/Bangkok
time zone. You can even check it here.
On the other side, Colcata and Colombo timezones render this timestamp as Jun 29 2021 22:30:00
.
Since you haven't shared the code where you convert timestamp to 22:30:00, my assumption that in that piece of code you are not using the right timezone (but rather Asia/Colcata
, or Asia/Colombo
, or something else that matches GMT +05:30
)
Upvotes: 1