「java计算周岁」年龄怎么计算周岁

博主:adminadmin 2022-11-29 03:17:06 71

今天给各位分享java计算周岁的知识,其中也会对年龄怎么计算周岁进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java输入身份证(18位),输出年龄。

public static void main(String[] sg0) {

String s = "350424198705062023";

int leh = s.length();

System.out.println(s.length());

if (leh != 18 leh != 15) {

System.out.println("NO");

}

else {

if (leh == 18) {

int se = Integer.valueOf(s.substring(leh - 1)) % 2;

String dates = s.substring(6, 10) + "-" + s.substring(10, 12) + "-" + s.substring(12, 14);

System.out.println(dates);

String sex = "";

if (leh == 0) {

sex = "M";

}

else {

sex = "F";

}

System.out.println(sex + "\t" + dates);

}

else {

String dates = "19" + s.substring(6, 8) + "-" + s.substring(8, 10) + "-" + s.substring(10, 12);

System.out.println(dates);

}

}

}

用java写用户在控制台按照“yyyy/mm/dd”的格式输入出生日期,请计算用户的年龄

年龄就是把当前的年份与用户的年份相减得到一个对象值1。然后将用户输入日期中的年份换成当年的,组成一个新的日期,将这新的日期与当天的日期进行比较,得到另一个对象值2。这个对象值2就是距离用户的生日的天数。这天数是正,那对象值1就是用户的年龄,是负把对象值+1就好。参考两日期之间的天数差方法:

public static int getDiffDay(String firstString, String secondString) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

Date firstDate = null;

Date secondDate = null;

try {

firstDate = df.parse(firstString);

secondDate = df.parse(secondString);

} catch (Exception e) {

// 日期型字符串格式错误

}

int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));

return nDay;

}

java计算年龄

import java.util.Calendar;

import java.util.Date;

import java.awt.*;

import java.awt.event.*;

import java.text.NumberFormat;

public class H {

public static void main(String args[]) {

new Time("年龄计算器");

}

}

class Time extends Frame implements ActionListener {

Calendar calendar;

Button button;

TextField t1, t2, t3;

Label l, l1, l2, l3;

Time(String s) {

super(s);

setLayout(new FlowLayout());

button = new Button("确定");

button.addActionListener(this);

t1 = new TextField(2);

t2 = new TextField(2);

t3 = new TextField(2);

l = new Label(" 请输入您的生日 ");

l.setBackground(Color.cyan);

l1 = new Label("年");

l2 = new Label("月");

l3 = new Label("日");

add(l);

add(t1);

add(l1);

add(t2);

add(l2);

add(t3);

add(l3);

add(button);

setBounds(100, 100, 280, 100);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

setVisible(true);

validate();

}

public void actionPerformed(ActionEvent e) {

calendar = Calendar.getInstance();

calendar.setTime(new Date());

NumberFormat f = NumberFormat.getInstance();

long time = calendar.getTimeInMillis();

if (e.getSource() == button) {

try {

int n = Integer.parseInt(t1.getText());

int y = Integer.parseInt(t2.getText());

int r = Integer.parseInt(t3.getText());

calendar.set(n, y - 1, r);

double time1 = calendar.getTimeInMillis();

double c = (time - time1) / (1000 * 60 * 60 * 24);

double d = c/365;

f.setMaximumFractionDigits(2);

String s = f.format(d);

l.setText("您的年龄约为" + s + " 岁");

} catch (NumberFormatException ee) {

l.setText("请正确输入");

}

}

}

}

功底浅薄,如果有问题,还望指教。

用Java编写一个程序,要求如下:

1.内部使用 C 的 longjmp 机制让出一个协程。 因此,如果一个 C 函数 foo 调用了一个 API 函数, 而这个 API 函数让出了(直接或间接调用了让出函数)。 由于 longjmp 会移除 C 栈的栈帧, Lua 就无法返回到 foo 里了。

2.为了回避这类问题, 碰到 API 调用中调用让出时,除了那些抛出错误的 API 外,还提供了三个函数: lua_yieldk, lua_callk,和 lua_pcallk 。 它们在让出发生时,可以从传入的 延续函数 (名为 k 的参数)继续运行。

3.我们需要预设一些术语来解释延续点。 对于从 Lua 中调用的 C 函数,我们称之为 原函数。 从这个原函数中调用的上面所述的三个 C API 函数我们称之为 被调函数。 被调函数可以使当前线程让出。 (让出发生在被调函数是 lua_yieldk, 或传入 lua_callk 或 lua_pcallk 的函数调用了让出时。)

