输入出生日期计算年龄小程序

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

public class CalcAge {
    public static void main(String[] args) {
        /**
         * 计算年龄 根据输入出生日期计算
         */
        Scanner sc = new Scanner(System.in);
        // yyyy-MM-dd
        String birthday = sc.next();
        SimpleDateFormat sdf = new SimpleDateFormat("y-MM-d");
        try {
            Date date = sdf.parse(birthday);
            long time = System.currentTimeMillis() - date.getTime();
            // time / 1000 / 3600 / 24 / 365 = 年
            System.out.println(time / 1000 / 3600 / 24 / 365 + "岁");
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过微信小程序的API实现身份证号的解析,获取出生日期年龄、性别等信息。以下是一个简单的实现过程: 1. 在wxml文件中定义一个输入框: ``` <view class="input-wrapper"> <input type="number" placeholder="请输入身份证号" bindinput="onInputIdCard" /> </view> ``` 2. 在js文件中实现onInputIdCard方法,获取输入的身份证号码: ``` onInputIdCard(e) { const idCard = e.detail.value // TODO: 对身份证号进行校验 } ``` 3. 对输入的身份证号进行校验,确保其合法性。 4. 调用微信小程序提供的API wx.getStorageSync,获取身份证号中的出生日期年龄、性别等信息: ``` const idCardInfo = getIdCardInfo(idCard) console.log(idCardInfo) function getIdCardInfo(idCard) { const year = idCard.substring(6, 10) const month = idCard.substring(10, 12) const day = idCard.substring(12, 14) const sex = idCard.substring(16, 17) % 2 === 0 ? '女' : '男' const birthday = year + '-' + month + '-' + day const age = getAge(year, month, day) return { birthday, age, sex } } function getAge(year, month, day) { const now = new Date() const currentYear = now.getFullYear() const currentMonth = now.getMonth() + 1 const currentDay = now.getDate() let age = currentYear - year if (currentMonth < month || (currentMonth === month && currentDay < day)) { age-- } return age } ``` 解析身份证号码的逻辑比较简单,主要是通过字符串截取的方式获取出生日期、性别等信息。年龄计算需要根据当前时间和出生日期计算得出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值