java星座、年龄、日期等

星座:

public static String getStar(Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        String star = "";
        if (month == 1 && day >= 20 || month == 2 && day <= 18)
        {
            star = "水瓶座";
        }
        if (month == 2 && day >= 19 || month == 3 && day <= 20)
        {
            star = "双鱼座";
        }
        if (month == 3 && day >= 21 || month == 4 && day <= 19)
        {
            star = "白羊座";
        }
        if (month == 4 && day >= 20 || month == 5 && day <= 20)
        {
            star = "金牛座";
        }
        if (month == 5 && day >= 21 || month == 6 && day <= 21)
        {
            star = "双子座";
        }
        if (month == 6 && day >= 22 || month == 7 && day <= 22)
        {
            star = "巨蟹座";
        }
        if (month == 7 && day >= 23 || month == 8 && day <= 22)
        {
            star = "狮子座";
        }
        if (month == 8 && day >= 23 || month == 9 && day <= 22)
        {
            star = "处女座";
        }
        if (month == 9 && day >= 23 || month == 10 && day <= 22)
        {
            star = "天秤座";
        }
        if (month == 10 && day >= 23 || month == 11 && day <= 21)
        {
            star = "天蝎座";
        }
        if (month == 11 && day >= 22 || month == 12 && day <= 21)
        {
            star = "射手座";
        }
        if (month == 12 && day >= 22 || month == 1 && day <= 19)
        {
            star = "摩羯座";
        }
        return star;
    }
View Code

年龄:

public static int getAge(Date birthday)
    {
        Date date = new Date();
        long day = (date.getTime() - birthday.getTime()) / (24 * 60 * 60 * 1000) + 1;
        int age = (int)(day / 365);
        if(age<=0){
            age=1;
        }
        return age;
    }
View Code

判断时间是否是现在之前:

public static boolean isTimeBefore(Date date)
    {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(date);
        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(new Date());
        if (calDateA.get(Calendar.HOUR) > calDateB.get(Calendar.HOUR))
            return true;
        if (calDateA.get(Calendar.HOUR) == calDateB.get(Calendar.HOUR))
        {
            if (calDateA.get(Calendar.MINUTE) > calDateB.get(Calendar.MINUTE))
                return true;
            if (calDateA.get(Calendar.MINUTE) == calDateB.get(Calendar.MINUTE))
                if (calDateA.get(Calendar.SECOND) > calDateB.get(Calendar.SECOND))
                    return true;
            if (calDateA.get(Calendar.SECOND) == calDateB.get(Calendar.SECOND))
                if (calDateA.get(Calendar.MILLISECOND) > calDateB.get(Calendar.MILLISECOND))
                    return true;
        }
        return false;
    }
View Code

是否是今天:

public static boolean isToday(Date timestamp)
    {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(timestamp);
        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(new Date());
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR) && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
            && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB.get(Calendar.DAY_OF_MONTH);
    }
View Code

是否是这个月:

public static boolean isThisMonth(Date d1)
    {
        Date d2 = new Date();
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(d1);
        cal2.setTime(d2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (subYear == 0)
        {
            if (cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH))
                return true;
        }
        return false;
    }
View Code

是否是这周:

public static boolean isThisWeek(Date d1)
    {
        Date d2 = new Date();
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(d1);
        cal2.setTime(d2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (subYear == 0)
        {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        }
        else if (subYear == 1 && cal2.get(Calendar.MONTH) == 11)
        {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        }
        else if (subYear == -1 && cal1.get(Calendar.MONTH) == 11)
        {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        }
        return false;
    }
View Code

计算时间过去多久:

public static String getFormDate(Date date)
    {
        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(new Date());
        Calendar dateCal = Calendar.getInstance();
        dateCal.setTime(date);
        if (nowCal.get(Calendar.YEAR) != dateCal.get(Calendar.YEAR))
        {
            SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd");
            return df.format(date);
        }
        int nowDay = nowCal.get(Calendar.DAY_OF_YEAR);
        int dateDay = dateCal.get(Calendar.DAY_OF_YEAR);
        if (nowDay != dateDay)
        {
            int days = nowDay - dateDay;
            String str = null;
            switch (days)
            {
                case 1:
                    str = "昨天";
                    break;
                case 2:
                    str = "前天";
                    break;
                case 3:
                    str = "3天前";
                    break;
                case 4:
                    str = "4天前";
                    break;
                case 5:
                    str = "5天前";
                    break;
                default:
                    SimpleDateFormat df = new SimpleDateFormat("MM-dd");
                    str = df.format(date);
                    break;
            }
            return str;
        }
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        long l = new Date().getTime() - date.getTime();
        int hour = new Long(l / (60 * 60 * 1000)).intValue();
        String str="";
        switch (hour)
        {
            case 0:
                long min = ((l / (60 * 1000)));
                if(min>5)
                    str=min+"分钟前";
                else
                    str="刚刚";
                break;
            case 1:
                str="1小时前";
                break;
            case 2:
                str="2小时前";
                break;
            case 3:
                str="3小时前";
                break;
            case 4:
                str="4小时前";
                break;
            case 5:
                str="5小时前";
                break;
            default:
                str=df.format(date);
                break;
        }
        return str;
    }
View Code

 

posted @ 2016-01-04 15:13  欧文_Owen  阅读(439)  评论(0编辑  收藏  举报