java--set the time difference in a text box to change the unit

Source: Internet
Author: User

1. Forward Change


New_time=text1.gettext ();//The time taken from the text box
New_date=formatter_2.parse (new_time);//Convert the time in a text box to a date type
Cal.settime (new_date);

New_time_1=text2.gettext ();//The time taken from the text box
New_date_1=formatter_2.parse (new_time_1);//Convert the time in a text box to a date type
Cal_1.settime (new_date_1);

Long Diff=new_date_1.gettime ()-new_date.gettime ();//Get time difference
Long days=diff/(1000*60*60*24);//difference in number of days
Long hours= (diff-days* (1000*60*60*24))/(1000*60*60);//Hours of difference
Long minutes= (diff-days* (1000*60*60*24)-hours* (1000*60*60))/(1000*60);//number of minutes of difference

   cal.add (calendar.day_of_month, (int)-days);
   cal.add (calendar.hour, (int)-hours);
   cal.add (Calendar.minute, (int)-minutes);

   new_date=cal.gettime ();
   string Test_time=formatter_2.format (new_date);
   stringbuffer test_str=new StringBuffer (). append (Test_time);
   test_time=test_str.tostring ();
   text1.settext (test_time);
   
   string first_time=text1.gettext ();
   
   cal_1.add (Calendar.day_of_month, (int)-days);
   cal_1.add (Calendar.hour, (int)-hours);
   cal_1.add (Calendar.minute, (int)-minutes);
   
   new_date_1=cal_1.gettime ();
   string Test_time_1=formatter_2.format (new_date_1);
   stringbuffer test_str_1=new StringBuffer (). append (Test_time_1);
   test_time_1=test_str_1.tostring ();
   text2.settext (test_time_1);
   
   string last_time_1=text2.gettext ();

2. Backward change

New_time=text1.gettext ();//The time taken from the text box
New_date=formatter_2.parse (new_time);//Convert the time in a text box to a date type
Cal.settime (new_date);

New_time_1=text2.gettext ();//The time taken from the text box
New_date_1=formatter_2.parse (new_time_1);//Convert the time in a text box to a date type
Cal_1.settime (new_date_1);

Long Diff=new_date_1.gettime ()-new_date.gettime ();//Get time difference
Long days=diff/(1000*60*60*24);//difference in number of days
Long hours= (diff-days* (1000*60*60*24))/(1000*60*60);//Hours of difference
Long minutes= (diff-days* (1000*60*60*24)-hours* (1000*60*60))/(1000*60);//number of minutes of difference

Cal.add (Calendar.day_of_month, (int) +days);
Cal.add (Calendar.hour, (int) +hours);
Cal.add (Calendar.minute, (int) +minutes);

   new_date=cal.gettime ();
   string Test_time=formatter_2.format (new_date);
   stringbuffer test_str=new StringBuffer (). append (Test_time);
   test_time=test_str.tostring ();
   text1.settext (test_time);
   
   string first_time=text1.gettext ();
   
   cal_1.add (Calendar.day_of_month, (int) +days);
   cal_1.add (Calendar.hour, (int) +hours);
   cal_1.add (Calendar.minute, (int) +minutes);
   
   new_date_1=cal_1.gettime ();
   string Test_time_1=formatter_2.format (new_date_1);
   stringbuffer test_str_1=new StringBuffer (). append (Test_time_1);
   test_time_1=test_str_1.tostring ();
   text2.settext (test_time_1);
   
   string last_time_1=text2.gettext ();

java--Sets the time difference in a text box to change in units

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.