Dark Horse Programmer-Calculation: 2012-3-17 "to" 2012-4-6 "how many days in between?

Source: Internet
Author: User
Tags date1 dateformat


Calculation:

How many days are there in the middle of 2012-3-17 "2012-4-6"?


Import Java.text.dateformat;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.date;public class DateDemos2 {//Practice://"2012-3-17" to "2012-4-6" how many days in between? /** writing ideas 1.    Converts a date-formatted string into a Date object.    2. Turn the date object into a millisecond value. 3. Subtract, programming days.  * @throws Exception */public static void Main (string[] args) throws Exception {//TODO auto-generated method stubstring S1 = "2012-3-17"; String s2 = "2012-4-6";//Call Custom Party Datetodemos (S1,S2);} private static void Datetodemos (string s1, string s2) throws Exception {//convert date in string to date format DateFormat df = Dateformat.get Dateinstance ();//Format Date Object df = new SimpleDateFormat ("Yyyy-mm-dd");//Converts a date in a string to a date in a Date object date date1 = Df.parse (S1);D Ate date2 = df.parse (s2);//Gets the time millisecond value corresponding to the corresponding date long time1 = Date1.gettime (); Long time2 = Date2.gettime (); Long times = Math.Abs (t IME2-TIME1); System.out.println (times); int day = GetDay (times); System.out.println (s1+ "and" +s2+ "Difference:" +day+ "Day");} private static int GetDay (long times) {//TODO auto-generated method Stubreturn (int) (TimeS/1000/60/60/24);}} 

To run the program:





Dark Horse Programmer-Calculation: 2012-3-17 "to" 2012-4-6 "how many days in between?

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.