A. 用java求出身份证上的出生年月日,年龄以及户口所在地,就是使用二维数组求的户口出了问题。。
1、不论是一维数组还是二维数组,数组的下标都是以“0”开始的,不是从1开始。
2、变量声明创建可以在类中操作,创建时可以赋初始值,但变量创建后,赋值须在方法中进行。
改成下面代码:
class IDcard{
String year = new String();
String month = new String();
String day = new String();
String province, pro = new String();
int x, age;
String[][] a = new String[34][2];
IDcard(){
setArrayValues(a); //初始化数组值
System.out.println("请输入身份证号码");
Scanner sc = new Scanner(System.in);
String s = sc.next();
year = s.substring(6, 10);
month = s.substring(10, 12);
day = s.substring(12, 14);
System.out.println("出生日期");
System.out.println(year + "年" + month + "月" + day + "日");
x = Integer.parseInt(year);
age = 2013 - x;
System.out.println("年龄:" + age);
System.out.println("户口地");
pro = s.substring(0, 2);
for (int i = 0; i < 34; i++) {
if (pro.equals(a[i][0])) { //数组中第一个元素下标从“0”开始
System.out.println(a[i][1]); //取第二个元素
break;
}
}
}
/**
* 初始化数组值
* @param a
*/ private void setArrayValues(String[][] a) {
a[0][0] = "11"; a[0][1] = "北京"; //数组中第一个元素下标从“0”开始
a[1][0] = "82"; a[1][1] = "澳门";
a[2][0] = "12"; a[2][1] = "天津";
a[3][0] = "13"; a[3][1] = "河北";
a[4][0] = "14"; a[4][1] = "山西";
a[5][0] = "15"; a[5][1] = "内蒙古";
a[6][0] = "21"; a[6][1] = "辽宁";
a[7][0] = "22"; a[7][1] = "吉林";
a[8][0] = "23"; a[8][1] = "黑龙江";
a[9][0] = "31"; a[9][1] = "上海";
a[10][0] = "32"; a[10][1] = "江苏";
a[11][0] = "33"; a[11][1] = "浙江";
a[12][0] = "34"; a[12][1] = "安徽";
a[13][0] = "35"; a[13][1] = "福建";
a[14][0] = "36"; a[14][1] = "江西";
a[15][0] = "37"; a[15][1] = "山东";
a[16][0] = "41"; a[16][1] = "河南";
a[17][0] = "42"; a[17][1] = "湖北";
a[18][0] = "43"; a[18][1] = "湖南";
a[19][0] = "44"; a[19][1] = "广东";
a[20][0] = "45"; a[20][1] = "广西";
a[21][0] = "46"; a[21][1] = "海南";
a[22][0] = "50"; a[22][1] = "重庆";
a[23][0] = "51"; a[23][1] = "四川";
a[24][0] = "52"; a[24][1] = "贵州";
a[25][0] = "53"; a[25][1] = "云南";
a[26][0] = "54"; a[26][1] = "西藏";
a[27][0] = "61"; a[27][1] = "陕西";
a[28][0] = "62"; a[28][1] = "甘肃";
a[29][0] = "63"; a[29][1] = "青海";
a[30][0] = "64"; a[30][1] = "宁夏";
a[31][0] = "65"; a[31][1] = "新疆";
a[32][0] = "71"; a[32][1] = "台湾";
a[33][0] = "81"; a[33][1] = "香港";
}
}
B. java如何获取当前时间 年月日 时分秒
//得到long类型当前时间
longl=System.currentTimeMillis();
//new日期对
Datedate=newDate(l);
//转换提日期输出格式
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-
ddHH:mm:ss");System.out.println(dateFormat.format(date));
(2)java计算年月日扩展阅读
package com.ob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
Calendar now = Calendar.getInstance();
System.out.println("年: " + now.get(Calendar.YEAR));
System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分: " + now.get(Calendar.MINUTE));
System.out.println("秒: " + now.get(Calendar.SECOND));
System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
System.out.println(now.getTime());
Date d = new Date();
System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);
System.out.println("格式化后的日期:" + dateNowStr);
String str = "2012-1-13 17:26:33";
//要跟上面sdf定义的格式一样
Date today = sdf.parse(str);
System.out.println("字符串转成日期:" + today);
}
}
C. java如何得到年月日。
1、获取当前的时间
Date date=new Date();//此时date为当前的时间
2、设置时间的格式
Date date=new Date();//此时date为当前的时间
System.out.println(date);
SimpleDateFormat dateFormat=new SimpleDateFormat(“YYYY-MM-dd”);//设置当前时间的格式,为年-月-日
System.out.println(dateFormat.format(date));
SimpleDateFormat dateFormat_min=new SimpleDateFormat(“YYYY-MM-dd HH:mm:ss”);//设置当前时间的格式,为年-月-日 时-分-秒
System.out.println(dateFormat_min.format(date));
(3)java计算年月日扩展阅读
java 获取当前微秒时间:
package com.ffcs.itm;
public class DataSecUtils {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis()); // 毫秒
System.out.println(getmicTime());
System.out.println(System.currentTimeMillis()); // 毫秒
System.out.println(getmicTime());
}
/**
* @return返回微秒
*/
public static Long getmicTime() {
Long cutime = System.currentTimeMillis() * 1000; // 微秒
Long nanoTime = System.nanoTime(); // 纳秒
return cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
}
}