10 友盟项目--日期工具类、时间对齐、地理类库Geolite

1.日期工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
* 日期工具类
*/
public class DateUtil {

   private static final String PATTERN_US_DATE  = "dd/MMM/yyyy:HH:mm:ss Z" ;

   /**
    * 默认使用的解析方式
    */
   public static long parseDateStr(String dateStr) throws ParseException {
      return parseDateStr(dateStr , "yyyy/MM/dd HH:mm:ss" , Locale.CHINA ) ;
   }
   /**
    * 默认使用的解析方式
    */
   public static long parseDateStrInUS(String dateStr) throws ParseException {
      return parseDateStr(dateStr , PATTERN_US_DATE , Locale.US ) ;
   }

   /**
    * 解析时间串,生成时间戳
    */
   public static long parseDateStr(String dateStr , String fmt , Locale locale) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat(fmt , locale) ;
      return sdf.parse(dateStr).getTime() ;
   }

   /**
    * 使用默认的格式(yyyy/MM/dd HH:mm:ss)格式化时间对象
    */
   public static String formatDate(Date date){
      return formatDate(date , "yyyy/MM/dd HH:mm:ss") ;
   }

   /**
    * 使用指定的格式格式时间对象
    */
   public static String formatDate(Date date , String fmt){
      SimpleDateFormat sdf = new SimpleDateFormat(fmt) ;
      return sdf.format(date) ;
   }

   /**
    * 使用指定的格式格式时间戳对象
    */
   public static String formatDate(long timestamp , String fmt){
      return formatDate(new Date(timestamp) ,fmt) ;
   }

   /**
    * 使用时间戳格式化默认的时间串
    */
   public static String formatDate(long timestamp){
      return formatDate(new Date(timestamp)) ;
   }
}
Date工具类

测试日期工具类

public class TestDateFormat {
    /**
     * Locale :地域信息 ,国际化
     */
    @Test
    public void test1() throws Exception {
        Date now = new Date() ;
        SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z" ,Locale.US) ;
        //System.out.println(df.format(now)) ;

        String str = "28/Oct/2018:20:43:46 -0700" ;
        Date thatDate = df.parse(str) ;
        //1540784626000
        System.out.println(thatDate.getTime());

        Date newDate = new Date(1540784626000L)  ;
        System.out.println(df.format(newDate));
    }

    @Test
    public void test2() throws Exception {
        String str = "28/Oct/2018:20:43:46 -0700" ;
        long ts = DateUtil.parseDateStrInUS(str) ;
        System.out.println(ts);
        String dateStr = DateUtil.formatDate(ts)  ;
        System.out.println(dateStr);
        System.out.println(DateUtil.parseDateStr(dateStr)) ;

    }
}

2.地理类库Geolite

猜你喜欢

转载自www.cnblogs.com/star521/p/9894106.html