标签:效率 date set 当前系统时间 main string 位置 exit 获取
package oop;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
String str=" dengwenxiong ";
//length()
int num1=str.length();
System.out.println(num1);
//charAt()
char a=str.charAt(0);
System.out.println(a);
//containas()
boolean f=str.contains("wen");
System.out.println(f);
//toCharArray()
char[] b=str.toCharArray();
System.out.println(Arrays.toString(b));
//indexOf()
int index1=str.indexOf("wn");
System.out.println(index1);
//lastIndexOf()
int index2=str.lastIndexOf("wn");
System.out.println(index2);
//trim()
String str2=str.trim();
System.out.println(str2);
//toUpperCase()
String str3=str.toUpperCase();
System.out.println(str3);
//endsWith()
boolean g=str.endsWith(" ");
System.out.println(g);
//replace()
String str4=str.replace(‘n‘,‘o‘);
System.out.println(str4);
//split()
String[] str5=str.split("on");
System.out.println(Arrays.toString(str5));
}
}
效率比String高,也比String节省内存
package oop;
import java.util.Arrays;
public class Test {
//StringBulider与StringBuffer使用方法一样
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
//append()追加
sb.append("deng");
System.out.println(sb);
//insert()插入
sb.insert(0,"shit ");
System.out.println(sb);
//replace()替换
sb.replace(0,5,"haha");
System.out.println(sb);
//delete()删除;含头不含尾
sb.delete(0,2);
System.out.println(sb);
}
}
double是近似存储
位于java.math包中,用于精确计算浮点数。
创建方式:
BigDecimal bd=new BigDecimal("1.0");
package oop;
import java.beans.beancontext.BeanContext;
import java.math.BigDecimal;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
BigDecimal d1=new BigDecimal("1.0");
BigDecimal d2=new BigDecimal("0.9");
//减法subtract()
System.out.println(d1.subtract(d2));
//加法add()
System.out.println(d1.add(d2));
//乘法multiply()
System.out.println(d1.multiply(d2));
//除法divide()可设置精确值
System.out.println(d1.divide(d2,2, BigDecimal.ROUND_CEILING));
}
}
package oop;
import java.util.Date;
public class Test {
public static void main(String[] args) {
//创建Date对象
//今天
Date date1=new Date();
System.out.println(date1.toLocaleString());
//昨天
Date date2=new Date(date1.getTime()-24*60*60*1000);
System.out.println(date2.toLocaleString());
//方法after,before
boolean f1=date2.after(date1);
System.out.println(f1);
//comareTo()
int d=date1.compareTo(date2);
System.out.println(d);
}
}
package oop;
import java.util.Calendar;
public class Test {
public static void main(String[] args) {
//创建calendar对象
Calendar calendar=Calendar.getInstance();
System.out.println(calendar.getTime().toLocaleString());
System.out.println(calendar.getTimeInMillis());
//获取时间信息
int year=calendar.get(Calendar.YEAR);
//月是从0-11
int month=calendar.get(Calendar.MONTH);
int day=calendar.get(Calendar.DAY_OF_MONTH);
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
//修改时间
Calendar calendar2=Calendar.getInstance();
calendar2.set(Calendar.YEAR,2009);
System.out.println(calendar2.getTime().toLocaleString());
//获得最大值
int max=calendar2.getActualMaximum(Calendar.MONDAY);
System.out.println(max);
package oop;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws Exception {
//创建SimpleDateFormat对象
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date=new Date();
//格式化date,把日期转成字符串
String str=sdf.format(date);
System.out.println(str);
//解析,把字符串转成日期
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy/MM/dd");
Date date2=sdf2.parse("1998/01/02");
System.out.println(date2);
}
}
package oop;
public class Test {
public static void main(String[] args) {
//数组复制
//src:源数组
//srcPos:从那个位置开始复制
//dest:目标数组
//dest:目标数组的位置
//length:复制的长度
int[] arr={1,2,3,4,5,6};
int[] dest=new int[8];
System.arraycopy(arr,0,dest,1,3);
for(int a:dest){
System.out.println(a);
}
//获取当前系统时间
System.out.println(System.currentTimeMillis());
//退出jvm
System.exit(0);
}
}
标签:效率 date set 当前系统时间 main string 位置 exit 获取
原文地址:https://www.cnblogs.com/python-road/p/13220844.html