Practice birth days

The code is as follows:

import java. text. SimpleDateFormat; import java. util. Date; import java. util. Scanner; // Calculate how many days you live public class datetest {public static void main (string [] args) throws exception {function();} Public static void function () throws exception {/ / get the date of birth. Enter system.out.println ("please enter the date of birth (format yyyy MM DD)"); string birthday = new scanner (system. In). Next(); / / convert the string date into a date object simpledateformat SDF = new simpledateformat ("yyyy MM DD") ; // Call the parse method to convert the string into a date object date birthdaydate = SDF parse(birthday); // Get today's Date object date todaydate = new date()// Convert the two dates into millisecond values. The gettime () method is long birthdaysecond = birthdaydate getTime(); long todaySecond=todayDate. getTime(); long second=todaySecond-birthdaySecond; if(second<0) System. out. Println ("not yet born"); else System. out. println(second/1000/24/60/60/365); } }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>