Android obtains the millisecond value at of the current day and formats the time.
Please specify the source for reprinting. Thank you ~~
This is a tool blog used to get the zero time of the day and format the time as a standard format.
You can obtain the zero point time in either of the following ways:
public static long getTodayZero() { Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTimeZone(TimeZone.getTimeZone(UTC+8)); 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 have never liked this method, because on different mobile phones, I tested bugs, which may be a problem I wrote. Please tell me. I added the UTC + 8 time zone to get the UTC + 0 time zone, which makes me very puzzled.
So I always use this method:
Public static long getTodayZero () {Date date = new Date (); long l = 24*60*60*1000; // The number of milliseconds per day // date. getTime () is the current number of milliseconds, which is the number of milliseconds from the zero point of the day to the present (the current number of milliseconds % the total number of milliseconds of the day, take the remainder .), In theory, it is equal to the number of milliseconds at zero, but the number of milliseconds is in UTC + 0 time zone. // Reduce the value of 8 hours in milliseconds to solve the time zone problem. Return (date. getTime ()-(date. getTime () % l)-8*60x60*1000 );}
The code is concise and convenient.
Then, format the time:
@SuppressLint(SimpleDateFormat) public static String getTime(long time) { SimpleDateFormat format=new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); Date d1=new Date(time); return format.format(d1); }