Tools: handle the conversion date

     Daily work, we often used to format conversion between string and date, calculation date between dates. In order to focus the project itself and not overly concerned about the other minutiae, he wrote the date in the development of a tool class might be used. This tool contains the main class "will convert strings to Date date date", "Convert Date date string", "Get daily date within the date range segment," "difference in time stamps of two dates "and so these methods.

 

java.text.ParseException Import; 
Import the java.text.SimpleDateFormat; 
Import of java.util.ArrayList; 
Import the java.util.Calendar; 
Import java.util.Date; 
Import java.util.List; 

public class DateTimeUtils 
{ 
    
    / ** 
     * the date string into a date object 
     * 
     * @param dateStr date string, such as "2018-10-08" 
     * @param dateFormate date formatted string, such as "the MM-dd-YYYY" 
     * @return date object 
     * @ See [class, method #, members of category #] 
     * @Description: the TODO 
     * / 
    public static parseDate a Date (dateText String, String dateFormate) 
    { 
        IF (. dateText dateText.trim == null || () isEmpty ()) 
            return null;
        
        = New new SDF the SimpleDateFormat the SimpleDateFormat (dateFormate); 
        a Date DATE = null; 
        the try 
        { 
            DATE = sdf.parse (dateText); 
        } 
        the catch (a ParseException E) 
        { 
          e.printStackTrace (); 
        } 
        return DATE; 
    } 
    
    / ** 
     * get a date daily collection date interval 
     * 
     * @param startDate start date 
     * @param endDate end date 
     * @return date set 
     * @see [class, method #, members of category #] 
     * / 
    public static List <a date> getDatePeriodList (a date startDate, a date endDate) 
    {  
        List <a Date> = new new listDate the ArrayList <a Date> ();
        // start date the date the list was added
        listDate.add (startDate); 
        // cycle calendar object will be used between the start and end dates 
        Calendar calCurrentDate Calendar.getInstance = (); 
        // set the initial value as a starting date 
        calCurrentDate.setTime (startDate); 
        the while ( endDate.after (calCurrentDate.getTime ())) 
        { 
            // each cycle, adding to the propulsion calendar object calCurrentDate one day, and converted to the Date object list listDate 
            calCurrentDate.add (Calendar.DAY_OF_MONTH, 1) ; 
            listDate.add (calCurrentDate.getTime ()); 
        } 
        return listDate; 
    } 
    
    / ** 
     * get a date string section daily collection date 
     * <br> 
     * as 2018-10-01,2018-10-02 , ... 2018-10-21  
     *
     * @param startDate start date 
     * @param endDate end date 
     * @param dateFormat date formatted string, such as "the MM-dd-YYYY" 
     * @return string set date 
     * @see [Class, Method #, class # members] 
     * @Description: the TODO 
     * / 
    public static list <String> datePeriodText (a date startDate, a date endDate, the dateFormat String) 
    { 
        
        list <String> = new new dateStrs the ArrayList <> (); 
        // start date to the date the list was added 
        dateToTextFormat startDateStr = String (startDate, the dateFormat); 
        dateStrs.add (startDateStr); 
        // cycle calendar object will be used between the start and end dates 
        calendar calCurrentDate Calendar.getInstance = (); 
        // set the initial value as a starting date 
        calCurrentDate.setTime (startDate); 
        while (endDate.after ( calCurrentDate.getTime ())) 
        {
            // each cycle, to the propulsion calendar object calCurrentDate one day, and the Date object converted listDate added to the list 
            calCurrentDate.add (Calendar.DAY_OF_MONTH,. 1); 
            String curDateStr = dateToTextFormat (calCurrentDate.getTime (), the dateFormat); 
            dateStrs.add (curDateStr); 
        } 
        return dateStrs; 
        
    } 
    
    / ** 
     * convert the date string object 
     * 
     * @param dATE date 
     * @param dateFormate date formatted string, such as "yyyy-MM-dd " 
     * @return date string format 
     * @see [class, method #, members of category #] 
     * @Description: the TODO 
     * / 
    / ** 
    public static string dateToTextFormat (a date dATE , String dateFormat)
    { 
        
        Return new new the SimpleDateFormat (the dateFormat) .format (DATE); 
    } 
    
     * Date format dateA compare two strings, the chronological order dateB 
     * <P> 
     * return value indicates a time after dateB dateA, the return value - 1 represents the time before the dateB dateA, returned value indicates dateA, dateB same time, 
     * 
     * @param dateA a date, such as "08:10" 
     * @param dateB date 2. "08:21" 
     * @param dateFormate date format string, such as "hh: mm" (12 hour), "HH: mm" ( 24 hour) 
     * @return -1,0,1 has three return value 
     * @see [class, method #, members of category #] 
     * @Description: the TODO 
     * / 
    public static int the compareTo (String dateA, dateB String, String dateFormate) 
    { 
        a Date date1 = parseDate (dateA, dateFormate); 
        a Date DATE2 = parseDate (dateB,dateFormate);
        // date1 After DATE2  
        IF (date1.getTime ()> date2.getTime ()) 
        { 
            return. 1;
        }
        // date1 before DATE2 
        the else IF (date1.getTime () <date2.getTime ()) 
        { 
            return -1; 
        } 
        // date1, DATE2 same time 
        the else 
        { 
            return 0; 
        } 
    } 
    
    / ** 
     * two strings is calculated date format dateA, the difference in time stamps dateB 
     * 
     * @param dateA date a, such as "08:10", "2018-03-10 12:12" 
     * @param dateB date B, such as "08:21", "2018-03-15 14:12" 
     * @param dateFormate date formatted string, such as "hh: mm" (12 hour), "HH: mm" ( 24 hour), " the mM-dd-HH YYYY: mm " 
     * @return milliseconds difference between the two dates 
     * @see [class, method #, members of category #] 
     * /
    Long the diff static public (String dateA, dateB String, String dateFormate) { 
        a Date date1 = parseDate (dateA, dateFormate); 
        a Date DATE2 = parseDate (dateB, dateFormate); 
      
        return date1.getTime () - date2.getTime (); 
    } 
    
    / ** 
     * number of hours between two dates 
     * @param newDate 
     * @param olddate 
     * @return 
     * @see [class, method #, members of category #] 
     * / 
    public static Long calcTimeDiffOnHour (a date newDate, a date olddate) { 
        IF (newDate == null || olddate == null) { 
            the throw new new an IllegalArgumentException ( "parameter newDate, oldDate not empty"); 
        } 
       
       Long diffMillis = newDate.getTime () - oldDate.getTime (); 
       return diffMillis / (1000 * 60 * 60); 
    } 
    
    
    
    public static void main (String [] args) 
    {
        long t=calcTimeDiffOnHour(new Date(),new Date(119,7,24));
        
        System.out.println(t);
        System.out.println(new Date(119,7,24)+"  "+new Date());
    }
}

  

 

Guess you like

Origin www.cnblogs.com/gocode/p/handler-of-date2text.html