工具类

常用工具类的收集

    /** 
     * 根据出生日期计算年龄
     *  
     * @param strBirthDate(yyyy-mm-dd or yyyy/mm/dd) 
     * @return  
     */  
    public static String getPersonAgeByBirthDate(Date dateBirthDate){  
        if (dateBirthDate == null){  
            return "";  
        }  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
        String strBirthDate=dateFormat.format(dateBirthDate);  

        //读取当前日期  
        Calendar c = Calendar.getInstance();  
        int year = c.get(Calendar.YEAR);  
        int month = c.get(Calendar.MONTH)+1;  
        int day = c.get(Calendar.DATE);  
        //计算年龄  
        int age = year - Integer.parseInt(strBirthDate.substring(0, 4)) - 1;  
        if (Integer.parseInt(strBirthDate.substring(5,7)) < month) {  
            age++;  
        } else if (Integer.parseInt(strBirthDate.substring(5,7))== month && Integer.parseInt(strBirthDate.substring(8,10)) <= day){  
            age++;  
        }  
        return String.valueOf(age);  
    }  
        private static final long ONE_MINUTE = 60;
        private static final long ONE_HOUR = 3600;
        private static final long ONE_DAY = 86400;
        private static final long ONE_MONTH = 2592000;
        private static final long ONE_YEAR = 31104000;

        public static Calendar calendar = Calendar.getInstance();
    /**
     * 距离现在的时间多久
     * @param date
     * @return
     *
     */
    public static String fromToday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        long time = date.getTime() / 1000;
        long now = new Date().getTime() / 1000;
        long ago = now - time;
        if (ago <= ONE_HOUR)
            return ago / ONE_MINUTE + "分钟前";
        else if (ago <= ONE_DAY)
            return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE)
                    + "分钟前";
        else if (ago <= ONE_DAY * 2)
            return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        else if (ago <= ONE_DAY * 3)
            return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        else if (ago <= ONE_MONTH) {
            long day = ago / ONE_DAY;
            return day + "天前" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        } else if (ago <= ONE_YEAR) {
            long month = ago / ONE_MONTH;
            long day = ago % ONE_MONTH / ONE_DAY;
            return month + "个月" + day + "天前"
                    + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        } else {
            long year = ago / ONE_YEAR;
            int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so month+1
            return year + "年前" + month + "月" + calendar.get(Calendar.DATE)
                    + "日";
        }

    }
/**
 * 根据经纬度计算距离(单位为KM)
 * @author zkd
 *
 */
public class DistanceUtil {

    private static final double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static double GetDistance(double lngme, double latme, double lngyou, double latyou) {
        double a, b, d, sa2, sb2;
        latme = rad(latme);
        latyou = rad(latyou);
        a = latme - latyou;
        b = rad(lngme - lngyou);

        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2 * EARTH_RADIUS * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(latme) * Math.cos(latyou) * sb2 * sb2));
        return d;
    }
/**
 * 查询周日期的工具类(传参为1表示上周,2为上上周 以此内推)
* @ClassName: LastWeekUtil 
* @Description: 
* @author zkd
* @date 2016年2月18日 下午5:19:59 
*
 */
public class LastWeekUtil {

    public static void main(String[] args) {
        List<String> list = calculateDate(-1);
        for (String string : list) {
            System.out.println(string);
        }
    }


    public static List<String> calculateDate(int index){
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, -index);
        for (int i = 0; i < 7; i++) {
            cal.add(Calendar.DATE, -1 * cal.get(Calendar.DAY_OF_WEEK) + 2 + i);
            dateList.add(sf.format(cal.getTime()));
        }
        return dateList;
    }
}
    /**
     * 判断是否为空的方法
     * 
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        return obj == null || "null".equals(obj) || "undefined".equals(obj)
                || "".equals(obj.toString().trim());
    }
    /**
     * 
     * @Title: getConstellation
     * @Description: 根据生日计算出星座
     * 
     */
    public static String getConstellation(Object birthday) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(birthday);
        int month = Integer.parseInt(format.substring(4, 6));
        int day = Integer.parseInt(format.substring(6, 8));
        int[] DAYARR = new int[] { 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22 };
        String[] CONSTELLATIONARR = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座",
                "天蝎座", "射手座", "摩羯座" };
        return day < DAYARR[month - 1] ? CONSTELLATIONARR[month - 1] : CONSTELLATIONARR[month];
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值