java.util中一些實用的工具類

一、StringTokenizer,例子以下:java

class StringToken
{
static String str = "1:java;"+"2:perl;"+"3:C/C++;"+"4:shell;";

public static void main(String [] args)
{
StringTokenizer ST
= new StringTokenizer(str,":;");//以:;爲分隔符
while(ST.hasMoreTokens()){
String key
= ST.nextToken();
String value
= ST.nextToken();
System.out.println(key
+":"+value);
}
Result:
1
:java
2:perl
3:C/C++
4:shell

二、Dateshell

class DateDemo
{

public static void main(String [] args)
{
Date date
= new Date();
System.out.println(date);
long sec = date.getTime();
System.out.println(sec);
long sec2 = System.currentTimeMillis();
System.out.println(sec2);

}
}
Result:
Fri Jul
22 15:25:26 CST 2011
1311319526595
1311319526610
三、Calendar
class CalendarDemo
{

public static void main(String [] args)
{
Calendar ca
= Calendar.getInstance();
String [] months
= {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
System.out.println(
"Date:");
System.out.print(months[ca.get(Calendar.MONTH)]
+" ");
System.out.print(ca.get(Calendar.DATE)
+" ");
System.out.println(ca.get(Calendar.HOUR)
+" ");
}
}
Date:
Jul
22 3

4.Randomdom

public class RandomApp{
public static void main(String args[]){
//建立了兩個類Random的對象。
Random ran1=new Random();
Random ran2
=new Random(12345);
//產生各類類型的隨機數
System.out.println("The 1st set of random numbers:");
System.out.println(
"\t Integer:"+ran1.nextInt());
System.out.println(
"\t Long:"+ran1.nextLong());
System.out.println(
"\t Float:"+ran1.nextFloat());//0--1.0之間
System.out.println("\t Double:"+ran1.nextDouble());//0--1.0之間
System.out.println("\t Gaussian:"+ran1.nextGaussian());
//產生同種類型的不一樣的隨機數
System.out.print("The 2nd set of random numbers:");
for(int i=0;i<5;i++){
System.out.print(ran2.nextInt(
12345) + " ");//0--12345之間
}
}
}
Result:
The 1st set of random numbers:
Integer:
-1953322287
Long:
-3756646598696960104
Float:
0.09489989
Double:
0.9836372044386406
Gaussian:
-0.16552271256858825
The 2nd set of random numbers:
8986 9205 7086 4563 310
相關文章
相關標籤/搜索