DateUtils

To be sorted out

package com.icil.esolution.utils;
/**
 * ********************************************

* @ClassName: DateUtils 

* @Description: 

* @author  Sea

* @date 28 Aug 2018 9:51:49 PM 
*
***************************************************
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import org.springframework.util.Assert;
public class DateUtils {

    
        /**
         * @Desc Date to String  like: yyyy-MM-dd HH:mm:ss
         * @param date
         * @return
         * @throws ParseException
         */
        public static  String formatDate(Date date,String patten)throws ParseException{
             SimpleDateFormat sdf = new SimpleDateFormat(patten);
            return sdf.format(date);
        }
        /**
         * @Desc String to Date eg: yyyy-MM-dd HH:mm:ss
         * @param strDate
         * @return
         * @throws ParseException
         */
        public static Date parse(String strDate,String patten) throws ParseException{
             SimpleDateFormat sdf = new SimpleDateFormat(patten);
            return sdf.parse(strDate);
        }
    
        /**
         * Turn the string into a timestamp of type Long, in the form of yyyy-MM-dd HH:mm:ss.*/
        /**
         * @Desc: Turn the string into a timestamp of type Long.*@param timestr Long
         * @param patten : eg:yyyy-MM-dd HH:mm:ss
         * @return
         */
        public static Long convertTimeToLong(String timestr, String patten) { 
            Assert.notNull(timestr, "time is null");
            DateTimeFormatter ftf = DateTimeFormatter.ofPattern(patten);
            LocalDateTime parse = LocalDateTime.parse(timestr, ftf);
            return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }  
        
        
        /**
         * @Desc: Converts a string transition date to a Long type timestamp to a String type timestamp in a patten format of: like yyy-MM-dd HH: mm:ss*/
        public static String convertTimeToString(Long time,String patten){
            Assert.notNull(time, "time is null");
            DateTimeFormatter ftf = DateTimeFormatter.ofPattern(patten);
            return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
        }
        
        
        
    
        
       
        
}

View Code

 

Leave a Reply

Your email address will not be published. Required fields are marked *