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;
}
}