導航:首頁 > 編程語言 > javagetyear

javagetyear

發布時間:2024-11-14 17:17:15

java獲取當前年份(不是時間,只要當前的年份),轉化成字元串

importjava.text.SimpleDateFormat;
importjava.util.Date;

publicclassTest{
publicstaticvoidmain(String[]args){
SimpleDateFormatsdf=newSimpleDateFormat("yyyy");
Datedate=newDate();
StringformatDate=sdf.format(date);
System.out.println("格式化後的年回份為答:"+formatDate);
}
}

⑵ 如何用java取得年,月,日,時,分,秒

這個問題可以用兩種方式得到:

方法一:在java中可以使用Date類直接獲得,但是這個方法過時了,不推薦使用。

方法二:使用 java.util.Calendar 類。

代碼例子:

//方法1:雖然還可以用,但是已經不建議使用,已經過時。
Datedate=newDate();
intold_y=date.getYear()+1900;//得到年份。因為得到的是1900年後至今過了多少年,所以要加1900
intold_m=date.getMonth()+1;//因為得到的結果是0~11,故而加一。
intold_d=date.getDate();//得到月份中今天的號數
System.out.println("現在是:"+old_y+"-"+old_m+"-"+old_d+"(使用過時方法)");//

//方法2:推薦使用
Calendarcalendar=Calendar.getInstance();
intnow_y=calendar.get(Calendar.YEAR);//得到年份
intnow_m=calendar.get(Calendar.MONTH)+1;//得到月份
intnow_d=calendar.get(Calendar.DATE);//得到月份中今天的號數
intnow_h=calendar.get(Calendar.HOUR_OF_DAY);//得到一天中現在的時間,24小時制
intnow_mm=calendar.get(Calendar.MINUTE);//得到分鍾數
intnow_s=calendar.get(Calendar.SECOND);//得到秒數
System.out.println("現在是:"+now_y+"-"+now_m+"-"+now_d+""+now_h+":"+now_mm+":"+now_s+"(使用推薦方法)");

結果:

現在是:2015-11-9(使用過時方法)

現在是:2015-11-9 18:7:42(使用推薦方法)

⑶ 用java代碼通過出生時間和當前時間如何計算年齡

自己寫啊 很簡單的

類方法中帶2個參數

當前時間 減去 出生時間

⑷ [Java]如何獲取年份~

GregorianCalendar類中的get()方法,導入Calendar和GregorianCalendar這兩個類。
參考代碼如下:

import java.util.Calendar;
import java.util.GregorianCalendar;

public class demo {

private int year;//年份
public demo() {//構造器
year=0;
}

public int test(){//獲得年份
GregorianCalendar g=new GregorianCalendar();
year=(int)g.get(Calendar.YEAR);
return year;
}

public static void main(String[] args) {
demo d = new demo();
System.out.print(d.test());
}
}

⑸ JAVA :new Date().getYear方法過時了嗎 為什麼要在後面加上1900才能得到真實的年份呢

Date().getYear()這個API的確是被廢棄了,Java中的Date表示的是自1900年以來所經過的時間。所以要得到真實年份的話需要加上1900.

⑹ java的Date類的getYear(),getMonth都有一橫線表示過時,那用什麼

表示這幾個方法都已經過時了!

JDK里的說明:
getYear
@Deprecated --"方法上使用這個註解表示該方法已過時,不推薦專使屬用了"
public int getYear()已過時。
從 JDK 1.1 開始,由 Calendar.get(Calendar.YEAR) - 1900 取代。

Calendar c = Calendar.getInstance();
c.get(Calendar.YEAR);

給你發個鏈接參考下吧!
http://blog.csdn.net/zerogotosum/article/details/1671314

閱讀全文

與javagetyear相關的資料

熱點內容
怎麼把ps文件變成源文件 瀏覽:807
如何把圖片放進這個文件夾 瀏覽:530
誰有qq認證圖標復制 瀏覽:881
2014年web伺服器測試工具 瀏覽:192
win7鏡像文件怎麼用 瀏覽:983
多數據can怎麼發送 瀏覽:997
什麼是夏威夷果種植技術app 瀏覽:160
上海學習編程哪個地方好 瀏覽:958
股票軟體與交易編程軟體哪個好 瀏覽:216
linux如何查看一個文件的位置 瀏覽:911
c頭文件無法打開源文件stdafx 瀏覽:750
蘋果x的桌面上可以建文件夾 瀏覽:368
ug8532位破解版安裝教程 瀏覽:490
電腦網路如何重新驅動 瀏覽:125
win10連接伺服器命令 瀏覽:228
盧克raid活動20升級卷 瀏覽:969
機票預訂什麼網站好 瀏覽:690
讀寫xml配置文件 瀏覽:153
java爬蟲解析html 瀏覽:861
約瑟夫集合java 瀏覽:275

友情鏈接