java str2date,java date类与string类实例代码分享
Date類用來指定日期和時間,其構(gòu)造函數(shù)及常用方法如下:
publicDate()
從當(dāng)前時間構(gòu)造日期時間對象。
publicStringtoString()
轉(zhuǎn)換成字符串。
publiclonggetTime()
返回自新世紀(jì)以來的毫秒數(shù),可以用于時間計算。
【例3.10】測試執(zhí)行循環(huán)花費(fèi)的時間(數(shù)量級為毫秒),具體時間情況如圖3.9所示。源程序代碼如下:
//程序文件名為UseDate.java
import java.util.Date;
public class UseDate
{
public static void main(String[] args)
{
Date dOld = new Date();
long lOld = dOld.getTime();
System.out.println("循環(huán)前系統(tǒng)時間為:" +dOld.toString());
int sum = 0;
for (int i=0; i<100; i++)
{
sum += i;
}
Date dNew = new Date();
long lNew = dNew.getTime();
System.out.println("循環(huán)后系統(tǒng)時間為:" +dNew.toString());
System.out.println("循環(huán)花費(fèi)的毫秒數(shù)為:" + (lNew - lOld));
}
}
結(jié)果顯示:
String類
String類用于操作非數(shù)值型字符串,它提供了七類方法操作,分別為字符串創(chuàng)建、字符串長度、字符串比較、字符串檢索、字符串截取、字符串運(yùn)算和數(shù)據(jù)類型轉(zhuǎn)換。
2. 字符串長度
public int length()
返回字符串的長度。
3. 字符串比較
public boolean equals(Object anObject)
比較字符串是否與anObject代表的字符串相同(區(qū)分大小寫)。
public boolean equalsIgnoreCase(String anotherString)
比較字符串是否與anotherString相同(不區(qū)分大小寫)。
1. 字符串創(chuàng)建
public String()
構(gòu)造一個空字符串。
public String(char[] value)
使用字符數(shù)組value中的字符以構(gòu)造一個字符串。
public String(String original)
使用原字符串original的拷貝以構(gòu)造一個新字符串。
4. 字符串檢索
public int indexOf(String str)
返回一個字符串中str第一次出現(xiàn)所在的位置。
public int indexOf(String str, int fromIndex)
返回從fromIndex開始字符串str出現(xiàn)所在的位置。
5. 字符串截取
public String substring(int beginIndex, int endIndex)
返回benginIndex到endIndex之間的字符串。
6. 字符串運(yùn)算
運(yùn)算符為“+”,表示連接運(yùn)算。下面的行語句輸出連接的字符串。
System.out.println("Hashtable:" + hScore.toString());
【例3.11】操作字符串,輸出結(jié)果如圖3.10所示。源程序代碼如下:
//程序文件名為TestString.java
public class TestString
{
public static void main(String[] args)
{
String str = new String("The substring begins at the specified beginIndex.");
String str1 = new String("string");
String str2 = new String();
int size = str.length();//字符串長度
int flag = str.indexOf("substring");
str2 = str.substring(flag,flag + 9);//取子字符串
System.out.println("字符串" + str + "\n總長度為:" + size);
if(str1.equals(str2))//判斷是否相等
System.out.println("截取的字符串為:" + str1);
else
System.out.println("截取的字符串為:" + str2);
}
}
結(jié)果顯示:
總結(jié)
以上就是本文關(guān)于java date類與string類實(shí)例代碼分享的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
總結(jié)
以上是生活随笔為你收集整理的java str2date,java date类与string类实例代码分享的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 发送表格,PHP邮件表格,带有使
- 下一篇: matlab 线性拟合相关系数,Matl