Android gets the millisecond value for 0 points of the day and formats the time

Source: Internet
Author: User

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

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.