4.假设正在运行的线程在执行被调函数时让出。 当再次延续这条线程,它希望继续被调函数的运行。 然而,被调函数不可能返回到原函数中。 这是因为之前的让出操作破坏了 C 栈的栈帧。 作为替代品,Lua 调用那个作为被调函数参数给出的 延续函数 。 正如其名,延续函数将延续原函数的任务。

5.注意这里那个额外的显式的对延续函数的调用:Lua 仅在需要时,这可能是由错误导致的也可能是发生了让出而需要继续运行,才会调用延续函数。 如果没有发生过任何让出,调用的函数正常返回, 那么 lua_pcallk (以及 lua_callk)也会正常返回。 (当然,这个例子中你也可以不在之后调用延续函数, 而是在原函数的调用后直接写上需要做的工作。)

6.Lua 会把延续函数看作原函数。 延续函数将接收到和原函数相同的 Lua 栈,其接收到的 lua 状态也和 被调函数若返回后应该有的状态一致。 (例如, lua_callk 调用之后, 栈中之前压入的函数和调用参数都被调用产生的返回值所替代。) 这时也有相同的上值。 等到它返回的时候,Lua 会将其看待成原函数的返回去操作。

7.我们需要预设一些术语来解释延续点。 对于从 Lua 中调用的 C 函数,我们称之为 原函数。 从这个原函数中调用的上面所述的三个 C API 函数我们称之为 被调函数。 被调函数可以使当前线程让出。 (让出发生在被调函数是 lua_yieldk, 或传入 lua_callk 或 lua_pcallk 的函数调用了让出时。)

8.假设正在运行的线程在执行被调函数时让出。 当再次延续这条线程,它希望继续被调函数的运行。 然而,被调函数不可能返回到原函数中。 这是因为之前的让出操作破坏了 C 栈的栈帧。 作为替代品,Lua 调用那个作为被调函数参数给出的 延续函数 。 正如其名,延续函数将延续原函数的任务。

希望能帮到你,谢谢!

用java实现年龄计算器,就是在界面上输入一个人出生年月日(yyyy-mm-dd),显示现在多少岁,假设一年有365天

这是一段javascript写的,无论是java或javascript原理都一样,进攻参考

!-- Begin

