Java Time class Simple integration

Source: Internet
Author: User

package com.rengh.utils;import java.text.parseexception;import java.text.simpledateformat; import java.util.date;import java.util.locale;public class dateutils {     /**  formatted time  */    public static string formattime (long  showtime)  {        return new simpledateformat (" Yyyy-mm-dd hh:mm:ss ",  locale.getdefault ()). Format (long                 .valueof (showTime));    }     /**  Formatting Time  */    public static string formattime (string showtime)  {        return new  SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss",  locale.getdefault ()). Format (long            &nbSp;    .valueof (showTime));    }    /**  The number of milliseconds to get the time  */    public static long getmillis ()  throws  Parseexception {        return system.currenttimemillis ();     }    /**  gets the number of milliseconds for the specified time, parameter format:  YYYY-MM-DD HH:MM:SS, Return NULL for calculation failure  */    public static long getmillis (STRING DSTR)  throws ParseException {        SimpleDateFormat  Sdf = new simpledateformat ("Yyyy-mm-dd hh:mm:ss");         date date = sdf.parse (DSTR);         long  s1 = date.gettime ();        return s1;     }    /**  Returns the number of milliseconds for the specified time   decrement   The number of milliseconds for the current date; parameter format:  YYYY-MM-DD HH:MM:SS, return NULL for calculation failure  */     public static long getmillisdistancenow (STRING DSTR)  throws  Parseexception {        simpledateformat sdf = new  simpledateformat ("Yyyy-mm-dd hh:mm:ss");        date  Date = sdf.parse (DSTR);        long s1 =  Date.gettime ();        long s2 =  System.currenttimemillis ();        return s1 - s2;     }    /**  return Format:2015-07-08 */     Public static string gettodaydate ()  {         Simpledateformat sdf = new simpledatefoRmat ("Yyyy-mm-dd",  locale.getdefault ());         string now  = sdf.format (Long.valueof (System.currenttimemillis ()));         return now;    }    /**  return Format: 2015-07-08 08:00:00  */    public static string gettodaytime ()  {         simpledateformat sdf = new simpledateformat ("Yyyy-MM-dd  hh:mm:ss ",  locale.getdefault ());         string now  = sdf.format (Long.valueof (System.currenttimemillis ()));         return now;    }    /**  return Format:2015-07-08 */     public static string gettomorrowdate ()  {         simpledateformAt sdf = new simpledateformat ("Yyyy-mm-dd",  locale.getdefault ());         String date = sdf                 .format (Long.valueof (System.currenttimemillis ()  + 1  * 24 * 60 * 60 * 1000));         return date;    }    /**  return Format: 2015-07-08 00:00:00  */    public static string gettomorrowtime ()  {         simpledateformat sdf = new simpledateformat ("Yyyy-MM-dd",  locale.getdefault ());        string now =  Sdf.format (Long.valueof (System.currenttimemillis ()  + 1 * 24 * 60 * 60  * 1000));         long zerohourmillis = 0;         try {             Zerohourmillis = getmillis (now +  " "  +  "00:00:00");         } catch  (parseexception e)  {             e.printstacktrace ();         }         sdf = new simpledateformat ("Yyyy-MM-dd HH: Mm:ss ",  locale.getdefault ());         string time =  sdf.format (long.valueof (zerohourmillis));        return  time;    }    /**  the size of the comparison date,  */    public  static int comparedaTe (string date1, string date2)  {         Simpledateformat sdf = new simpledateformat ("Yyyy-mm-dd",  locale.getdefault ());         Date d1 = null;         Date d2 = null;        try {             d1 = sdf.parse (date1);             d2 = sdf.parse (Date2);         } catch  (parseexception e)  {             e.printstacktrace ();             return -1;        }         reTurn d1.compareto (D2);    }    /**  Compare date to current date size,  */     public static int comparedate (STRING DATESTR)  {         simpledateformat sdf = new simpledateformat (" Yyyy-mm-dd ",  locale.getdefault ());         string now =  sdf.format (Long.valueof (System.currenttimemillis ()));         date date = null;        date nowdate =  null;        try {             date = sdf.parse (DATESTR);             nowdate = sdf.parse (now);         } catch  (parseexception e)  {            e.printstacktrace ();             return -1;         }        return nowdate.compareto ( date);     }}


Java Time class Simple integration

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.