简体   繁体   中英

How can I check if a millisecond timestamp is at midnight in my current time zone?

I have a timestamp in milliseconds that is 1349557200000 . How can I check that it occurs at midnight at my current time zone?

The best I've come up with is something like:

Calendar mycal = Calendar.getInstance();
Integer offset = (mycal.get(Calendar.ZONE_OFFSET) + mycal.get(Calendar.DST_OFFSET)) * (60 * 1000);
Boolean isAtMidnight = 1349557200000L % (24L * 60L * 60L * 1000L)) == (long) offset ? true : false;

This looks a tad complicated. I was wondering if there are any shortcut methods that I might be missing. Thanks.

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(milliseconds);
return cal.get(Calendar.HOUR_OF_DAY) == 0
       && cal.get(Calendar.MINUTE) == 0
       && cal.get(Calendar.SECOND) == 0
       && cal.get(Calendar.MILLISECOND) == 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM