Java calculates two date months, days

Source: Internet
Author: User
Tags date1 dateformat set time

Package Com.myjava;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.Calendar; public class MyCalendar {    /**     * @param args     * @throws parseexception     *    /public static void main ( String[] args) throws ParseException {        System.out.println (mycalendar.getmonthspace ("1982-12-8", "2012-12-12"));    } /**     *      * @param date1 <String>     * @param date2 <String>     * @return int     * @throws ParseException     *    /public static int Getmonthspace (string date1, String date2)            throws ParseException { C15/>int result = 0;        SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");        Calendar C1 = Calendar.getinstance ();        Calendar C2 = calendar.getinstance ();        C1.settime (Sdf.parse (date1));        C2.settime (Sdf.parse (Date2));        result = C2.get (calendar.month)-C1.get (calendar.month);        return result = = 0? 1:math.abs (result);}    }

     * @param date1 <String>     * @param date2 <String>     * @return int     * @throws parseexception     */
   public int Getdatespace (String date1, String date2)            throws parseexception {        int result = 0;        Calendar calst = Calendar.getinstance ();        Calendar caled = Calendar.getinstance ();        Calst.settime (Parsedate ("YyyyMMdd", Date1));        Caled.settime (Parsedate ("YyyyMMdd", Date2));          Set time is 0 o'clock            calst.set (calendar.hour_of_day, 0);            Calst.set (calendar.minute, 0);            Calst.set (Calendar.second, 0);            Caled.set (calendar.hour_of_day, 0);            Caled.set (calendar.minute, 0);            Caled.set (Calendar.second, 0);           Get two dates with a difference of days            int day = ((int) (Caled.gettime (). GetTime ()/1000)-(int) (Calst.gettime (). GetTime ()/1000))/3600/ ;                    return days;       }

public static final int Daysbetween (date early, date late) {Java.util.Calendar Calst = java.util.calendar.g            Etinstance ();            Java.util.Calendar caled = Java.util.Calendar.getInstance ();             Calst.settime (early);             Caled.settime (late);             Set time is 0 o'clock Calst.set (java.util.Calendar.HOUR_OF_DAY, 0);             Calst.set (Java.util.Calendar.MINUTE, 0);             Calst.set (Java.util.Calendar.SECOND, 0);             Caled.set (Java.util.Calendar.HOUR_OF_DAY, 0);             Caled.set (Java.util.Calendar.MINUTE, 0);            Caled.set (Java.util.Calendar.SECOND, 0); The number of days to get the difference between two dates int day = ((int) (Caled.gettime (). GetTime ()/+)-(int) (Calst. GetTime (                      ). GetTime ()/1000)/3600/24;       return days;            } public static void Main (string[] args) {Date earlydate = new Date ();            Date latedate = new Date (); DateFormat df = dateforMat.getdateinstance ();                try {earlydate = Df.parse ("2009-09-21");            Latedate = Df.parse ("2009-10-16");              } catch (ParseException e) {e.printstacktrace ();             } int days = Daysbetween (earlydate,latedate);       System.out.println (days); }

Java calculates the number of months and days between two dates

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.