Reprint please indicate the source, thank you ~ ~
This is a tool-like blog that gets 0 points of the day and formats the time as a standard form.
There are two ways to get a 0-point time:
public static long gettodayzero () {Date date = new date (); Calendar cal = Calendar.getinstance (); Cal.settimezone (Timezone.gettimezone ()); Cal.settime (date); Cal.set (Calendar.hour, 0 ); Cal.set (Calendar.second, 1 ); Cal.set (Calendar.minute, 0 ); Cal.set (Calendar.millisecond, 0 ); System.out.println ( "Today Zero:" + Cal.gettimeinmillis ()); return cal.gettimeinmillis (); }
I always do not like this way, because on different mobile phones, I test there is a bug, may be I write a bit of a problem, welcome everyone to tell me. I added the utc+8 time zone, incredibly get or utc+0 timezone, I am very puzzled.
So I always use this way:
publicstaticlonggetTodayZero() { new Date(); long24*60*60*1000//每天的毫秒数 //date.getTime()是现在的毫秒数,它 减去 当天零点到现在的毫秒数( 现在的毫秒数%一天总的毫秒数,取余。),理论上等于零点的毫秒数,不过这个毫秒数是UTC+0时区的。 //减8个小时的毫秒值是为了解决时区的问题。 return86060 *1000);}
The code is simple and convenient.
Then format the time:
@SuppressLint("SimpleDateFormat") publicstaticgetTime(long time) { SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d1=new Date(time); return format.format(d1); }
What you get is, for example today: 2105-4-1 00:00:00
Android gets the millisecond value for 0 points of the day and formats the time