幾個(gè)經(jīng)常見的對(duì)于日期的問題
發(fā)表時(shí)間:2024-06-18 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]1. 怎樣計(jì)算兩個(gè)時(shí)間之間的間隔? 間隔=Date1.getTime()-Date2.getTime();得出來(lái)的是毫秒數(shù). 除1000是秒,再除60是分,再除60是小時(shí).............................. 記住java標(biāo)準(zhǔn)庫(kù)中所有時(shí)間類都以此為基礎(chǔ)轉(zhuǎn)化...
1. 怎樣計(jì)算兩個(gè)時(shí)間之間的間隔?
間隔=Date1.getTime()-Date2.getTime();得出來(lái)的是毫秒數(shù).
除1000是秒,再除60是分,再除60是小時(shí)..............................
記住java標(biāo)準(zhǔn)庫(kù)中所有時(shí)間類都以此為基礎(chǔ)轉(zhuǎn)化的,只是他寫好了一些
轉(zhuǎn)化的方法給你用而已.但都離不開這個(gè)毫秒數(shù)為基礎(chǔ).
2. t=Calendar.getInstance();m=t.get(t.MONTH)+1;這里為什么要加一?
在java語(yǔ)言里,date的month的取值范圍是:0~11,與人們的自然表達(dá)上相差1。
3. 系統(tǒng)時(shí)間與當(dāng)前日期的區(qū)別?
系統(tǒng)時(shí)間確切的說(shuō)應(yīng)該是
System.currentTimeMillis();
new Date()是當(dāng)前日期,雖然它getTime();和System.currentTimeMillis();
一樣,但System.currentTimeMillis();
4. 如何計(jì)算兩個(gè)日期的天數(shù)差值?
long beginTime = beginDate.getTime();
long endTime2 = endDate.getTime();
long betweenDays = (long)((endTime - beginTime) / (1000 * 60 * 60 *24) + 0.5);
5. 如何比較日期時(shí)間大小?
第一種方法:
use Calendar object to compare
java.util.Calendar class can be used to compare date. In order to do this,
you guy should parse that string into int year, month, day and construct a
Calendar object, and then do comparison.
Below is a sample
StringTokenizer token = new StringTokenizer(your string,"-");
int year = Integer.parseInt(token.nextToken());
int month = Integer.parseInt(token.nextToken());
int day = Integer.parseInt(token.nextToken());
Calendar date = Calendar.getInstance();
date.set(year,month,day);
Calendar today = Calendar.getInstacne();
if(date.after(today)){
//......
}
第二種方法
Date nowDate=new Date();//當(dāng)前時(shí)間\r
long nowTime=nowDate.getTime;
long lastTime=userTime.longValue();//以前的時(shí)間\r
long time=nowTime-lastTime;//時(shí)間相減比較。
if(time>(long)60000)//1分鐘{}
另外可用以下參考
用時(shí)間戳,Date.getTime()可以把當(dāng)前時(shí)間改成時(shí)間戳,
用CompareTo();
用before(),after(),equals();
6. 格式化日期的問題\r
目的:
第一次求日期
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");
String riqi=formatter.format(currentTime_1);
第二次求時(shí)間\r
java.text.DateFormat format1 = new java.text.SimpleDateFormat("hhmmss");
java.util.Date currentTime_2 = new java.util.Date();
String shijian=format1.format(currentTime_2);
得到的結(jié)果是
2002-02-19和115324(11點(diǎn)53分24秒)
實(shí)現(xiàn):
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd-H-mm-ss");
java.util.Date currentTime_1 = new java.util.Date();
String str_date = formatter.format(currentTime_1);
StringTokenizer token = new StringTokenizer(str_date,"-");
String year = token.nextToken();
String month= token.nextToken();
String day = token.nextToken();
String hh = token.nextToken();
String mm = token.nextToken();
String ss = token.nextToken();
String riqi=year+"年\\"+month+"月"+day+"日"+" "+hh+"點(diǎn)\\"+mm+"分"+ss+"秒\\";
String newdir=year+month+day;
String wenjian = hh+mm+ss;
7. 怎么得到一個(gè)月的天數(shù)?
java.util.Calendar date = java.util.Calendar.getInstance();
System.out.println(date.getActualMaximum(date.DAY_OF_MONTH));