Java语言的基础知识11
? ? ? ? ? ? ? ? ? ? ? ? ? ? 第十二章(編程常用類)
1、Date類最常用的構造方法就是默認的無參數的Date()構造方法,它使用系統中當前日期和時間創建并初始化Date類對象。
? Date now = new Date();
Date類的另一個構造方法是Date(long date),這個構造方法接受一個Long類型的整數來初始化Date對象
long timeMillis = System.currentTimeMillis();
Date date = new Date(timeMillis);
比較Date對象 有after()和before();二者都是返回布爾值。
2、Date類的CompareTo()方法用于比較兩個日期對象順序,返回的是int類型。
package com.lenovo.dishisanzhang;
import java.util.Date;
public class DateTest1 {
public static void main(String[] args) {
Date now = new Date();
long dateMillis = System.currentTimeMillis() - 5000;
Date otherDate = new Date(dateMillis);
int compare = now.compareTo(otherDate);
switch (compare) {
case 0:
System.out.println("兩個日期表示的時間相同");
break;
case 1:
System.out.println("othrDate對象標示的時間小于now對象表示的時間");
break;
case -1:
System.out.println("othrDate對象標示的時間大于now對象表示的時間");
break;
default:
System.out.println(compare);
break;
}
}
}
3、String類的靜態format()方法通過特殊轉移符作為參數可以實現對日期和時間的格式化。format()方法用于創建格式化的字符串,它有兩種重載形式:
format(String format,Object....args)
format(Local local,String format,Object....args)
format()方法不僅可以完成日期的格式化,也可以實現時間的格式化。
package com.lenovo.dishisanzhang;
import java.util.Date;
public class GetDate {
? ?public static void main(String[] args) {
? Date date = new Date();
String hour = String.format("%tH", date);
String minute = String.format("%tM", date);
String day = String.format("%tS", date);
System.out.println("現在是北京時間:"+" "+hour+"時"+minute+"分"+day+"秒");
}
}
4、Math類包含了所有用于數學運算的函數方法,這些方法都是靜態的,所以每個方法只要使用"Math.數學方法"就可以調用,使用起來比較簡單。Math類中除了函數方法之外還存在一些常用的數學常量,例如圓周率、E等。這些數字常量作為Math類的成員變量出現,調用起來很簡單,可以使用以下格式進行調用
Math.PI
Math.E
5、java提供了2種方式產生隨機數,分別為調用Math類的random()方法和Random()類提供的產生的各種數據類型隨機數的方法。
0<=Math.random()<1.0
產生n1到n2之間的隨機數
int s = (int)n1+(int)(Math.random*(n2-n1))
char c = (char)(Math.random() * 26 + 'a');
這里面其實進行了一次系統默認的數據類型轉換和一個強制類型數據類型轉換
默認的數據類型轉換也稱為 隱式的數據類型轉換
當然了 強制。。 稱為 顯式
首先 Math.random() * 26 + 'a' 這里面就進行了 隱式轉換
Math.random() * 26 的結果 是0-26的double 那么就是一個double+char 的表達式 這個時候根據規則會從小數據類型默認的轉換為大數據類型 然后進行計算
也就是說 0-26的一個double + 97.000000 那么它的結果當然也是一個double型
最后強制的將這個double型轉為char型
(char)('a‘+Math.random()*('z'-'a'+1));
在java中又提供了一種可以獲取隨機數的方式,那就是java.util.Random類??梢酝ㄟ^實例化一個Random對象創建一個隨機數生成器。
Random random= new Random();
其中參數也可以是隨機說生成的種子,
6、DecimalFormat 數字格式化類,
首先實例化DecimalFormat對象,然后調用方法。
7、
infoArea.setBorder(new LineBorder(Color.black, 1,false));
Parameters:
color - the color of the border
thickness - the thickness of the border
roundedCorners - whether or not border corners should be round
Double number=Double.parseDouble(text.trim());//字符串轉換為Double
8、
? ?private void keep12Int(double number,DecimalFormat dmf) {
dmf.applyPattern("強制截取12位整數:000000000000\n");
String numString =dmf.format(number);
infoArea.append(numString);
}
? ?private void keep6(double number,DecimalFormat dmf){
? dmf.applyPattern("強制保留6位小數:#.000000\n");
? String numString =dmf.format(number);
infoArea.append(numString);
? ?}
? ?private void keep1_6(double number,DecimalFormat dmf){
? dmf.applyPattern("保留1-6位小數:#.######\n");
? String numString =dmf.format(number);
?infoArea.append(numString);
? ?}
9、
Date date = new Date();
String timesString =String.format("%tH:%tM:%tS %tp",date,date,date,date);
轉載于:https://blog.51cto.com/7129486/1541076
總結
以上是生活随笔為你收集整理的Java语言的基础知识11的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flex中的图表
- 下一篇: 深入理解Java:注解