Data.getYear(), 這個(gè)方法已經(jīng)過(guò)時(shí)了,建議用下面的方法取得日期。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:國(guó)際域名空間、網(wǎng)頁(yè)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、臥龍網(wǎng)站維護(hù)、網(wǎng)站推廣。
import java.util.* ;
public class DateDemo{
public static void main(String args[]){
Calendar calendar = new GregorianCalendar(); // 實(shí)例化Calendar類對(duì)象
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + (calendar.get(Calendar.MONTH) + 1));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
}
};
1、獲取當(dāng)前的時(shí)間
Date date=new Date();//此時(shí)date為當(dāng)前的時(shí)間
2、設(shè)置時(shí)間的格式
Date date=new Date();//此時(shí)date為當(dāng)前的時(shí)間
System.out.println(date);
SimpleDateFormat dateFormat=new SimpleDateFormat(“YYYY-MM-dd”);//設(shè)置當(dāng)前時(shí)間的格式,為年-月-日
System.out.println(dateFormat.format(date));
SimpleDateFormat dateFormat_min=new SimpleDateFormat(“YYYY-MM-dd HH:mm:ss”);//設(shè)置當(dāng)前時(shí)間的格式,為年-月-日 時(shí)-分-秒
System.out.println(dateFormat_min.format(date));
擴(kuò)展資料
java 獲取當(dāng)前微秒時(shí)間:
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;
}
}
Calendar這個(gè)類是很強(qiáng)大的,你的這個(gè)需求,一句話就能搞定 完整代碼如下:
import java.util.Calendar;
public class Test {
public static void main(String[] args) {
//獲取今天日期
Calendar cal = Calendar.getInstance();
//輸出今天是今天多少天
//今天是20111111,所以是第315天 輸出:315
System.out.println(cal.get(Calendar.DAY_OF_YEAR));
}
}
有兩種方法:
方法一:用java.util.Date類來(lái)實(shí)現(xiàn),并結(jié)合java.text.DateFormat類來(lái)實(shí)現(xiàn)時(shí)間的格式化,看下面代碼:
import java.util.*;
import java.text.*;
//以下默認(rèn)時(shí)間日期顯示方式都是漢語(yǔ)語(yǔ)言方式
//一般語(yǔ)言就默認(rèn)漢語(yǔ)就可以了,時(shí)間日期的格式默認(rèn)為MEDIUM風(fēng)格,比如:2008-6-16 20:54:53
//以下顯示的日期時(shí)間都是再Date類的基礎(chǔ)上的來(lái)的,還可以利用Calendar類來(lái)實(shí)現(xiàn)見(jiàn)類TestDate2.java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
DateFormat d1 = DateFormat.getDateInstance(); //默認(rèn)語(yǔ)言(漢語(yǔ))下的默認(rèn)風(fēng)格(MEDIUM風(fēng)格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);
DateFormat d2 = DateFormat.getDateTimeInstance();
String str2 = d2.format(now);
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
DateFormat d4 = DateFormat.getInstance(); //使用SHORT風(fēng)格顯示日期和時(shí)間
String str4 = d4.format(now);
DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //顯示日期,周,時(shí)間(精確到秒)
String str5 = d5.format(now);
DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //顯示日期。時(shí)間(精確到秒)
String str6 = d6.format(now);
DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //顯示日期,時(shí)間(精確到分)
String str7 = d7.format(now);
DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //顯示日期,時(shí)間(精確到分)
String str8 = d8.format(now);//與SHORT風(fēng)格相比,這種方式最好用
System.out.println("用Date方式顯示時(shí)間: " + now);//此方法顯示的結(jié)果和Calendar.getInstance().getTime()一樣
System.out.println("用DateFormat.getDateInstance()格式化時(shí)間后為:" + str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化時(shí)間后為:" + str2);
System.out.println("用DateFormat.getTimeInstance()格式化時(shí)間后為:" + str3);
System.out.println("用DateFormat.getInstance()格式化時(shí)間后為:" + str4);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時(shí)間后為:" + str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時(shí)間后為:" + str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時(shí)間后為:" + str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時(shí)間后為:" + str8);
}
}
運(yùn)行結(jié)果:
用Date方式顯示時(shí)間: Mon Jun 16 20:54:53 CST 2008
用DateFormat.getDateInstance()格式化時(shí)間后為:2008-6-16
用DateFormat.getDateTimeInstance()格式化時(shí)間后為:2008-6-16 20:54:53
用DateFormat.getTimeInstance()格式化時(shí)間后為:20:54:53
用DateFormat.getInstance()格式化時(shí)間后為:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時(shí)間后為
:2008年6月16日 星期一 下午08時(shí)54分53秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時(shí)間后為
:2008年6月16日 下午08時(shí)54分53秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時(shí)間后
為:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時(shí)間
后為:2008-6-16 20:54:53
方法二:用java.util.Calendar類來(lái)實(shí)現(xiàn),看下面:
import java.util.*;
import java.text.*;
//以下是利用Calendar類來(lái)實(shí)現(xiàn)日期時(shí)間的,和Date類相比較比較簡(jiǎn)單
public class TestDate2 {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//獲取年份
int month=ca.get(Calendar.MONTH);//獲取月份
int day=ca.get(Calendar.DATE);//獲取日
int minute=ca.get(Calendar.MINUTE);//分
int hour=ca.get(Calendar.HOUR);//小時(shí)
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
System.out.println("用Calendar.getInstance().getTime()方式顯示時(shí)間: " + ca.getTime());
System.out.println("用Calendar獲得日期是:" + year +"年"+ month +"月"+ day + "日");
System.out.println("用Calendar獲得時(shí)間是:" + hour +"時(shí)"+ minute +"分"+ second +"秒");
System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個(gè)例子正好是周二,故結(jié)果顯示2,如果你再周6運(yùn)行,那么顯示6)
}
}
運(yùn)行結(jié)果是:
用Calendar.getInstance().getTime()方式顯示時(shí)間: Mon Jun 16 21:54:21 CST 2008
用Calendar獲得日期是:2008年5月16日
用Calendar獲得時(shí)間是:9時(shí)54分21秒
2
總結(jié):中的來(lái)說(shuō),方法二是最方便的,方法一顯得分笨拙,不過(guò)看個(gè)人喜歡了。
還有一種方法利用System.currentTimeMillis()也可以。
分享名稱:java代碼獲取今年 java獲取下一年
URL分享:http://www.rwnh.cn/article16/ddjcgdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站制作、云服務(wù)器、網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)