Android时间转换 秒转为年月日时间

参考文章

    Date day=new Date();//获取现在都时间
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定时时间格式
                try {
                    String now=df.format(day);//现在都时间经过时间格式的刷新后就可以获取到对应的时间了,这个是字符串
                    Date now1 =df.parse(now);//用parse对字符串转为Date类型,这个要try catch的

                    long second = data.birthday;//获取服务器发送过来的数据,发过来的数据是秒为时间戳
                    String dateString = secondToDate(second,"yyyy-MM-dd hh:mm:ss");//调用方法,将秒转为标准类型时间形式
                    Date birthday1=df.parse(dateString);//转为date类型
                    Log.v("age1234",dateString);

                    int age=now1.getYear()-birthday1.getYear();//直接用get方法获取年份相减获得岁数
                    Log.v("age123",String.valueOf(age));
                    Log.v("birthday123",String.valueOf(birthday1));
                    Log.v("birthday123",String.valueOf(birthday1.getDay()));
                    //下面就是判断了
                    if(birthday1.getMonth()==2&&birthday1.getDay()>=21||birthday1.getMonth()==3&&birthday1.getDay()<=19){
                        tv_age.setText(age+"岁"+" "+"白羊座");
                    }
                    if(birthday1.getMonth()==3&&birthday1.getDay()>=20||birthday1.getMonth()==4&&birthday1.getDay()<=20){
                        tv_age.setText(age+"岁"+" "+"金牛座");
                    }
                    if(birthday1.getMonth()==4&&birthday1.getDay()>=21||birthday1.getMonth()==5&&birthday1.getDay()<=21){
                        tv_age.setText(age+"岁"+" "+"双子座");
                    }
                    if(birthday1.getMonth()==5&&birthday1.getDay()>=22||birthday1.getMonth()==6&&birthday1.getDay()<=22){
                        tv_age.setText(age+"岁"+" "+"巨蟹座");
                    }
                    if(birthday1.getMonth()==6&&birthday1.getDay()>=23||birthday1.getMonth()==7&&birthday1.getDay()<=22){
                        tv_age.setText(age+"岁"+" "+"狮子座");
                    }
                    if(birthday1.getMonth()==7&&birthday1.getDay()>=23||birthday1.getMonth()==8&&birthday1.getDay()<=22){
                        tv_age.setText(age+"岁"+" "+"处女座");
                    }
                    if(birthday1.getMonth()==8&&birthday1.getDay()>=23||birthday1.getMonth()==9&&birthday1.getDay()<=22){
                        tv_age.setText(age+"岁"+" "+"天秤座");
                    }
                    if(birthday1.getMonth()==9&&birthday1.getDay()>=24||birthday1.getMonth()==10&&birthday1.getDay()<=22){
                        tv_age.setText(age+"岁"+" "+"天蝎座");
                    }
                    if(birthday1.getMonth()==10&&birthday1.getDay()>=23||birthday1.getMonth()==11&&birthday1.getDay()<=21){
                        tv_age.setText(age+"岁"+" "+"射手座");
                    }
                    if(birthday1.getMonth()==11&&birthday1.getDay()>=21||birthday1.getMonth()==0&&birthday1.getDay()<=29){
                        tv_age.setText(age+"岁"+" "+"摩羯座");
                    }
                    if(birthday1.getMonth()==0&&birthday1.getDay()>=20||birthday1.getMonth()==1&&birthday1.getDay()<=18){
                        tv_age.setText(age+"岁"+" "+"水瓶座");
                    }
                    if(birthday1.getMonth()==1&&birthday1.getDay()>=19||birthday1.getMonth()==2&&birthday1.getDay()<=20){
                        tv_age.setText(age+"岁"+" "+"双鱼座");
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }

secondToDate方法

    private String secondToDate(long second,String patten) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(second * 1000);//转换为毫秒
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(patten);
        String dateString = format.format(date);
        return dateString;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值