Java gets the number of milliseconds for a specific time __java

Source: Internet
Author: User

Method One:

Use the Date class: DATE.UTC (int year,int month, int date,int hrs, int min,int sec);

Long ms = DATE.UTC (2009, 0, 1, 0, 0, 0);//Time: From January 1, 1970 0:0 0 seconds, to

January 1, 2009, 0:0 the number of milliseconds in 0 seconds.


Note: the DATE.UTC () function is deprecated and is not advocated for use.

Method Two:

Use the Calendar class:

Function ditto

Calendar C = calendar.getinstance ();
C.set (calendar.year,2009);
C.set (calendar.month,0);
C.set (calendar.date,1);
C.set (calendar.hour_of_day,0);
C.set (calendar.minute,0);
C.set (calendar.second,0);
Long ms = C.gettimeinmillis ();

The method of obtaining time by the number of milliseconds starting at a specific time: code continues above the

Long mstime = 91816645*1000;

Long UTC = ms + Mstime;

C.settimeinmillis (UTC);
Date d = c.gettime ();

SimpleDateFormat SF = new SimpleDateFormat ("Yyyy_mm_dd HH:mm:ss");

String time = Sf.format (d);


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.