Android obtains the millisecond value at of the current day and formats the time.

Source: Internet
Author: User

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);      }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.