Java Date-time operations

Source: Internet
Author: User


package com.gds.web.util;import java.text.parseexception;import java.text.simpledateformat; import java.util.calendar;import java.util.date;public class dateoputil {/**       *  @param  args      *  @throws   parseexception       */      public  Static void main (String[] args)  throws Exception {           system.out.println (Daysbetween ("2015-09-28 10:10:10", "2015-10-01  23:00:00 "));      }        /*      * Date format Conversion  yyyy-mm-dd     */    public  static string turndateformat (string date) throws exception    {     simpledatefOrmat format = new simpledateformat ("Yyyy-mm-dd");    return  Format.format (Format.parse (date));     }        /*      *  Two date comparison time      */    public  static boolean dateequal (string begin_date,string end_date) throws Exception     {    SimpleDateFormat format = new  SimpleDateFormat ("Yyyy-mm-dd");     date d1 = format.parse (begin_date);         date d2 = format.parse (end_date);         if (D1.gettime ()  == d2.gettime ())          {        return true ;         }        return false;    }     /*      *  Two date comparison time      */    public  static boolean datedef (string now_date,string use_date) throws Exception     {    simpledateformat format = new simpledateformat ( "Yyyy-mm-dd");     date now_time = format.parse (now_date);         date end_time = format.parse (use_date);         if (End_time.gettime ()  >= now_time.gettime ())          {        return true ;         }        return false;     }        /**     *  calculates the number of days separated by two dates        * calculation of the date format of a string       */      public  static int daysbetween (string smdate,string bdate)  throws Exception{           simpledateformat sdf=new simpledateformat (" Yyyy-mm-dd ");          calendar cal =  Calendar.getinstance ();             cal.settime ( Sdf.parse (smdate));             long time1  = cal.gettimeinmillis ();                          cal.settime (Sdf.parse (bdate));              long time2 = cal.gettimeinmillis ();                  long between_days= ( TIME2-TIME1)/(1000*3600*24);                      return integer.parseint (String.valueOf (between_days));        }        /**       *  get the day after the specified date       *        *  @param  specifiedDay      *  @return        *  @throws  Exception      */       public static string getspecifieddaybefore (String specifiedDay,int  i)  {//can be used with new date (). Tolocalstring () pass parameter        calendar c = calendar.getinstance ();            Date date = null;            try {                date = new simpledateformat ("Yy-MM-dd"). Parse ( Specifiedday);           } catch  ( Parseexception e)  {                e.printstacktrace ();           }            c.settime (date);            int day = c.get (calendar.date);            c.seT (calendar.date, day + i);               string dayafter = new simpledateformat ("Yyyy-MM-dd")                     .format ( C.gettime ());           return dayafter;       }            /**      *  determine the current date is the day of the week      *       *  @param  pTime  time to judge      *  @return  dayForWeek  Judging results      *  @Exception   Abnormal      */ public  static int dayforweek (string ptime)  throws Exception {   simpledateformat format = new&nbsp SimpleDateFormat ("Yyyy-mm-dd");   calendar c = calendar.getinstance ();   C.settime (Format.parse (ptime));   int dayforweek = 0;  if (C.get (Calendar.DAY _of_week)  == 1) {  dayforweek = 7;  }else{  dayforweek =  c.get (Calendar.day_of_week)  - 1;  }  return dayForWeek; }    }


Package com.gds.web.util;import java.text.dateformat;import java.text.parseexception;import  java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import  org.apache.commons.lang3.stringutils;import com.ctc.wstx.util.stringutil;/** *   @author  microe * * */public class FixedDate { /**       *  get the day before the specified date       *       *   @param  specifiedDay      *  @return        *  @throws  Exception      */       Public static string getspecifieddaybefore (string specifiedday,int i)  {//can be used with new  date (). tolocalstring () Pass parameters        Calendar c =  Calendar.getinstance ();            date date = null;            try {                date = new simpledateformat ("Yy-MM-dd"). Parse (SpecifiedDay) ;           } catch  (ParseException e)  {                E.printstacktrace ();           }            c.settime (date);            int day = c.get (calendar.date);            c.set (calendar.date, day - i);              &nbSp String dayafter = new simpledateformat ("Yyyy-mm-dd")                     .format (C.getTime ());            return dayAfter;       }/* *  get a specified date, week 1st of Week  */public static string getmondayofthisweek ( String fixed_date)  throws Exception { DateFormat fmt =new  SimpleDateFormat ("Yyyy-mm-dd");             date  date = fmt.parse (fixed_date);  calendar c = calendar.getinstance ();  C.settime (date);  int day_of_week = c.get (Calendar.day_of_week)  - 1; if   (day_of_week == 0)  day_of_week = 7; c.add (Calendar.date, -day_of_week  + 1);  return fmt.format (C.gettime ()) +  " 00:00:00";} /* *  gets the specified date, a date of this month  */public static string getdateofthismonth (String fixed_ Date)  throws exception {simpledateformat fmt = new simpledateformat (" Yyyy-mm-dd ");  //gets the first day of the current month: Date date1 = fmt.parse (fixed_date);         calendar c = calendar.getinstance ();           c.settime (date1);         c.add (Calendar.MONTH,  0);         c.set (calendar.day_of_month,1);//set to number 1th, the current date is the first day of the month  return fmt.format (C.gettime ()) +  " 00:00:00";}  /**      *  start time of the current quarter, i.e. 2012-01-1 00:00:00       *      *  @return       */      public&nbsP;static string  getcurrentquarterstarttime (string fixed_date)  throws Exception     {    SimpleDateFormat fmt = new  SimpleDateFormat ("Yyyy-mm-dd");D ate date1 = fmt.parse (fixed_date);         calendar c = calendar.getinstance ();          c.settime (date1);         int  Currentmonth = c.get (Calendar.month)  + 1;          if  (currentmonth >= 1 && currentmonth <= 3)               c.set (calendar.month, 0);          else if  (currentmonth >= 4 && &NBSP;CURRENTMONTH&NBSP;&LT;=&NBSP;6)  &nbsP;           c.set (calendar.month, 3);          else if  (currentmonth >= 7 && &NBSP;CURRENTMONTH&NBSP;&LT;=&NBSP;9)               c.set (calendar.month, 4);         else if  ( CURRENTMONTH&NBSP;&GT;=&NBSP;10&NBSP;&AMP;&AMP;&NBSP;CURRENTMONTH&NBSP;&LT;=&NBSP;12)               c.set (calendar.month, 9);          c.set (calendar.date, 1);          return fmt.format (C.gettime ())  +  " 00:00:00";              }//get first half, second half start time public static string  Gethalfyearstarttime (string fixed_dATE)  throws exception{simpledateformat fmt = new simpledateformat ("Yyyy-MM-dd") ;D Ate date1 = fmt.parse (fixed_date);         calendar  c = calendar.getinstance ();          c.settime ( DATE1);         int currentmonth = c.get (Calendar.MONTH)  + 1;         String date = null;          try {              if  (currentmonth >= 1 && currentmonth  &LT;=&NBSP;6) {                  c.set (calendar.month, 0);              }else if (CURRENTMONTH&NBSP;&GT;=&NBSP;7&NBSP;&AMP;&AMP;&NBSP;CURRENTMONTH&NBSP;&LT;=&NBSP;12) {                 c.set ( CALENDAR.MONTH,&NBSP;6);             }              c.set (Calendar.DATE, 1);              date = fmt.format (C.getTime ( )) +  " 00:00:00";         } catch  (Exception  e)  {             e.printstacktrace ();         }          return date;     }  /**      *   Start time of the current year, i.e. 2012-01-01 00:00:00      *      *  @return        */     public   static string getcurrentyearstarttime ( String fixed_date)  throws Exception    {      Simpledateformat fmt = new simpledateformat ("Yyyy-mm-dd");     date  date1 = fmt.parse (fixed_date);         calendar c  = calendar.getinstance ();          c.settime (date1);          c.set (calendar.month, 0);          c.set (calendar.date, 1);          return fmt.format (C.gettime ()) +  " 00:00:00";     }        &nbSp;     public static void main (String[] args)  throws  Exception {system.out.println ( getcurrentquarterstarttime ("2015-09-09")  );}}


Java Date-time operations

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.