Java calculates the intermediate days based on the start time and end time, and prints

Source: Internet
Author: User
Tags diff

Java calculates the intermediate days based on the start time and end time, and prints

import java.text.simpledateformat;import java.util.date;public class calcdate {     static simpledateformat sdf = new simpledateformat ("Yyyy-MM-dd  hh:mm:ss ");    static simpledateformat ymd = new  SimpleDateFormat ("Yyyy-mm-dd");     public static void main (String[]  args) throws exception{        date begintime =  Sdf.parse ("2015-04-23 12:32:44");//        date endtime  = sdf.parse ("2015-04-23 14:56:44");        date  Endtime = sdf.parse ("2015-04-29 14:56:44");         long  diff =  (Endtime.gettime ()  - begintime.gettime ())  /  (86400 * 1000 );      &nbsP;  system.out.println ("diff = "  + diff);         date sb = null;        date se =  null;        if (diff == 0) {             sb = beginTime;             se = endTime;             string ss = string.format ("%d> sb=%s, se=%s",  1,  coder.datetimetostr (SB),  coder.datetimetostr (SE));         &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (ss);         }else {             int i = 0;             while  (I <= diff)  {                 if  (i == 0)  {                     sb  = beginTime;                     se = sdf.parse (Ymd.format (SB)  +  " 23:59:59");                 } else  if  (I == diff)  {                     sb = sdf.parse (Ymd.format (endTime)  +   " 00:00:00");                     se = endtime;                 } else {                     se = new date (Begintime.gettime ()  + i  * 24 * 60 * 60 * 1000);                     sb = sdf.parse (Ymd.format (SE )  +  " 00:00:00");                     se = sdf.parse (Ymd.format (SE)  +  " 23:59:59" );                }                 String ss  = string.format ("%d> sb=%s, se=%s ",  i, coder.datetimetostr (Sb),  coder.datetimetostr (Se));   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (ss);                 i++;             }         }    }}
diff = 60> sb=2015-04-23 12:32:44, se=2015-04-23 23:59:591> sb=2015-04-24 00:00:00, se=2015-04-24 23:59:592> Sb= 2015-04-25 00:00:00, se=2015-04-25 23:59:593> sb=2015-04-26 00:00:00, se=2015-04-26 23:59:594> Sb=2015-04-27 00:00:00, se=2015-04-27 23:59:595> sb=2015-04-28 00:00:00, se=2015-04-28 23:59:596> Sb=2015-04-29 00:00:00, Se= 2015-04-29 14:56:44


Java calculates the intermediate days based on the start time and end time, and prints

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.