两个整数相除得到小数并限制小数位数
int a = 1, b = 3;
System.out.println("---->result: " + (a / b));
//获取小数
System.out.println("---->result: " + ((float) a / b));
System.out.println("---->result: " + (a / (float) b));
System.out.println("---->result: " + ((float) a / (float) b));
//限制小数位数
float result = (float) a / b;
DecimalFormat df = new DecimalFormat("0.00");//表示两位小数
System.out.println("---->result: " + df.format(result));// format() 返回的是String
DecimalFormat df1 = new DecimalFormat("#.000");
System.out.println("---->result: " + df1.format(result));
Math 的 floor、round、ceil 函数
/**
* Math.floor(num) : 返回小于 num 的最大整数
* Math.ceil(num) : 返回大于 num 的最小整数
* Math.round(num) :返回 num “四舍五入” 后的整数
* 经典题:Math.round(11.5) 的结果为 12 ,Math.round(-11.5) 的结果为 -11
*/
float[] numList = {1.40f, 1.5f, 1.6f, -1.40f, -1.5f, -1.6f};
for (float num : numList) {
System.out.println("---->result: " + new DecimalFormat("0").format(Math.floor(num)));
System.out.println("---->result: " + Math.ceil(num));
System.out.println("---->result: " + Math.round(num));
}
获取星期名字数组和月份名字数组
Locale locale = Locale.getDefault();//获取当前默认的区域
System.out.println(locale);
//DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(Locale.US);
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
//返回星期名字数组的长度为 8 ,其中 0 的时候为空
String[] weekShortName = dateFormatSymbols.getShortWeekdays();
String[] weekName = dateFormatSymbols.getWeekdays();
for(int i = 1;i<weekShortName.length;i++){
//System.out.println(weekShortName[i]);
System.out.println(weekShortName[i].substring(2,3));
System.out.println(weekName[i]);
}
//返回月份名字数组的长度为 13 ,其中 12 的时候为空
String[] monthShortName = dateFormatSymbols.getShortMonths();
String[] monthName = dateFormatSymbols.getMonths();
for(int i = 0;i<monthName.length;i++){
System.out.println(monthShortName[i]);
System.out.println(monthName[i]);
}
Java 引用
Java 引用分强引用、软引用、弱引用、虚引用。强引用 (SoftReference) 用来保存对象实例,除非 JVM 即将 OutOfMemory ,否则是不会被 GC 回收,这个特性使得它特别适合设计对象Cache。弱引用 (WeekReference) 用来保存的对象实例可以被GC回收掉。这个类通常用于在某处保存对象引用,而又不干扰该对象被GC回收,通常用于Debug、内存监视工具等程序中。
public static void main(String[] args){
A a = new A();
a.str = "Hello,reference";
//弱引用
WeakReference<A> weakReference = new WeakReference<A>(a);
//强引用
SoftReference<A> softReference = new SoftReference<A>(a);
a = null;
int i = 0;
// while(weakReference.get()!=null){
while(softReference.get()!=null){
// System.out.println(String.format("Get str from object of weakReference:%s,count:%d",
// weakReference.get().str,++i));
System.out.println(String.format("--str-->: %s ,count: %d ",
softReference.get().str,++i));
if(i%10==0){
System.gc();
System.out.println("System.gc() was invoked!");
}
try{
Thread.sleep(500);
}catch (InterruptedException e){
}
}
System.out.println("object A was cleared by JVM!");
}
static class A{
public String str;
}
日历获取月份天数
//获取上个月的第一天
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
//获取上个月的最后一天
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.DAY_OF_MONTH, 1);
calendar1.add(Calendar.DATE, -1);
System.out.println(calendar1.get(Calendar.DAY_OF_MONTH));
//获取这个月的最后一天
Calendar calendar2 = Calendar.getInstance();
// calendar2.add(Calendar.MONTH, 1);
// calendar2.set(Calendar.DAY_OF_MONTH,1);
// calendar2.add(Calendar.DATE,-1);
calendar2.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
System.out.println(calendar2.get(Calendar.DAY_OF_MONTH));
获取日历每月显示的数组
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);//设置月份
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) < 1 ? 7 : calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(dayOfWeek);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
int lastDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
int lastDayOfLastMonth = calendar.get(Calendar.DAY_OF_MONTH);
for (int raw = 0; raw < 6; raw++) {
for (int column = 0; column < 7; column++) {
int day = (raw * 7 + column + 1) - dayOfWeek + 1;
if (day < 1) {
day = lastDayOfLastMonth + day;
} else if (day > lastDayOfMonth) {
day = day - lastDayOfMonth;
}
System.out.printf("%2d" + " ", day);
}
System.out.println("");
}
截取函数 substring() 和 split()
/**
* substring(int start);//截取索引start开始后面的字符串
* substring(int start, int end);//截取索引start开始end结束中间的字符串
*/
String num = "123456789";
System.out.println(num.substring(3));//截取3(包括3)开始后面的字符串
System.out.println(num.substring(2,5));//截取2(包括2)开始5(不包括5)结束中间的字符串
System.out.println(num.substring(1,1));//为空
/**
* split(String temp);//截掉temp字符串,返回截取剩下的字符串数组
* split(String temp,int limit);//限制limit个字符串
*/
String str = "123,4,56,789";
for (String temp : str.split(",")) {
System.out.println(temp);
}
for (String temp : str.split(",",2)) {
System.out.println(temp);
}
网友评论