function run() {

with (document.agecalc) {

dd = parseInt(day.selectedIndex) + 1;

mm = parseInt(month.selectedIndex) + 1;

yy = year.value;

if (yy.length != 4 || isNaN(yy)) {

document.agecalc.timealive.value = "请输入4位数的年份.";

document.agecalc.year.select();

document.agecalc.year.focus();

return;

}

}

days = new Date();

gdate = days.getDate();

gmonth = days.getMonth();

gyear = days.getYear();

if (gyear 2000) gyear += 1900;

age = gyear - yy;

if ((mm == (gmonth + 1)) (dd = parseInt(gdate))) {

age = age;

} else {

if (mm = (gmonth)) {

age = age;

} else {

age = age - 1;

}

}

if (age == 0)

age = age;

document.agecalc.timealive.value = "你已经满了 " + age+ " 周岁 . . .\n\n";

if (mm = (gmonth + 1))

age = age - 1;

if ((mm == (gmonth + 1)) (dd parseInt(gdate)))

age = age + 1;

var m;

var n;

if (mm == 12) n = 31 - dd;

if (mm == 11) n = 61 - dd;

if (mm == 10) n = 92 - dd;

if (mm == 9) n = 122 - dd;

if (mm == 8) n = 153 - dd;

if (mm == 7) n = 184 - dd;

if (mm == 6) n = 214 - dd;

if (mm == 5) n = 245 - dd;

if (mm == 4) n = 275 - dd;

if (mm == 3) n = 306 - dd;

if (mm == 2) { n = 334 - dd; if (leapyear(yy)) n++; }

if (mm == 1) { n = 365 - dd; if (leapyear(yy)) n++; }

if (gmonth == 1) m = 31;

if (gmonth == 2) {

m = 59;

if (leapyear(gyear)) m++;

}

if (gmonth == 3) { m = 90; if (leapyear(gyear)) m++; }

if (gmonth == 4) { m = 120; if (leapyear(gyear)) m++; }

if (gmonth == 5) { m = 151; if (leapyear(gyear)) m++; }

if (gmonth == 6) { m = 181; if (leapyear(gyear)) m++; }

if (gmonth == 7) { m = 212; if (leapyear(gyear)) m++; }

if (gmonth == 8) { m = 243; if (leapyear(gyear)) m++; }

if (gmonth == 9) { m = 273; if (leapyear(gyear)) m++; }

if (gmonth == 10) { m = 304; if (leapyear(gyear)) m++; }

if (gmonth == 11) { m = 334; if (leapyear(gyear)) m++; }

if (gmonth == 12) { m = 365; if (leapyear(gyear)) m++; }

months = age * 12;

months += 12 - parseInt(mm);

months += gmonth;

totdays = (parseInt(age) * 365);

totdays += age / 4;

totdays = parseInt(totdays) + gdate + m + n;

if (gmonth == 1) p = 31 + gdate;

if (gmonth == 2) {

p = 59 + gdate;

if (leapyear(gyear)) m = m+1;

}

if (gmonth == 3) { p = 90 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 4) { p = 120 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 5) { p = 151 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 6) { p = 181 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 7) { p = 212 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 8) { p = 243 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 9) { p = 273 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 10) { p = 304 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 11) { p = 334 + gdate; if (leapyear(gyear)) p++; }

if (gmonth == 12) { p = 365 + gdate; if (leapyear(gyear)) p++; }

weeks = (age * 365) + n + p;

weeks = weeks / 7;

etcdays = parseFloat(weeks) - parseInt(weeks);

etcdays = Math.round(etcdays * 7);

weeks = parseInt(weeks);

etcdays += parseInt(age / 4);

if (etcdays 7)

weeks += parseInt(etcdays / 7);

document.agecalc.timealive.value += " 或 " + months + " 月龄\n";

document.agecalc.timealive.value += " 或 " + weeks + " 周龄\n";

document.agecalc.timealive.value += " 或 " + totdays + " 日龄\n";

var time = new Date();

ghour = time.getHours();

gmin = time.getMinutes();

gsec = time.getSeconds();

hour = ((age * 365) + n + p) * 24;

hour += (parseInt(age / 4) * 24);

document.agecalc.timealive.value += " 或 " + hour + " 小时\n";

var min = (hour * 60) + gmin;

document.agecalc.timealive.value += " 或 " + min + " 分钟\n";

sec = (min * 60) + gsec;

document.agecalc.timealive.value += " 或 " + sec + " 秒钟";

mm = mm - 1;

var r;

if (mm == 0) r = 0;

if (mm == 1) r = 31;

if (mm == 2) { r = 59; if (leapyear(gyear)) m++; }

if (mm == 3) { r = 90; if (leapyear(gyear)) r++; }

if (mm == 4) { r = 120; if (leapyear(gyear)) r++; }

if (mm == 5) { r = 151; if (leapyear(gyear)) r++; }

if (mm == 6) { r = 181; if (leapyear(gyear)) r++; }

if (mm == 7) { r = 212; if (leapyear(gyear)) r++; }

if (mm == 8) { r = 243; if (leapyear(gyear)) r++; }

if (mm == 9) { r = 273; if (leapyear(gyear)) r++; }

if (mm == 10) { r = 304; if (leapyear(gyear)) r++; }

if (mm == 11) { r = 334; if (leapyear(gyear)) r++; }

mm = mm + 1;

r = parseInt(r) + parseInt(dd);

if ((mm = (gmonth + 1)) (dd gdate)) {

bday = r - m - gdate;

}

else {

if ((leapyear(gyear)) ((mm 2) (dd 29))) {

a = 366;

} else {

a = 365;

}

bday = a + (r - m - gdate);

}

nhour = 24 - parseInt(ghour);

nmin = 60 - parseInt(gmin);

nsec = 60 - parseInt(gsec);

while (bday 366) bday -= 365;

if (((bday == 366) (leapyear(gyear)) || ((bday == 365) (!leapyear(gyear))))) {

document.agecalc.timealive.value += "\n\nSenlon祝贺您!今天是你的生日!祝你生日快乐!";

} else {

document.agecalc.timealive.value += "\n\n另外,你下一个生日距今还有:\n"

+ bday + " 天 " + nhour + " 小时 " + nmin + " 分 " + nsec + " 秒";

setTimeout("run()", 1000);

}

}

function leapyear(a) {

if (((a%4 == 0) (a%100 != 0)) || (a%400 == 0))

return true;

else

return false;

}

// End --

来自

关于java计算周岁和年龄怎么计算周岁的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-29,除非注明,否则均为首码项目网原创文章,转载请注明出处。